from collections import deque H,W = map(int,input().split()) sx,sy,gx,gy = map(int,input().split()) B = [[0 for _ in range(W+1)]] for _ in range(H): b = [0]+list(map(int,list(input()))) B.append(b) A = [[0 for _ in range(W+1)] for _ in range(H+1)] A[sx][sy] = 1 que = deque([(sx,sy)]) while que: i,j = que.popleft() for di,dj in [(1,0),(0,1),(-1,0),(0,-1)]: ni = i+di nj = j+dj if 1<=ni<=H and 1<=nj<=W and abs(B[ni][nj]-B[i][j])<=1 and A[ni][nj]==0: A[ni][nj] = 1 que.append((ni,nj)) for di,dj in [(2,0),(0,2),(-2,0),(0,-2)]: ni = i+di nj = j+dj if 1<=ni<=H and 1<=nj<=W and B[ni][nj]==B[i][j] and B[i+di//2][j+dj//2]