#!/usr/bin/env python3



import collections

import sys

input=sys.stdin.readline



h,w=list(map(int,input().split()))

sy,sx=list(map(int,input().split()))

gy,gx=list(map(int,input().split()))

board=[['#']*(w+2)]+[list('#'+input().replace('\n','')+'#') for _ in range(h)]+[['#']*(w+2)]

scores=[[10**18]*(w+2) for _ in range(h+2)]

scores[sy][sx]=0

q=collections.deque()

q.append((sy,sx))

count=0

while len(q)!=0:

  y,x=q.popleft()

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

    if 1<=x+dx<=w and 1<=y+dy<=h and board[y+dy][x+dx]!='#' and scores[y+dy][x+dx]>scores[y][x]:

      scores[y+dy][x+dx]=scores[y][x]

      q.appendleft((y+dy,x+dx))

  for dy in range(-2,3):

    for dx in range(-2,3):

      if dx==0 and dy==0:

        continue

      if 1<=x+dx<=w and 1<=y+dy<=h and board[y+dy][x+dx]!='#' and scores[y+dy][x+dx]>scores[y][x]+1:

        scores[y+dy][x+dx]=scores[y][x]+1

        q.append((y+dy,x+dx))

if scores[gy][gx]==10**18:

    print((-1))

else:

    print((scores[gy][gx]))
#!/usr/bin/env python3



import collections



h,w=list(map(int,input().split()))

sy,sx=list(map(int,input().split()))

gy,gx=list(map(int,input().split()))

board=[['#']*(w+2)]+[list('#'+eval(input())+'#') for _ in range(h)]+[['#']*(w+2)]

checked=[[False]*(w+1) for _ in range(h+1)]

checked[sy][sx]=True

q=collections.deque()

q.append((sy,sx))

ans=0

while 1:

  cur_pos=[]

  while len(q)!=0:

    y,x=q.popleft()

    cur_pos.append((y,x))

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

      if 1<=y+dy<=h and 1<=x+dx<=w and board[y+dy][x+dx]=='.' and checked[y+dy][x+dx]==False:

        checked[y+dy][x+dx]=True

        q.append((y+dy,x+dx))

  if checked[gy][gx]==True:

    print(ans)

    exit()

  if len(cur_pos)==0:

    print((-1))

    exit()

  ans+=1

  for y,x in cur_pos:

    for dy in range(-2,3):

      for dx in range(-2,3):

        if dy==0 and dx==0:

          continue

        if 1<=y+dy<=h and 1<=x+dx<=w and board[y+dy][x+dx]=='.' and checked[y+dy][x+dx]==False:

          checked[y+dy][x+dx]=True

          q.append((y+dy,x+dx))
#!/usr/bin/env python3
import collections
import sys

input = sys.stdin.readline
h, w = list(map(int, input().split()))
sy, sx = list(map(int, input().split()))
gy, gx = list(map(int, input().split()))
board = (
    [["#"] * (w + 2)]
    + [list("#" + input().replace("\n", "") + "#") for _ in range(h)]
    + [["#"] * (w + 2)]
)
scores = [[10**18] * (w + 2) for _ in range(h + 2)]
scores[sy][sx] = 0
q = collections.deque()
q.append((sy, sx))
count = 0
while len(q) != 0:
    y, x = q.popleft()
    for dx, dy in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
        if (
            1 <= x + dx <= w
            and 1 <= y + dy <= h
            and board[y + dy][x + dx] != "#"
            and scores[y + dy][x + dx] > scores[y][x]
        ):
            scores[y + dy][x + dx] = scores[y][x]
            q.appendleft((y + dy, x + dx))
    for dy in range(-2, 3):
        for dx in range(-2, 3):
            if dx == 0 and dy == 0:
                continue
            if (
                1 <= x + dx <= w
                and 1 <= y + dy <= h
                and board[y + dy][x + dx] != "#"
                and scores[y + dy][x + dx] > scores[y][x] + 1
            ):
                scores[y + dy][x + dx] = scores[y][x] + 1
                q.append((y + dy, x + dx))
