小技巧:从(n-1, n-1) 倒着找 (0, 0), 最后打印路线方便些
n = int(input())
mat = [list(map(int, input().split())) for _ in range(n)]
path = [[None] * n for _ in range(n)]
q = [(n - 1, n - 1)]
while q:
nq = []
for x, y in q:
for dx, dy in zip((-1, 0, 1, 0), (0, -1, 0, 1)):
nx, ny = x + dx, y + dy
if 0 <= nx < n and 0 <= ny < n and mat[nx][ny] == 0 and path[nx][ny] == None:
path[nx][ny] = (x, y)
nq.append((nx, ny))
q = nq
x, y = 0, 0
while not( x == y == n - 1):
print(x, y)
x, y = path[x][y]
print(n - 1, n - 1)