def main():

    from sys import setrecursionlimit, stdin, stderr

    from os import environ

    from collections import defaultdict, deque, Counter

    from math import ceil, floor

    from itertools import accumulate, combinations, combinations_with_replacement

    setrecursionlimit(10**6)

    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0

    input = lambda: stdin.readline().rstrip()

    LMIIS = lambda: list(map(int,input().split()))

    II = lambda: int(eval(input()))

    P = 10**9+7

    INF = 10**18+10



    H,W = LMIIS()

    Ch,Cw = LMIIS()

    Ch -= 1

    Cw -= 1

    Dh,Dw = LMIIS()

    Dh -= 1

    Dw -= 1

    S = []

    for i in range(H):

        S.append(eval(input()))

    

    num_warp = []

    for i in range(H):

        num_warp.append([-1]*W)

    

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





    def check_and_move(y,x,d):

        if 0 <= y < H and 0 <= x < W and num_warp[y][x] == -1 and S[y][x] == '.':

            num_warp[y][x] = d

            return True

        return False









    # 歩行はDFS, ワープはBFS

    while targets:

        y,x,d = targets.pop()

        if not check_and_move(y,x,d):

            continue



        for y2,x2 in [(y-1,x),(y,x+1),(y+1,x),(y,x-1)]:

            targets.append((y2,x2,d))



        for y2,x2 in[

                (y-2,x-2),(y-2,x-1),(y-2,x),(y-2,x+1),(y-2,x+2),

                (y-1,x-2),(y-1,x-1),(y-1,x+1),(y-2,x+2),

                (y,x-2),(y,x+2),

                (y+1,x-2),(y+1,x-1),(y+1,x+1),(y+1,x+2),

                (y+2,x-2),(y+2,x-1),(y+2,x),(y+2,x+1),(y+2,x+2)

            ]:

            targets.appendleft((y2,x2,d+1))



    print((num_warp[Dh][Dw]))







    

main()
def main():

    from sys import setrecursionlimit, stdin, stderr

    from os import environ

    from collections import defaultdict, deque, Counter

    from math import ceil, floor

    from itertools import accumulate, combinations, combinations_with_replacement

    setrecursionlimit(10**6)

    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0

    input = lambda: stdin.readline().rstrip()

    LMIIS = lambda: list(map(int,input().split()))

    II = lambda: int(eval(input()))

    P = 10**9+7

    INF = 10**18+10



    H,W = LMIIS()

    Ch,Cw = LMIIS()

    Ch -= 1

    Cw -= 1

    Dh,Dw = LMIIS()

    Dh -= 1

    Dw -= 1

    S = []

    for i in range(H):

        S.append(eval(input()))

    

    num_warp = []

    for i in range(H):

        num_warp.append([-1]*W)

    

    





    def check_and_move(y,x,d):

        if 0 <= y < H and 0 <= x < W and num_warp[y][x] == -1 and S[y][x] == '.':

            num_warp[y][x] = d

            return True

        return False





    # 歩行はDFS, ワープはBFS

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

    while targets:

        y,x,d = targets.pop()

        if not check_and_move(y,x,d):

            continue



        if y == Dh and x == Dw:

            break



        for y2,x2 in [(y-1,x),(y,x+1),(y+1,x),(y,x-1)]:

            targets.append((y2,x2,d))



        for y2,x2 in[

                (y-2,x-2),(y-2,x-1),(y-2,x),(y-2,x+1),(y-2,x+2),

                (y-1,x-2),(y-1,x-1),(y-1,x+1),(y-2,x+2),

                (y,x-2),(y,x+2),

                (y+1,x-2),(y+1,x-1),(y+1,x+1),(y+1,x+2),

                (y+2,x-2),(y+2,x-1),(y+2,x),(y+2,x+1),(y+2,x+2)

            ]:

            targets.appendleft((y2,x2,d+1))



    print((num_warp[Dh][Dw]))







    

