from collections import deque



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

C = list(map(int, input().split()))

C = tuple([C[0]-1, C[1]-1])

D = tuple(map(int, input().split()))

D = tuple([D[0]-1, D[1]-1])

maze = [list(eval(input())) for _ in range(h)]



visit = [[False]*w for _ in range(h)]

for i in range(h):

    for j in range(w):

        if maze[i][j] == "#":

            visit[i][j] = True



DQ = deque([C])

DQ_warp = deque([])



dx = [1, 0, -1, 0]

dy = [0, 1, 0, -1]



ans = 0



while DQ or DQ_warp:

    while DQ:

        now = DQ.popleft()

        if now == D:

            print(ans)

            exit()

        x, y = now[0], now[1]

        if visit[x][y]:

            continue

        visit[x][y] = True

        for i in range(4):

            nx = x + dx[i]

            ny = y + dy[i]

            if 0<=nx<=h-1 and 0<=ny<=w-1 and visit[nx][ny]==False:

                DQ.append(tuple([nx, ny]))



        for i in range(-2, 3):

            for j in range(-2, 3):

                if i == j == 0: continue

                nx = x + i

                ny = y + j

                if 0<=nx<=h-1 and 0<=ny<=w-1 and visit[nx][ny]==False:

                    DQ_warp.append(tuple([nx, ny]))



    if DQ_warp:

        DQ = DQ_warp.copy()

        DQ_warp = deque([])

        ans += 1



print((-1))
from collections import deque



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

Ch, Cw = list(map(int, input().split()))

Dh, Dw = list(map(int, input().split()))

Ch, Cw, Dh, Dw = Ch-1, Cw-1, Dh-1, Dw-1



maze = [list(eval(input())) for _ in range(H)]

visit = [[-1] * W for _ in range(H)]

visit[Ch][Cw] = 0



DQ = deque([(Ch, Cw)])

dx = [1, 0, -1, 0]

dy = [0, 1, 0, -1]



while DQ:

    px, py = DQ.popleft()

    if (px, py) == (Dh, Dw):

        print((visit[px][py]))

        exit()



    for i in range(4):

        nx = px + dx[i]

        ny = py + dy[i]

        if 0<=nx<=H-1 and 0<=ny<=W-1 and maze[nx][ny]=="." and (visit[nx][ny]>visit[px][py] or visit[nx][ny]==-1):

            visit[nx][ny] = visit[px][py]

            DQ.appendleft((nx, ny))



    for i in range(-2, 3):

        for j in range(-2, 3):

            if i == j == 0:

                continue



            nx = px + i

            ny = py + j



            if 0<=nx<=H-1 and 0<=ny<=W-1 and maze[nx][ny]=="." and visit[nx][ny]==-1:

                visit[nx][ny] = visit[px][py] + 1

                DQ.append((nx, ny))



    # for a in visit:

    #     print(a)



print((-1))
from collections import deque

