import sys

from collections import deque



read = sys.stdin.read

readline = sys.stdin.readline

readlines = sys.stdin.readlines

sys.setrecursionlimit(10 ** 9)

INF = 1 << 60

MOD = 1000000007



dxdy4 = ((-1, 0), (0, -1), (0, 1), (1, 0))





def main():

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

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

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

    S = [readline().strip() for _ in range(H)]



    Ch -= 1

    Cw -= 1

    Dh -= 1

    Dw -= 1



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

    dist[Ch][Cw] = 0

    seen = [[False] * W for _ in range(H)]

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

    while queue:

        x, y = queue.popleft()

        seen[x][y] = True

        for dx, dy in dxdy4:

            nx, ny = x + dx, y + dy

            if (

                0 <= nx < H

                and 0 <= ny < W

                and not seen[nx][ny]

                and (dist[nx][ny] == -1 or dist[nx][ny] > dist[x][y])

                and S[nx][ny] == '.'

            ):

                dist[nx][ny] = dist[x][y]

                queue.appendleft((nx, ny))

        for dx in range(-2, 3):

            for dy in range(-2, 3):

                nx, ny = x + dx, y + dy

                if (

                    0 <= nx < H

                    and 0 <= ny < W

                    and not seen[nx][ny]

                    and dist[nx][ny] > dist[x][y] + 1

                    and S[nx][ny] == '.'

                ):

                    dist[nx][ny] = dist[x][y] + 1

                    queue.append((nx, ny))



    if dist[Dh][Dw] == INF:

        print((-1))

    else:

        print((dist[Dh][Dw]))



    return





if __name__ == '__main__':

    main()

import sys

from collections import deque



read = sys.stdin.read

readline = sys.stdin.readline

readlines = sys.stdin.readlines

sys.setrecursionlimit(10 ** 9)

INF = 1 << 60

MOD = 1000000007



dxdy4 = ((-1, 0), (0, -1), (0, 1), (1, 0))





def main():

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

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

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

    S = [readline().strip() for _ in range(H)]



    Ch -= 1

    Cw -= 1

    Dh -= 1

    Dw -= 1



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

    dist[Ch][Cw] = 0

    seen = [[False] * W for _ in range(H)]

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

    while queue:

        x, y = queue.popleft()

        if seen[x][y]:

            continue

        seen[x][y] = True

        for dx, dy in dxdy4:

            nx, ny = x + dx, y + dy

            if (

                0 <= nx < H

                and 0 <= ny < W

                and not seen[nx][ny]

                and (dist[nx][ny] == -1 or dist[nx][ny] > dist[x][y])

                and S[nx][ny] == '.'

            ):

                dist[nx][ny] = dist[x][y]

                queue.appendleft((nx, ny))

        for dx in range(-2, 3):

            for dy in range(-2, 3):

                nx, ny = x + dx, y + dy

                if (

                    0 <= nx < H

                    and 0 <= ny < W

                    and not seen[nx][ny]

                    and dist[nx][ny] > dist[x][y] + 1

                    and S[nx][ny] == '.'

                ):

                    dist[nx][ny] = dist[x][y] + 1

                    queue.append((nx, ny))



    if dist[Dh][Dw] == INF:

        print((-1))

    else:

        print((dist[Dh][Dw]))



    return





if __name__ == '__main__':

    main()

import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10**9)
INF = 1 << 60
MOD = 1000000007
dxdy4 = ((-1, 0), (0, -1), (0, 1), (1, 0))


def main():
    H, W = list(map(int, readline().split()))
    Ch, Cw = list(map(int, readline().split()))
    Dh, Dw = list(map(int, readline().split()))
    S = [readline().strip() for _ in range(H)]
    Ch -= 1
    Cw -= 1
    Dh -= 1
    Dw -= 1
    dist = [[INF] * W for _ in range(H)]
    dist[Ch][Cw] = 0
    seen = [[False] * W for _ in range(H)]
    queue = deque([(Ch, Cw)])
    while queue:
        x, y = queue.popleft()
        seen[x][y] = True
        for dx, dy in dxdy4:
            nx, ny = x + dx, y + dy
            if (
                0 <= nx < H
                and 0 <= ny < W
                and not seen[nx][ny]
                and (dist[nx][ny] == -1 or dist[nx][ny] > dist[x][y])
                and S[nx][ny] == "."
            ):
                dist[nx][ny] = dist[x][y]
                queue.appendleft((nx, ny))
        for dx in range(-2, 3):
            for dy in range(-2, 3):
                nx, ny = x + dx, y + dy
                if (
                    0 <= nx < H
                    and 0 <= ny < W
                    and not seen[nx][ny]
                    and dist[nx][ny] > dist[x][y] + 1
                    and S[nx][ny] == "."
                ):
                    dist[nx][ny] = dist[x][y] + 1
                    queue.append((nx, ny))
    if dist[Dh][Dw] == INF:
        print((-1))
    else:
        print((dist[Dh][Dw]))
    return


if __name__ == "__main__":
    main()

import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10**9)
INF = 1 << 60
MOD = 1000000007
dxdy4 = ((-1, 0), (0, -1), (0, 1), (1, 0))


def main():
    H, W = list(map(int, readline().split()))
    Ch, Cw = list(map(int, readline().split()))
    Dh, Dw = list(map(int, readline().split()))
    S = [readline().strip() for _ in range(H)]
    Ch -= 1
    Cw -= 1
    Dh -= 1
    Dw -= 1
    dist = [[INF] * W for _ in range(H)]
    dist[Ch][Cw] = 0
    seen = [[False] * W for _ in range(H)]
    queue = deque([(Ch, Cw)])
    while queue:
        x, y = queue.popleft()
        if seen[x][y]:
            continue
        seen[x][y] = True
        for dx, dy in dxdy4:
            nx, ny = x + dx, y + dy
            if (
                0 <= nx < H
                and 0 <= ny < W
                and not seen[nx][ny]
                and (dist[nx][ny] == -1 or dist[nx][ny] > dist[x][y])
                and S[nx][ny] == "."
            ):
                dist[nx][ny] = dist[x][y]
                queue.appendleft((nx, ny))
        for dx in range(-2, 3):
            for dy in range(-2, 3):
                nx, ny = x + dx, y + dy
                if (
                    0 <= nx < H
                    and 0 <= ny < W
                    and not seen[nx][ny]
                    and dist[nx][ny] > dist[x][y] + 1
                    and S[nx][ny] == "."
                ):
                    dist[nx][ny] = dist[x][y] + 1
                    queue.append((nx, ny))
    if dist[Dh][Dw] == INF:
        print((-1))
    else:
        print((dist[Dh][Dw]))
    return


if __name__ == "__main__":
    main()

+        if seen[x][y]: +            continue