main()
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement

    setrecursionlimit(10**6)
    dbg = (
        (
            lambda *something: stderr.write(
                "\033[92m{}\033[0m".format(str(something) + "\n")
            )
        )
        if "TERM_PROGRAM" in environ
        else lambda *x: 0
    )
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int, input().split()))
    II = lambda: int(eval(input()))
    P = 10**9 + 7
    INF = 10**18 + 10
    H, W = LMIIS()
    Ch, Cw = LMIIS()
    Ch -= 1
    Cw -= 1
    Dh, Dw = LMIIS()
    Dh -= 1
    Dw -= 1
    S = []
    for i in range(H):
        S.append(eval(input()))
    num_warp = []
    for i in range(H):
        num_warp.append([-1] * W)
    targets = deque([(Ch, Cw, 0)])

    def check_and_move(y, x, d):
        if 0 <= y < H and 0 <= x < W and num_warp[y][x] == -1 and S[y][x] == ".":
            num_warp[y][x] = d
            return True
        return False

    # 歩行はDFS, ワープはBFS
    while targets:
        y, x, d = targets.pop()
        if not check_and_move(y, x, d):
            continue
        for y2, x2 in [(y - 1, x), (y, x + 1), (y + 1, x), (y, x - 1)]:
            targets.append((y2, x2, d))
        for y2, x2 in [
            (y - 2, x - 2),
            (y - 2, x - 1),
            (y - 2, x),
            (y - 2, x + 1),
            (y - 2, x + 2),
            (y - 1, x - 2),
            (y - 1, x - 1),
            (y - 1, x + 1),
            (y - 2, x + 2),
            (y, x - 2),
            (y, x + 2),
            (y + 1, x - 2),
            (y + 1, x - 1),
            (y + 1, x + 1),
            (y + 1, x + 2),
            (y + 2, x - 2),
            (y + 2, x - 1),
            (y + 2, x),
            (y + 2, x + 1),
            (y + 2, x + 2),
        ]:
            targets.appendleft((y2, x2, d + 1))
    print((num_warp[Dh][Dw]))


main()

def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement

    setrecursionlimit(10**6)
    dbg = (
        (
            lambda *something: stderr.write(
                "\033[92m{}\033[0m".format(str(something) + "\n")
            )
        )
        if "TERM_PROGRAM" in environ
        else lambda *x: 0
    )
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int, input().split()))
    II = lambda: int(eval(input()))
    P = 10**9 + 7
    INF = 10**18 + 10
    H, W = LMIIS()
    Ch, Cw = LMIIS()
    Ch -= 1
    Cw -= 1
    Dh, Dw = LMIIS()
    Dh -= 1
    Dw -= 1
    S = []
    for i in range(H):
        S.append(eval(input()))
    num_warp = []
    for i in range(H):
        num_warp.append([-1] * W)

    def check_and_move(y, x, d):
        if 0 <= y < H and 0 <= x < W and num_warp[y][x] == -1 and S[y][x] == ".":
            num_warp[y][x] = d
            return True
        return False

    # 歩行はDFS, ワープはBFS
    targets = deque([(Ch, Cw, 0)])
    while targets:
        y, x, d = targets.pop()
        if not check_and_move(y, x, d):
            continue
        if y == Dh and x == Dw:
            break
        for y2, x2 in [(y - 1, x), (y, x + 1), (y + 1, x), (y, x - 1)]:
            targets.append((y2, x2, d))
        for y2, x2 in [
            (y - 2, x - 2),
            (y - 2, x - 1),
            (y - 2, x),
            (y - 2, x + 1),
            (y - 2, x + 2),
            (y - 1, x - 2),
            (y - 1, x - 1),
            (y - 1, x + 1),
            (y - 2, x + 2),
            (y, x - 2),
            (y, x + 2),
            (y + 1, x - 2),
            (y + 1, x - 1),
            (y + 1, x + 1),
            (y + 1, x + 2),
            (y + 2, x - 2),
            (y + 2, x - 1),
            (y + 2, x),
            (y + 2, x + 1),
            (y + 2, x + 2),
        ]:
            targets.appendleft((y2, x2, d + 1))
    print((num_warp[Dh][Dw]))


main()

-    targets = deque([(Ch, Cw, 0)]) +    targets = deque([(Ch, Cw, 0)]) +        if y == Dh and x == Dw: +            break