if scores[gy][gx] == 10**18:
    print((-1))
else:
    print((scores[gy][gx]))

#!/usr/bin/env python3
import collections

h, w = list(map(int, input().split()))
sy, sx = list(map(int, input().split()))
gy, gx = list(map(int, input().split()))
board = (
    [["#"] * (w + 2)]
    + [list("#" + eval(input()) + "#") for _ in range(h)]
    + [["#"] * (w + 2)]
)
checked = [[False] * (w + 1) for _ in range(h + 1)]
checked[sy][sx] = True
q = collections.deque()
q.append((sy, sx))
ans = 0
while 1:
    cur_pos = []
    while len(q) != 0:
        y, x = q.popleft()
        cur_pos.append((y, x))
        for dy, dx in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
            if (
                1 <= y + dy <= h
                and 1 <= x + dx <= w
                and board[y + dy][x + dx] == "."
                and checked[y + dy][x + dx] == False
            ):
                checked[y + dy][x + dx] = True
                q.append((y + dy, x + dx))
    if checked[gy][gx] == True:
        print(ans)
        exit()
    if len(cur_pos) == 0:
        print((-1))
        exit()
    ans += 1
    for y, x in cur_pos:
        for dy in range(-2, 3):
            for dx in range(-2, 3):
                if dy == 0 and dx == 0:
                    continue
                if (
                    1 <= y + dy <= h
                    and 1 <= x + dx <= w
                    and board[y + dy][x + dx] == "."
                    and checked[y + dy][x + dx] == False
                ):
                    checked[y + dy][x + dx] = True
                    q.append((y + dy, x + dx))

-import sys -input = sys.stdin.readline -    + [list("#" + input().replace("\n", "") + "#") for _ in range(h)] +    + [list("#" + eval(input()) + "#") for _ in range(h)] -scores = [[10**18] * (w + 2) for _ in range(h + 2)] -scores[sy][sx] = 0 +checked = [[False] * (w + 1) for _ in range(h + 1)] +checked[sy][sx] = True -count = 0 -while len(q) != 0: -    y, x = q.popleft() -    for dx, dy in [[-1, 0], [1, 0], [0, -1], [0, 1]]: -        if ( -            1 <= x + dx <= w -            and 1 <= y + dy <= h -            and board[y + dy][x + dx] != "#" -            and scores[y + dy][x + dx] > scores[y][x] -        ): -            scores[y + dy][x + dx] = scores[y][x] -            q.appendleft((y + dy, x + dx)) -    for dy in range(-2, 3): -        for dx in range(-2, 3): -            if dx == 0 and dy == 0: -                continue +ans = 0 +while 1: +    cur_pos = [] +    while len(q) != 0: +        y, x = q.popleft() +        cur_pos.append((y, x)) +        for dy, dx in [[-1, 0], [1, 0], [0, -1], [0, 1]]: -                1 <= x + dx <= w -                and 1 <= y + dy <= h -                and board[y + dy][x + dx] != "#" -                and scores[y + dy][x + dx] > scores[y][x] + 1 +                1 <= y + dy <= h +                and 1 <= x + dx <= w +                and board[y + dy][x + dx] == "." +                and checked[y + dy][x + dx] == False -                scores[y + dy][x + dx] = scores[y][x] + 1 +                checked[y + dy][x + dx] = True -if scores[gy][gx] == 10**18: -    print((-1)) -else: -    print((scores[gy][gx])) +    if checked[gy][gx] == True: +        print(ans) +        exit() +    if len(cur_pos) == 0: +        print((-1)) +        exit() +    ans += 1 +    for y, x in cur_pos: +        for dy in range(-2, 3): +            for dx in range(-2, 3): +                if dy == 0 and dx == 0: +                    continue +                if ( +                    1 <= y + dy <= h +                    and 1 <= x + dx <= w +                    and board[y + dy][x + dx] == "." +                    and checked[y + dy][x + dx] == False +                ): +                    checked[y + dy][x + dx] = True +                    q.append((y + dy, x + dx))