H, W = list(map(int, input().split()))

sh, sw = list(map(int, input().split()))

gh, gw = list(map(int, input().split()))



sh += 1

sw += 1

gh += 1

gw += 1



S = [0]*(H+4)

S[0] = '*'*(W+4)

S[1] = '*'*(W+4)

S[-2] = '*'*(W+4)

S[-1] = '*'*(W+4)

for h in range(2,H+2):

  S[h] = '**' + eval(input()) + '**'



visited = [[float('inf')]*(W+4) for _ in range(H+4)]

dh1 = [0,1,0,-1]

dw1 = [1,0,-1,0]

dh2 = [2,2,2,2,2,1,1,1,1,0,0,-1,-1,-1,-1,-2,-2,-2,-2,-2]

dw2 = [-2,-1,0,1,2,-2,-1,1,2,-2,2,-2,-1,1,2,-2,-1,0,1,2]



from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge

d = []

heapify(d)

heappush(d,(0,sh,sw))

ans = float('inf')

while len(d)>0:

  cnt,h,w = heappop(d)

  if cnt>=ans:

    break

  visited[h][w] = cnt

  for i in range(4):

    h1 = h+dh1[i]

    w1 = w+dw1[i]

    if (h1,w1)==(gh,gw):

      ans = cnt

    if S[h1][w1]=='.':

      if visited[h1][w1]>cnt:

        visited[h1][w1]=cnt

        heappush(d,(cnt,h1,w1))

  for i in range(20):

    h2 = h+dh2[i]

    w2 = w+dw2[i]

    if (h2,w2)==(gh,gw):

      ans = cnt+1

    if S[h2][w2]=='.':

      if visited[h2][w2]>cnt+1:

        visited[h2][w2]=cnt+1

        heappush(d,(cnt+1,h2,w2))

ans = ans if ans<float('inf') else -1

print(ans)

H, W = list(map(int, input().split()))

sh, sw = list(map(int, input().split()))

gh, gw = list(map(int, input().split()))



sh += 1

sw += 1

gh += 1

gw += 1

s = sh*3000+sw

g = gh*3000+gw



S = [0]*(H+4)

S[0] = '*'*(W+4)

S[1] = '*'*(W+4)

S[-2] = '*'*(W+4)

S[-1] = '*'*(W+4)

for h in range(2,H+2):

  S[h] = '**' + eval(input()) + '**'



visited = [[float('inf')]*(W+4) for _ in range(H+4)]

dh1 = [0,1,0,-1]

dw1 = [1,0,-1,0]

dh2 = [2,2,2,2,2,1,1,1,1,0,0,-1,-1,-1,-1,-2,-2,-2,-2,-2]

dw2 = [-2,-1,0,1,2,-2,-1,1,2,-2,2,-2,-1,1,2,-2,-1,0,1,2]



from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge

d = []

heapify(d)

heappush(d,s)

ans = float('inf')

while len(d)>0:

  a = heappop(d)

  cnt,a1 = divmod(a,10**7)

  h,w = divmod(a1,3000)

  if cnt>=ans:

    break

  visited[h][w] = cnt

  for i in range(4):

    h1 = h+dh1[i]

    w1 = w+dw1[i]

    if (h1,w1)==(gh,gw):

      ans = cnt

    if S[h1][w1]=='.':

      if visited[h1][w1]>cnt:

        visited[h1][w1]=cnt

        heappush(d,cnt*(10**7)+h1*3000+w1)

  for i in range(20):

    h2 = h+dh2[i]

    w2 = w+dw2[i]

    if (h2,w2)==(gh,gw):

      ans = cnt+1

    if S[h2][w2]=='.':

      if visited[h2][w2]>cnt+1:

        visited[h2][w2]=cnt+1

        heappush(d,(cnt+1)*(10**7)+h2*3000+w2)

ans = ans if ans<float('inf') else -1

print(ans)
H, W = list(map(int, input().split()))
sh, sw = list(map(int, input().split()))
gh, gw = list(map(int, input().split()))
sh += 1
sw += 1
gh += 1
gw += 1
S = [0] * (H + 4)
S[0] = "*" * (W + 4)
S[1] = "*" * (W + 4)
S[-2] = "*" * (W + 4)
S[-1] = "*" * (W + 4)
for h in range(2, H + 2):
    S[h] = "**" + eval(input()) + "**"
