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

ch, cw = list(map(int, input().split()))

ch -= 1

cw -= 1

dh, dw = list(map(int, input().split()))

dh -= 1

dw -= 1

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

dist = [[float("inf")] * W for _ in range(H)]



# x = []

# for i in range(5):

#     for j in range(5):

#         if i == 3 and j == 3:

#             continue

#         x.append((i-2, j-2))

# x = set(x)

y = {(0, 1), (1, 0), (-1, 0), (0, -1)}

# x = x - y



from collections import deque

q = deque()

q.append((ch, cw))

dist[ch][cw] = 0

while q:

    h, w = q.popleft()

    for hh, ww in y:

        if 0 <= h+hh < H and 0 <= w+ww < W and S[h+hh][w+ww] == "." and dist[h+hh][w+ww] > dist[h][w]:

            dist[h+hh][w+ww] = dist[h][w]

            q.appendleft((h+hh, w+ww))

    for hh in range(-2, 3):

        for ww in range(-2, 3):

            if 0 <= h+hh < H and 0 <= w+ww < W and S[h+hh][w+ww] == "." and dist[h+hh][w+ww] > dist[h][w] + 1:

                dist[h+hh][w+ww] = dist[h][w] + 1

                q.append((h+hh, w+ww))



if dist[dh][dw] == float("inf"):

    print((-1))

else:

    print((dist[dh][dw]))

from collections import deque



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

ch, cw = [int(x)-1 for x in input().split()]

dh, dw = [int(x)-1 for x in input().split()]

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

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



move = []

for i in range(-2, 3):

    for j in range(-2, 3):

        if i == 0 and j == 0:

            continue

        move.append((i, j))

q = deque()

q.append((0, ch, cw))

while q:

    c, h, w = q.popleft()

    if dist[h][w] >= 0:

        continue

    dist[h][w] = c

    for hh, ww in move:

        if 0 <= h+hh < H and 0 <= w+ww < W and S[h+hh][w+ww] == "." and dist[h+hh][w+ww] < 0:

            if (hh, ww) in ((0,1), (1,0), (-1,0), (0,-1)):

                q.appendleft((c, h+hh, w+ww)) # 徒歩は前に追加

            else:

                q.append((c+1, h+hh, w+ww)) # ワープは後ろに追加



print((dist[dh][dw]))
H, W = list(map(int, input().split()))
ch, cw = list(map(int, input().split()))
ch -= 1
cw -= 1
dh, dw = list(map(int, input().split()))
dh -= 1
dw -= 1
S = [list(eval(input())) for _ in range(H)]
dist = [[float("inf")] * W for _ in range(H)]
# x = []
# for i in range(5):
#     for j in range(5):
#         if i == 3 and j == 3:
#             continue
#         x.append((i-2, j-2))
# x = set(x)
y = {(0, 1), (1, 0), (-1, 0), (0, -1)}
# x = x - y
from collections import deque

q = deque()
q.append((ch, cw))
dist[ch][cw] = 0
while q:
    h, w = q.popleft()
    for hh, ww in y:
        if (
            0 <= h + hh < H
            and 0 <= w + ww < W
            and S[h + hh][w + ww] == "."
            and dist[h + hh][w + ww] > dist[h][w]
        ):
            dist[h + hh][w + ww] = dist[h][w]
            q.appendleft((h + hh, w + ww))
    for hh in range(-2, 3):
        for ww in range(-2, 3):
            if (
                0 <= h + hh < H
                and 0 <= w + ww < W
                and S[h + hh][w + ww] == "."
                and dist[h + hh][w + ww] > dist[h][w] + 1
            ):
                dist[h + hh][w + ww] = dist[h][w] + 1
                q.append((h + hh, w + ww))
if dist[dh][dw] == float("inf"):
    print((-1))
else:
    print((dist[dh][dw]))

from collections import deque

H, W = list(map(int, input().split()))
ch, cw = [int(x) - 1 for x in input().split()]
dh, dw = [int(x) - 1 for x in input().split()]
S = [list(eval(input())) for _ in range(H)]
dist = [[-1] * W for _ in range(H)]
move = []
for i in range(-2, 3):
    for j in range(-2, 3):
        if i == 0 and j == 0:
            continue
        move.append((i, j))
q = deque()
q.append((0, ch, cw))
while q:
    c, h, w = q.popleft()
    if dist[h][w] >= 0:
        continue
    dist[h][w] = c
    for hh, ww in move:
        if (
            0 <= h + hh < H
            and 0 <= w + ww < W
            and S[h + hh][w + ww] == "."
            and dist[h + hh][w + ww] < 0
        ):
            if (hh, ww) in ((0, 1), (1, 0), (-1, 0), (0, -1)):
                q.appendleft((c, h + hh, w + ww))  # 徒歩は前に追加
            else:
                q.append((c + 1, h + hh, w + ww))  # ワープは後ろに追加
print((dist[dh][dw]))

-H, W = list(map(int, input().split())) -ch, cw = list(map(int, input().split())) -ch -= 1 -cw -= 1 -dh, dw = list(map(int, input().split())) -dh -= 1 -dw -= 1 -S = [list(eval(input())) for _ in range(H)] -dist = [[float("inf")] * W for _ in range(H)] -# x = [] -# for i in range(5): -#     for j in range(5): -#         if i == 3 and j == 3: -#             continue -#         x.append((i-2, j-2)) -# x = set(x) -y = {(0, 1), (1, 0), (-1, 0), (0, -1)} -# x = x - y +H, W = list(map(int, input().split())) +ch, cw = [int(x) - 1 for x in input().split()] +dh, dw = [int(x) - 1 for x in input().split()] +S = [list(eval(input())) for _ in range(H)] +dist = [[-1] * W for _ in range(H)] +move = [] +for i in range(-2, 3): +    for j in range(-2, 3): +        if i == 0 and j == 0: +            continue +        move.append((i, j)) -q.append((ch, cw)) -dist[ch][cw] = 0 +q.append((0, ch, cw)) -    h, w = q.popleft() -    for hh, ww in y: +    c, h, w = q.popleft() +    if dist[h][w] >= 0: +        continue +    dist[h][w] = c +    for hh, ww in move: -            and dist[h + hh][w + ww] > dist[h][w] +            and dist[h + hh][w + ww] < 0 -            dist[h + hh][w + ww] = dist[h][w] -            q.appendleft((h + hh, w + ww)) -    for hh in range(-2, 3): -        for ww in range(-2, 3): -            if ( -                0 <= h + hh < H -                and 0 <= w + ww < W -                and S[h + hh][w + ww] == "." -                and dist[h + hh][w + ww] > dist[h][w] + 1 -            ): -                dist[h + hh][w + ww] = dist[h][w] + 1 -                q.append((h + hh, w + ww)) -if dist[dh][dw] == float("inf"): -    print((-1)) -else: -    print((dist[dh][dw])) +            if (hh, ww) in ((0, 1), (1, 0), (-1, 0), (0, -1)): +                q.appendleft((c, h + hh, w + ww))  # 徒歩は前に追加 +            else: +                q.append((c + 1, h + hh, w + ww))  # ワープは後ろに追加 +print((dist[dh][dw]))