for m in CartesianIndices(M) and then use m[1] and m[2] in your code.
for m in CartesianIndices(M)
m[1]
m[2]