for r in range(rows):
    for c in range(cols):
        i = r * cols + c
if c > 0:
    M[i - 1, i] = M[i, i - 1] = 1
if r > 0:
    M[i - cols, i] = M[i, i - cols] = 1
How can you make an adjacency matrix which would emulate a 2d grid