visited = [[float("inf")] * (W + 4) for _ in range(H + 4)]
dh1 = [0, 1, 0, -1]
dw1 = [1, 0, -1, 0]
dh2 = [2, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, -1, -1, -1, -1, -2, -2, -2, -2, -2]
dw2 = [-2, -1, 0, 1, 2, -2, -1, 1, 2, -2, 2, -2, -1, 1, 2, -2, -1, 0, 1, 2]
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge

d = []
heapify(d)
heappush(d, (0, sh, sw))
ans = float("inf")
while len(d) > 0:
    cnt, h, w = heappop(d)
    if cnt >= ans:
        break
    visited[h][w] = cnt
    for i in range(4):
        h1 = h + dh1[i]
        w1 = w + dw1[i]
        if (h1, w1) == (gh, gw):
            ans = cnt
        if S[h1][w1] == ".":
            if visited[h1][w1] > cnt:
                visited[h1][w1] = cnt
                heappush(d, (cnt, h1, w1))
    for i in range(20):
        h2 = h + dh2[i]
        w2 = w + dw2[i]
        if (h2, w2) == (gh, gw):
            ans = cnt + 1
        if S[h2][w2] == ".":
            if visited[h2][w2] > cnt + 1:
                visited[h2][w2] = cnt + 1
                heappush(d, (cnt + 1, h2, w2))
ans = ans if ans < float("inf") else -1
print(ans)

H, W = list(map(int, input().split()))
sh, sw = list(map(int, input().split()))
gh, gw = list(map(int, input().split()))
sh += 1
sw += 1
gh += 1
gw += 1
s = sh * 3000 + sw
g = gh * 3000 + gw
S = [0] * (H + 4)
S[0] = "*" * (W + 4)
S[1] = "*" * (W + 4)
S[-2] = "*" * (W + 4)
S[-1] = "*" * (W + 4)
for h in range(2, H + 2):
    S[h] = "**" + eval(input()) + "**"
visited = [[float("inf")] * (W + 4) for _ in range(H + 4)]
dh1 = [0, 1, 0, -1]
dw1 = [1, 0, -1, 0]
dh2 = [2, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, -1, -1, -1, -1, -2, -2, -2, -2, -2]
dw2 = [-2, -1, 0, 1, 2, -2, -1, 1, 2, -2, 2, -2, -1, 1, 2, -2, -1, 0, 1, 2]
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge

d = []
heapify(d)
heappush(d, s)
ans = float("inf")
while len(d) > 0:
    a = heappop(d)
    cnt, a1 = divmod(a, 10**7)
    h, w = divmod(a1, 3000)
    if cnt >= ans:
        break
    visited[h][w] = cnt
    for i in range(4):
        h1 = h + dh1[i]
        w1 = w + dw1[i]
        if (h1, w1) == (gh, gw):
            ans = cnt
        if S[h1][w1] == ".":
            if visited[h1][w1] > cnt:
                visited[h1][w1] = cnt
                heappush(d, cnt * (10**7) + h1 * 3000 + w1)
    for i in range(20):
        h2 = h + dh2[i]
        w2 = w + dw2[i]
        if (h2, w2) == (gh, gw):
            ans = cnt + 1
        if S[h2][w2] == ".":
            if visited[h2][w2] > cnt + 1:
                visited[h2][w2] = cnt + 1
                heappush(d, (cnt + 1) * (10**7) + h2 * 3000 + w2)
ans = ans if ans < float("inf") else -1
print(ans)

+s = sh * 3000 + sw +g = gh * 3000 + gw -heappush(d, (0, sh, sw)) +heappush(d, s) -    cnt, h, w = heappop(d) +    a = heappop(d) +    cnt, a1 = divmod(a, 10**7) +    h, w = divmod(a1, 3000) -                heappush(d, (cnt, h1, w1)) +                heappush(d, cnt * (10**7) + h1 * 3000 + w1) -                heappush(d, (cnt + 1, h2, w2)) +                heappush(d, (cnt + 1) * (10**7) + h2 * 3000 + w2)