I think your problem is that you initially define your array like this after the user enters M and N:



So, for example if M=N=5 then we have created a 5x5 matrix (the arraz Z has 25 elements)....