from collections import deque

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

CY, CX = [int(x)-1 for x in input().split()]  # 魔法使いの初期値

DY, DX = [int(x)-1 for x in input().split()]  # 目的地

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

# print(gridgraph)



q = deque()

q.append((CY, CX))  # スタート地点

# 各頂点について2*2の幅優先する？

adjacent = [[1, 0], [0, 1], [-1, 0], [0, -1]]  # 上下左右

distant = [[10**10]*W for _ in range(H)]

distant[CY][CX] = 0

warp_y = list(range(-2, 3, 1))

warp_x = list(range(-2, 3, 1))

while len(q):

    cy, cx = q.popleft()  # 現在位置

    for dy, dx in adjacent:

        # 新しい位置

        ny = cy+dy

        nx = cx+dx

        if ny < 0 or ny >= H or nx < 0 or nx >= W:

            continue

        # 道の場合

        if gridgraph[ny][nx] == "." and distant[ny][nx] > distant[cy][cx]:

            # 道の場合はコスト0で移動できる

            distant[ny][nx] = distant[cy][cx]

            q.appendleft((ny, nx))

    # 周囲5*5の道に+1で到達する

    for i in warp_y:

        for j in warp_x:

            target_y, target_x = cy+i, cx+j

            if target_y < 0 or target_y >= H or target_x < 0 or target_x >= W:

                continue

            else:

                if gridgraph[target_y][target_x] == "." and distant[target_y][target_x] > distant[cy][cx]+1:

                    distant[target_y][target_x] = distant[cy][cx]+1

                    q.append((target_y, target_x))



if distant[DY][DX] == 10**10:

    print((-1))

else:

    print((distant[DY][DX]))

from collections import deque

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

CY, CX = [int(x)-1 for x in input().split()]

DY, DX = [int(x)-1 for x in input().split()]



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

q = deque()

q.append([CY, CX])

# 更新されるなら追加

adjacent = [[1, 0], [0, 1], [-1, 0], [0, -1]]

INF = float("inf")

distant = [[INF]*W for _ in range(H)]

distant[CY][CX] = 0

while len(q):

    cy, cx = q.popleft()

    for dy, dx in adjacent:

        ny, nx = cy+dy, cx+dx

        if ny < 0 or ny >= H or nx < 0 or nx >= W:

            continue

        if gridgraph[ny][nx] == "." and distant[ny][nx] > distant[cy][cx]:

            distant[ny][nx] = distant[cy][cx]

            q.appendleft([ny, nx])

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

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

            target_y, target_x = cy+i, cx+j

            if target_y < 0 or target_y >= H or target_x < 0 or target_x >= W:

                continue

            if gridgraph[target_y][target_x] == "." and distant[target_y][target_x] > distant[cy][cx]+1:

                distant[target_y][target_x] = distant[cy][cx]+1

                q.append([target_y, target_x])

if distant[DY][DX] == INF:

    print((-1))

else:

    print((distant[DY][DX]))

from collections import deque

H, W = list(map(int, input().split()))  # H行, W列
CY, CX = [int(x) - 1 for x in input().split()]  # 魔法使いの初期値
DY, DX = [int(x) - 1 for x in input().split()]  # 目的地
gridgraph = [eval(input()) for _ in range(H)]
# print(gridgraph)
q = deque()
q.append((CY, CX))  # スタート地点
# 各頂点について2*2の幅優先する？
adjacent = [[1, 0], [0, 1], [-1, 0], [0, -1]]  # 上下左右
distant = [[10**10] * W for _ in range(H)]
distant[CY][CX] = 0
warp_y = list(range(-2, 3, 1))
warp_x = list(range(-2, 3, 1))
while len(q):
    cy, cx = q.popleft()  # 現在位置
    for dy, dx in adjacent:
        # 新しい位置
        ny = cy + dy
        nx = cx + dx
        if ny < 0 or ny >= H or nx < 0 or nx >= W:
            continue
        # 道の場合
        if gridgraph[ny][nx] == "." and distant[ny][nx] > distant[cy][cx]:
            # 道の場合はコスト0で移動できる
            distant[ny][nx] = distant[cy][cx]
            q.appendleft((ny, nx))
    # 周囲5*5の道に+1で到達する
    for i in warp_y:
        for j in warp_x:
            target_y, target_x = cy + i, cx + j
            if target_y < 0 or target_y >= H or target_x < 0 or target_x >= W:
                continue
            else:
                if (
                    gridgraph[target_y][target_x] == "."
                    and distant[target_y][target_x] > distant[cy][cx] + 1
                ):
                    distant[target_y][target_x] = distant[cy][cx] + 1
                    q.append((target_y, target_x))
