from sys import stdin

nii=lambda:list(map(int,stdin.readline().split()))

lnii=lambda:list(map(int,stdin.readline().split()))

from collections import deque



h,w=nii()

ch,cw=nii()

dh,dw=nii()

s=[list(eval(input())) for i in range(h)]



ch-=1

cw-=1

dh-=1

dw-=1



ans=[[-1]*w for i in range(h)]

ans[ch][cw]=0



que=deque()

que.append((ch,cw))



def BFS():

  while que:

    y,x=que.popleft()



    if y==dh and x==dw:

      print((ans[y][x]))

      exit()



    for dy,dx in [[-1,0],[1,0],[0,-1],[0,1]]:

      ny=y+dy

      nx=x+dx

      if 0<=ny<h and 0<=nx<w and s[ny][nx]!='#':

        que.appendleft((ny,nx))

        s[ny][nx]='#'

        ans[ny][nx]=ans[y][x]



      for i in range(-2,3):

        for j in range(-2,3):

          ny=y+i

          nx=x+j

          if 0<=ny<h and 0<=nx<w and s[ny][nx]!='#' and ans[ny][nx]==-1:

            que.append((ny,nx))

            ans[ny][nx]=ans[y][x]+1



BFS()



print((-1))
from sys import stdin

nii=lambda:list(map(int,stdin.readline().split()))

lnii=lambda:list(map(int,stdin.readline().split()))

from collections import deque



h,w=nii()

ch,cw=nii()

dh,dw=nii()

s=[list(eval(input())) for i in range(h)]



ch-=1

cw-=1

dh-=1

dw-=1



dist=[[-1]*w for i in range(h)]

dist[ch][cw]=0



que=deque()

que.append((ch,cw))



def BFS(que):

  while que:

    y,x=que.popleft()



    if y==dh and x==dw:

      print((dist[y][x]))

      exit()



    for dy,dx in [[1,0],[-1,0],[0,1],[0,-1]]:

      ny=y+dy

      nx=x+dx

      if 0<=ny<h and 0<=nx<w and s[ny][nx]!='#':

        que.appendleft((ny,nx))

        s[ny][nx]='#'

        dist[ny][nx]=dist[y][x]



    for i in range(-2,3):

      for j in range(-2,3):

        ny=y+i

        nx=x+j

        if 0<=ny<h and 0<=nx<w and s[ny][nx]!='#' and dist[ny][nx]==-1:

          que.append((ny,nx))

          dist[ny][nx]=dist[y][x]+1



BFS(que)



print((-1))
from sys import stdin

nii = lambda: list(map(int, stdin.readline().split()))
lnii = lambda: list(map(int, stdin.readline().split()))
from collections import deque

h, w = nii()
ch, cw = nii()
dh, dw = nii()
s = [list(eval(input())) for i in range(h)]
ch -= 1
cw -= 1
dh -= 1
dw -= 1
ans = [[-1] * w for i in range(h)]
ans[ch][cw] = 0
que = deque()
que.append((ch, cw))


def BFS():
    while que:
        y, x = que.popleft()
        if y == dh and x == dw:
            print((ans[y][x]))
            exit()
        for dy, dx in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
            ny = y + dy
            nx = x + dx
            if 0 <= ny < h and 0 <= nx < w and s[ny][nx] != "#":
                que.appendleft((ny, nx))
                s[ny][nx] = "#"
                ans[ny][nx] = ans[y][x]
            for i in range(-2, 3):
                for j in range(-2, 3):
                    ny = y + i
                    nx = x + j
                    if (
                        0 <= ny < h
                        and 0 <= nx < w
                        and s[ny][nx] != "#"
                        and ans[ny][nx] == -1
                    ):
                        que.append((ny, nx))
                        ans[ny][nx] = ans[y][x] + 1


BFS()
print((-1))

from sys import stdin

nii = lambda: list(map(int, stdin.readline().split()))
lnii = lambda: list(map(int, stdin.readline().split()))
from collections import deque

h, w = nii()
ch, cw = nii()
dh, dw = nii()
s = [list(eval(input())) for i in range(h)]
ch -= 1
cw -= 1
dh -= 1
dw -= 1
dist = [[-1] * w for i in range(h)]
dist[ch][cw] = 0
que = deque()
que.append((ch, cw))


def BFS(que):
    while que:
        y, x = que.popleft()
        if y == dh and x == dw:
            print((dist[y][x]))
            exit()
        for dy, dx in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
            ny = y + dy
            nx = x + dx
            if 0 <= ny < h and 0 <= nx < w and s[ny][nx] != "#":
                que.appendleft((ny, nx))
                s[ny][nx] = "#"
                dist[ny][nx] = dist[y][x]
        for i in range(-2, 3):
            for j in range(-2, 3):
                ny = y + i
                nx = x + j
                if (
                    0 <= ny < h
                    and 0 <= nx < w
                    and s[ny][nx] != "#"
                    and dist[ny][nx] == -1
                ):
                    que.append((ny, nx))
                    dist[ny][nx] = dist[y][x] + 1


BFS(que)
print((-1))

-ans = [[-1] * w for i in range(h)] -ans[ch][cw] = 0 +dist = [[-1] * w for i in range(h)] +dist[ch][cw] = 0 -def BFS(): +def BFS(que): -            print((ans[y][x])) +            print((dist[y][x])) -        for dy, dx in [[-1, 0], [1, 0], [0, -1], [0, 1]]: +        for dy, dx in [[1, 0], [-1, 0], [0, 1], [0, -1]]: -                ans[ny][nx] = ans[y][x] -            for i in range(-2, 3): -                for j in range(-2, 3): -                    ny = y + i -                    nx = x + j -                    if ( -                        0 <= ny < h -                        and 0 <= nx < w -                        and s[ny][nx] != "#" -                        and ans[ny][nx] == -1 -                    ): -                        que.append((ny, nx)) -                        ans[ny][nx] = ans[y][x] + 1 +                dist[ny][nx] = dist[y][x] +        for i in range(-2, 3): +            for j in range(-2, 3): +                ny = y + i +                nx = x + j +                if ( +                    0 <= ny < h +                    and 0 <= nx < w +                    and s[ny][nx] != "#" +                    and dist[ny][nx] == -1 +                ): +                    que.append((ny, nx)) +                    dist[ny][nx] = dist[y][x] + 1 -BFS() +BFS(que)