from collections import deque



di = [-1,0,1,0]

dj = [0,-1,0,1]



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

si,sj = list(map(int, input().split()))

ti,tj = list(map(int, input().split()))

si -= 1

sj -= 1

ti -= 1

tj -=1

s = list()

for i in range(H):

  s.append(eval(input()))



inf = 10**9

dist = [[inf] * W for i in range(H)]

q = deque()

q.appendleft([si,sj])

dist[si][sj] = 0

while(len(q) > 0):

  tmp = q.popleft()

  i = tmp[0]

  j = tmp[1]

  d = dist[i][j]

  for v in range(4):

    ni = i + di[v]

    nj = j + dj[v]

    if (ni < 0) or (ni >= H) or (nj < 0) or (nj >= W):

      continue

    if s[ni][nj] == "#":

      continue

    if dist[ni][nj] <= d:

      continue

    dist[ni][nj] = d

    q.appendleft([ni,nj])

  for ei in range(-2, 3, 1):

    for ej in range(-2, 3, 1):

      ni = i + ei

      nj = j + ej

      if (ni < 0) or (ni >= H) or (nj < 0) or (nj >= W):

        continue

      if dist[ni][nj] <= d+1:

        continue

      if s[ni][nj] == "#":

        continue

      dist[ni][nj] = d+1

      q.append([ni,nj])



ans = dist[ti][tj]

if ans == inf:

  ans = -1

print(ans)
from collections import deque



di = [-1,0,1,0]

dj = [0,-1,0,1]



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

si,sj = list(map(int, input().split()))

ti,tj = list(map(int, input().split()))

si -= 1

sj -= 1

ti -= 1

tj -= 1

s = list()

for i in range(H):

  s.append(eval(input()))



inf = 10**9

dist = [[inf] * W for i in range(H)]

q = deque()

q.appendleft([si,sj])

dist[si][sj] = 0

while(len(q) > 0):

  tmp = q.popleft()

  i = tmp[0]

  j = tmp[1]

  d = dist[i][j]

  for v in range(4):

    ni = i + di[v]

    nj = j + dj[v]

    if (ni < 0) or (ni >= H) or (nj < 0) or (nj >= W):

      continue

    if s[ni][nj] == "#":

      continue

    if dist[ni][nj] <= d:

      continue

    q.appendleft([ni,nj])

    dist[ni][nj] = d

    

  for ei in range(-2,3,1):

    for ej in range(-2,3,1):

      ni = i + ei

      nj = j + ej

      if (ni < 0) or (ni >= H) or (nj < 0) or (nj >= W):

        continue

      if s[ni][nj] == "#":

        continue

      if dist[ni][nj] <= d+1:

        continue

      q.append([ni,nj])

      dist[ni][nj] = d+1

      

ans = dist[ti][tj]

if ans == inf:

  ans = -1

print(ans)
from collections import deque

di = [-1, 0, 1, 0]
dj = [0, -1, 0, 1]
H, W = list(map(int, input().split()))
si, sj = list(map(int, input().split()))
ti, tj = list(map(int, input().split()))
si -= 1
sj -= 1
ti -= 1
tj -= 1
s = list()
for i in range(H):
    s.append(eval(input()))
inf = 10**9
dist = [[inf] * W for i in range(H)]
q = deque()
q.appendleft([si, sj])
dist[si][sj] = 0
while len(q) > 0:
    tmp = q.popleft()
    i = tmp[0]
    j = tmp[1]
    d = dist[i][j]
    for v in range(4):
        ni = i + di[v]
        nj = j + dj[v]
        if (ni < 0) or (ni >= H) or (nj < 0) or (nj >= W):
            continue
        if s[ni][nj] == "#":
            continue
        if dist[ni][nj] <= d:
            continue
        dist[ni][nj] = d
        q.appendleft([ni, nj])
    for ei in range(-2, 3, 1):
        for ej in range(-2, 3, 1):
            ni = i + ei
            nj = j + ej
            if (ni < 0) or (ni >= H) or (nj < 0) or (nj >= W):
                continue
            if dist[ni][nj] <= d + 1:
                continue
            if s[ni][nj] == "#":
                continue
            dist[ni][nj] = d + 1
            q.append([ni, nj])
ans = dist[ti][tj]
if ans == inf:
    ans = -1
print(ans)

from collections import deque

di = [-1, 0, 1, 0]
dj = [0, -1, 0, 1]
H, W = list(map(int, input().split()))
si, sj = list(map(int, input().split()))
ti, tj = list(map(int, input().split()))
si -= 1
sj -= 1
ti -= 1
tj -= 1
s = list()
for i in range(H):
    s.append(eval(input()))
inf = 10**9
dist = [[inf] * W for i in range(H)]
q = deque()
q.appendleft([si, sj])
dist[si][sj] = 0
while len(q) > 0:
    tmp = q.popleft()
    i = tmp[0]
    j = tmp[1]
    d = dist[i][j]
    for v in range(4):
        ni = i + di[v]
        nj = j + dj[v]
        if (ni < 0) or (ni >= H) or (nj < 0) or (nj >= W):
            continue
        if s[ni][nj] == "#":
            continue
        if dist[ni][nj] <= d:
            continue
        q.appendleft([ni, nj])
        dist[ni][nj] = d
    for ei in range(-2, 3, 1):
        for ej in range(-2, 3, 1):
            ni = i + ei
            nj = j + ej
            if (ni < 0) or (ni >= H) or (nj < 0) or (nj >= W):
                continue
            if s[ni][nj] == "#":
                continue
            if dist[ni][nj] <= d + 1:
                continue
            q.append([ni, nj])
            dist[ni][nj] = d + 1
ans = dist[ti][tj]
if ans == inf:
    ans = -1
print(ans)

+        q.appendleft([ni, nj]) -        q.appendleft([ni, nj]) +            if s[ni][nj] == "#": +                continue -            if s[ni][nj] == "#": -                continue +            q.append([ni, nj]) -            q.append([ni, nj])