from collections import deque

INF = int(1e10)



def main():

    from builtins import int, map

    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 = [eval(input()) for _ in range(H)]



    # dist = defaultdict(lambda: float("inf"))

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

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



    while queue:

        (h, w, d) = queue.popleft()



        if dist[h][w] == float("inf") or dist[h][w] > d:

            dist[h][w] = d

            

            # 5x5-grid

            for ddh in range(-2, 3):

                for ddw in range(-2, 3):

                    nh, nw = h + ddh, w + ddw

                    if 0 <= nh < H and 0 <= nw < W and S[nh][nw] == '.':

                        # cost-0 move

                        if abs(ddh) + abs(ddw) <= 1:

                            queue.appendleft((nh, nw, d))

                        # cost-1 move

                        else:

                            queue.append((nh, nw, d + 1))



    if dist[Dh][Dw] == float("inf"):

        print((-1))

    else:

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



if __name__ == '__main__':

    main()

from collections import deque



INF = int(1e10)



dh = [0, 0, 1, -1]

dw = [1, -1, 0, 0]



def main():

    from builtins import int, map

    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 = [[INF] * W for _ in range(H)]

    dist[Ch][Cw] = 0

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



    while queue:

        (h, w) = queue.popleft()



        for i in range(4):

            dhi, dwi = dh[i], dw[i]

            nh, nw = h + dhi, w + dwi

            if 0 <= nh < H and 0 <= nw < W and S[nh][nw] == '.':

                if dist[h][w] < dist[nh][nw]:

                    dist[nh][nw] = dist[h][w]

                    queue.appendleft((nh, nw))

        

        # 5x5-grid

        for ddh in range(-2, 3):

            for ddw in range(-2, 3):

                if abs(ddh) + abs(ddw) <= 1:

                    continue

                nh, nw = h + ddh, w + ddw

                if 0 <= nh < H and 0 <= nw < W and S[nh][nw] == '.':

                    if dist[nh][nw] > dist[h][w] + 1:

                        dist[nh][nw] = dist[h][w] + 1

                        queue.append((nh, nw))



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

        print((-1))

    else:

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



if __name__ == '__main__':

    main()

from collections import deque

INF = int(1e10)


def main():
    from builtins import int, map

    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 = [eval(input()) for _ in range(H)]
    # dist = defaultdict(lambda: float("inf"))
    dist = [[float("inf")] * W for _ in range(H)]
    queue = deque([(Ch, Cw, 0)])
    while queue:
        (h, w, d) = queue.popleft()
        if dist[h][w] == float("inf") or dist[h][w] > d:
            dist[h][w] = d
            # 5x5-grid
            for ddh in range(-2, 3):
                for ddw in range(-2, 3):
                    nh, nw = h + ddh, w + ddw
                    if 0 <= nh < H and 0 <= nw < W and S[nh][nw] == ".":
                        # cost-0 move
                        if abs(ddh) + abs(ddw) <= 1:
                            queue.appendleft((nh, nw, d))
                        # cost-1 move
                        else:
                            queue.append((nh, nw, d + 1))
    if dist[Dh][Dw] == float("inf"):
        print((-1))
    else:
        print((dist[Dh][Dw]))


if __name__ == "__main__":
    main()

from collections import deque

INF = int(1e10)
dh = [0, 0, 1, -1]
dw = [1, -1, 0, 0]


def main():
    from builtins import int, map

    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 = [[INF] * W for _ in range(H)]
    dist[Ch][Cw] = 0
    queue = deque([(Ch, Cw)])
    while queue:
        (h, w) = queue.popleft()
        for i in range(4):
            dhi, dwi = dh[i], dw[i]
            nh, nw = h + dhi, w + dwi
            if 0 <= nh < H and 0 <= nw < W and S[nh][nw] == ".":
                if dist[h][w] < dist[nh][nw]:
                    dist[nh][nw] = dist[h][w]
                    queue.appendleft((nh, nw))
        # 5x5-grid
        for ddh in range(-2, 3):
            for ddw in range(-2, 3):
                if abs(ddh) + abs(ddw) <= 1:
                    continue
                nh, nw = h + ddh, w + ddw
                if 0 <= nh < H and 0 <= nw < W and S[nh][nw] == ".":
                    if dist[nh][nw] > dist[h][w] + 1:
                        dist[nh][nw] = dist[h][w] + 1
                        queue.append((nh, nw))
    if dist[Dh][Dw] == INF:
        print((-1))
    else:
        print((dist[Dh][Dw]))


if __name__ == "__main__":
    main()

+dh = [0, 0, 1, -1] +dw = [1, -1, 0, 0] -    S = [eval(input()) for _ in range(H)] -    # dist = defaultdict(lambda: float("inf")) -    dist = [[float("inf")] * W for _ in range(H)] -    queue = deque([(Ch, Cw, 0)]) +    S = [list(eval(input())) for _ in range(H)] +    dist = [[INF] * W for _ in range(H)] +    dist[Ch][Cw] = 0 +    queue = deque([(Ch, Cw)]) -        (h, w, d) = queue.popleft() -        if dist[h][w] == float("inf") or dist[h][w] > d: -            dist[h][w] = d -            # 5x5-grid -            for ddh in range(-2, 3): -                for ddw in range(-2, 3): -                    nh, nw = h + ddh, w + ddw -                    if 0 <= nh < H and 0 <= nw < W and S[nh][nw] == ".": -                        # cost-0 move -                        if abs(ddh) + abs(ddw) <= 1: -                            queue.appendleft((nh, nw, d)) -                        # cost-1 move -                        else: -                            queue.append((nh, nw, d + 1)) -    if dist[Dh][Dw] == float("inf"): +        (h, w) = queue.popleft() +        for i in range(4): +            dhi, dwi = dh[i], dw[i] +            nh, nw = h + dhi, w + dwi +            if 0 <= nh < H and 0 <= nw < W and S[nh][nw] == ".": +                if dist[h][w] < dist[nh][nw]: +                    dist[nh][nw] = dist[h][w] +                    queue.appendleft((nh, nw)) +        # 5x5-grid +        for ddh in range(-2, 3): +            for ddw in range(-2, 3): +                if abs(ddh) + abs(ddw) <= 1: +                    continue +                nh, nw = h + ddh, w + ddw +                if 0 <= nh < H and 0 <= nw < W and S[nh][nw] == ".": +                    if dist[nh][nw] > dist[h][w] + 1: +                        dist[nh][nw] = dist[h][w] + 1 +                        queue.append((nh, nw)) +    if dist[Dh][Dw] == INF: