example of a shitty code
game=[[1,1,1,1,1,1,1], [1,'A',1,1,0,0,1], [1,0,0,0,0,1,1], [1,0,0,1,1,1,1], [1,1,0,0,0,'B',1], [1,1,1,1,1,1,1]] rows=len(game) cols=len(game[0]) graph={} for i in range(1,rows-1): for i in range(1,rows-1): if(game[i][j]!=1): adj=[] for ele in [(i-1,j),(i+1,j),(i,j-1),(i,j+1)]: if game[ele[0]][ele[1]] == 0 or game[ele[0]][ele[1]]=='B' : adj.append((ele[0],ele[1])) graph[(i,j)]=adj print(graph) {(1, 1): [(2, 1)], (1, 4): [(2, 4), (1, 5)], (1, 5): [(1, 4)], (2, 1): [(3, 1), (2, 2)], (2, 2): [(3, 2), (2, 1), (2, 3)], (2, 3): [(2, 2), (2, 4)], (2, 4): [(1, 4), (2, 3)], (3, 1): [(2, 1), (3, 2)], (3, 2): [(2, 2), (4, 2), (3, 1)], (4, 2): [(3, 2), (4, 3)], (4, 3): [(4, 2), (4, 4)], (4, 4): [(4, 3), (4, 5)], (4, 5): [(4, 4)]}