h, w = list(map(int, input().split()))
C = list(map(int, input().split()))
C = tuple([C[0] - 1, C[1] - 1])
D = tuple(map(int, input().split()))
D = tuple([D[0] - 1, D[1] - 1])
maze = [list(eval(input())) for _ in range(h)]
visit = [[False] * w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if maze[i][j] == "#":
            visit[i][j] = True
DQ = deque([C])
DQ_warp = deque([])
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
ans = 0
while DQ or DQ_warp:
    while DQ:
        now = DQ.popleft()
        if now == D:
            print(ans)
            exit()
        x, y = now[0], now[1]
        if visit[x][y]:
            continue
        visit[x][y] = True
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if 0 <= nx <= h - 1 and 0 <= ny <= w - 1 and visit[nx][ny] == False:
                DQ.append(tuple([nx, ny]))
        for i in range(-2, 3):
            for j in range(-2, 3):
                if i == j == 0:
                    continue
                nx = x + i
                ny = y + j
                if 0 <= nx <= h - 1 and 0 <= ny <= w - 1 and visit[nx][ny] == False:
                    DQ_warp.append(tuple([nx, ny]))
    if DQ_warp:
        DQ = DQ_warp.copy()
        DQ_warp = deque([])
        ans += 1
print((-1))

from collections import deque

H, W = list(map(int, input().split()))
Ch, Cw = list(map(int, input().split()))
Dh, Dw = list(map(int, input().split()))
Ch, Cw, Dh, Dw = Ch - 1, Cw - 1, Dh - 1, Dw - 1
maze = [list(eval(input())) for _ in range(H)]
visit = [[-1] * W for _ in range(H)]
visit[Ch][Cw] = 0
DQ = deque([(Ch, Cw)])
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
while DQ:
    px, py = DQ.popleft()
    if (px, py) == (Dh, Dw):
        print((visit[px][py]))
        exit()
    for i in range(4):
        nx = px + dx[i]
        ny = py + dy[i]
        if (
            0 <= nx <= H - 1
            and 0 <= ny <= W - 1
            and maze[nx][ny] == "."
            and (visit[nx][ny] > visit[px][py] or visit[nx][ny] == -1)
        ):
            visit[nx][ny] = visit[px][py]
            DQ.appendleft((nx, ny))
    for i in range(-2, 3):
        for j in range(-2, 3):
            if i == j == 0:
                continue
            nx = px + i
            ny = py + j
            if (
                0 <= nx <= H - 1
                and 0 <= ny <= W - 1
                and maze[nx][ny] == "."
                and visit[nx][ny] == -1
            ):
                visit[nx][ny] = visit[px][py] + 1
                DQ.append((nx, ny))
    # for a in visit:
    #     print(a)
print((-1))

-h, w = list(map(int, input().split())) -C = list(map(int, input().split())) -C = tuple([C[0] - 1, C[1] - 1]) -D = tuple(map(int, input().split())) -D = tuple([D[0] - 1, D[1] - 1]) -maze = [list(eval(input())) for _ in range(h)] -visit = [[False] * w for _ in range(h)] -for i in range(h): -    for j in range(w): -        if maze[i][j] == "#": -            visit[i][j] = True -DQ = deque([C]) -DQ_warp = deque([]) +H, W = list(map(int, input().split())) +Ch, Cw = list(map(int, input().split())) +Dh, Dw = list(map(int, input().split())) +Ch, Cw, Dh, Dw = Ch - 1, Cw - 1, Dh - 1, Dw - 1 +maze = [list(eval(input())) for _ in range(H)] +visit = [[-1] * W for _ in range(H)] +visit[Ch][Cw] = 0 +DQ = deque([(Ch, Cw)]) -ans = 0 -while DQ or DQ_warp: -    while DQ: -        now = DQ.popleft() -        if now == D: -            print(ans) -            exit() -        x, y = now[0], now[1] -        if visit[x][y]: -            continue -        visit[x][y] = True -        for i in range(4): -            nx = x + dx[i] -            ny = y + dy[i] -            if 0 <= nx <= h - 1 and 0 <= ny <= w - 1 and visit[nx][ny] == False: -                DQ.append(tuple([nx, ny])) -        for i in range(-2, 3): -            for j in range(-2, 3): -                if i == j == 0: -                    continue -                nx = x + i -                ny = y + j -                if 0 <= nx <= h - 1 and 0 <= ny <= w - 1 and visit[nx][ny] == False: -                    DQ_warp.append(tuple([nx, ny])) -    if DQ_warp: -        DQ = DQ_warp.copy() -        DQ_warp = deque([]) -        ans += 1 +while DQ: +    px, py = DQ.popleft() +    if (px, py) == (Dh, Dw): +        print((visit[px][py])) +        exit() +    for i in range(4): +        nx = px + dx[i] +        ny = py + dy[i] +        if ( +            0 <= nx <= H - 1 +            and 0 <= ny <= W - 1 +            and maze[nx][ny] == "." +            and (visit[nx][ny] > visit[px][py] or visit[nx][ny] == -1) +        ): +            visit[nx][ny] = visit[px][py] +            DQ.appendleft((nx, ny)) +    for i in range(-2, 3): +        for j in range(-2, 3): +            if i == j == 0: +                continue +            nx = px + i +            ny = py + j +            if ( +                0 <= nx <= H - 1 +                and 0 <= ny <= W - 1 +                and maze[nx][ny] == "." +                and visit[nx][ny] == -1 +            ): +                visit[nx][ny] = visit[px][py] + 1 +                DQ.append((nx, ny)) +    # for a in visit: +    #     print(a)