if distant[DY][DX] == 10**10:
    print((-1))
else:
    print((distant[DY][DX]))

from collections import deque

H, W = list(map(int, input().split()))
CY, CX = [int(x) - 1 for x in input().split()]
DY, DX = [int(x) - 1 for x in input().split()]
gridgraph = [eval(input()) for _ in range(H)]
q = deque()
q.append([CY, CX])
# 更新されるなら追加
adjacent = [[1, 0], [0, 1], [-1, 0], [0, -1]]
INF = float("inf")
distant = [[INF] * W for _ in range(H)]
distant[CY][CX] = 0
while len(q):
    cy, cx = q.popleft()
    for dy, dx in adjacent:
        ny, nx = cy + dy, cx + dx
        if ny < 0 or ny >= H or nx < 0 or nx >= W:
            continue
        if gridgraph[ny][nx] == "." and distant[ny][nx] > distant[cy][cx]:
            distant[ny][nx] = distant[cy][cx]
            q.appendleft([ny, nx])
    for i in range(-2, 3, 1):
        for j in range(-2, 3, 1):
            target_y, target_x = cy + i, cx + j
            if target_y < 0 or target_y >= H or target_x < 0 or target_x >= W:
                continue
            if (
                gridgraph[target_y][target_x] == "."
                and distant[target_y][target_x] > distant[cy][cx] + 1
            ):
                distant[target_y][target_x] = distant[cy][cx] + 1
                q.append([target_y, target_x])
if distant[DY][DX] == INF:
    print((-1))
else:
    print((distant[DY][DX]))

-H, W = list(map(int, input().split()))  # H行, W列 -CY, CX = [int(x) - 1 for x in input().split()]  # 魔法使いの初期値 -DY, DX = [int(x) - 1 for x in input().split()]  # 目的地 +H, W = list(map(int, input().split())) +CY, CX = [int(x) - 1 for x in input().split()] +DY, DX = [int(x) - 1 for x in input().split()] -# print(gridgraph) -q.append((CY, CX))  # スタート地点 -# 各頂点について2*2の幅優先する？ -adjacent = [[1, 0], [0, 1], [-1, 0], [0, -1]]  # 上下左右 -distant = [[10**10] * W for _ in range(H)] +q.append([CY, CX]) +# 更新されるなら追加 +adjacent = [[1, 0], [0, 1], [-1, 0], [0, -1]] +INF = float("inf") +distant = [[INF] * W for _ in range(H)] -warp_y = list(range(-2, 3, 1)) -warp_x = list(range(-2, 3, 1)) -    cy, cx = q.popleft()  # 現在位置 +    cy, cx = q.popleft() -        # 新しい位置 -        ny = cy + dy -        nx = cx + dx +        ny, nx = cy + dy, cx + dx -        # 道の場合 -            # 道の場合はコスト0で移動できる -            q.appendleft((ny, nx)) -    # 周囲5*5の道に+1で到達する -    for i in warp_y: -        for j in warp_x: +            q.appendleft([ny, nx]) +    for i in range(-2, 3, 1): +        for j in range(-2, 3, 1): -            else: -                if ( -                    gridgraph[target_y][target_x] == "." -                    and distant[target_y][target_x] > distant[cy][cx] + 1 -                ): -                    distant[target_y][target_x] = distant[cy][cx] + 1 -                    q.append((target_y, target_x)) -if distant[DY][DX] == 10**10: +            if ( +                gridgraph[target_y][target_x] == "." +                and distant[target_y][target_x] > distant[cy][cx] + 1 +            ): +                distant[target_y][target_x] = distant[cy][cx] + 1 +                q.append([target_y, target_x]) +if distant[DY][DX] == INF: