enumerate @ 2024-04-12 20:14:52
from math import inf
from collections import deque
import sys
n = int(sys.stdin.readline())
g = [sys.stdin.readline() for i in range(n)]
x1,y1,x2,y2 = [int(i) - 1 for i in sys.stdin.readline().split()]
vis = [[inf] * n for i in range(n)]
vis[x1][y1] = 0
quequ = deque()
quequ.append((x1,y1))
dirs = [(1,0),(-1,0),(0,-1),(0,1)]
while quequ:
x,y = quequ.popleft()
for dir in dirs:
dx = x + dir[0]
dy = y + dir[1]
if 0 <= dx < n and 0 <= dy < n:
if g[dx][dy] == '0' and vis[dx][dy] == inf:
vis[dx][dy] = vis[x][y] + 1
quequ.append((dx,dy))
print(vis[x2][y2] if vis[x2][y2] != inf else -1)