from collections import deque



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

Ch, Cw = list([int(x) - 1 for x in input().split()])

Dh, Dw = list([int(x) - 1 for x in input().split()])

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



def walk(i, j):

    ans = []

    for h, w in [[i+1, j], [i-1, j], [i, j+1], [i, j-1]]:

        # print(h, w)

        if h >= 0 and w >= 0 and h < H and w < W:

            # print('hoen')

            if S[h][w] == '.':

                if maze[h][w] == -1:

                    ans.append((h, w))

    return ans



def warp(i, j):

    tmp = []

    for dh in range(-2, 3):

        for dw in range(-2, 3):

            if not (dh == 0 and dw == 0):

                tmp.append([i + dh, j + dw])

    ans = []

    for h, w in tmp:

        if h >= 0 and w >= 0 and h < H and w < W:

            if S[h][w] == '.':

                if maze[h][w] == -1:

                    ans.append((h, w))

    return ans



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

warp_n = 0

maze[Ch][Cw] = 0

walk_stack= deque(walk(Ch, Cw))

warp_set = set(warp(Ch, Cw))

# print(walk(Ch, Cw))

# print(warp(Ch, Cw))

# print('-----')

while walk_stack or warp_set:

    while walk_stack:

        h, w = walk_stack.pop()

        if maze[h][w] == -1:

            maze[h][w] = warp_n

            walk_stack.extend(walk(h, w))

            warp_set |= set(warp(h, w))

    warp_n += 1

    # print(*maze, sep='\n')

    # print('-----')

    next_warp = set()

    for h, w in warp_set:

        if maze[h][w] == -1:

            maze[h][w] = warp_n

            walk_stack.extend(walk(h, w))

            next_warp |= set(warp(h, w))

    warp_set = next_warp

print((maze[Dh][Dw]))
from collections import deque



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

Ch, Cw = list([int(x) - 1 for x in input().split()])

Dh, Dw = list([int(x) - 1 for x in input().split()])

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

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



def walk(h, w, warp_n):

    ans = []

    if maze[h][w] != -1:

        return []

    else:

        maze[h][w] = warp_n

        ans += [(h, w)]

        

    return ans



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

while queue:

    h, w, warp_n = queue.popleft()

    if maze[h][w] != -1:

        continue

    maze[h][w] = warp_n

    # walk

    for dh, dw in [(-1, 0), (1, 0), (0, -1), (0, 1)]:

            if h+dh < 0 or w+dw < 0 or h+dh >= H or w+dw >= W:

                continue

            if S[h+dh][w+dw] == '#':

                continue

            queue.appendleft((h+dh, w+dw, warp_n))

    # warp

    for dh in range(-2, 3):

        for dw in range(-2, 3):

            if h+dh < 0 or w+dw < 0 or h+dh >= H or w+dw >= W:

                continue

            if S[h+dh][w+dw] == '#':

                continue

            if dh == 0 and dw == 0:

                continue

            queue.append((h+dh, w+dw, warp_n+1))



print((maze[Dh][Dw]))
from collections import deque

H, W = list(map(int, input().split()))
Ch, Cw = list([int(x) - 1 for x in input().split()])
Dh, Dw = list([int(x) - 1 for x in input().split()])
S = [eval(input()) for _ in range(H)]


def walk(i, j):
    ans = []
    for h, w in [[i + 1, j], [i - 1, j], [i, j + 1], [i, j - 1]]:
        # print(h, w)
        if h >= 0 and w >= 0 and h < H and w < W:
            # print('hoen')
            if S[h][w] == ".":
                if maze[h][w] == -1:
                    ans.append((h, w))
    return ans


def warp(i, j):
    tmp = []
    for dh in range(-2, 3):
        for dw in range(-2, 3):
            if not (dh == 0 and dw == 0):
                tmp.append([i + dh, j + dw])
    ans = []
    for h, w in tmp:
        if h >= 0 and w >= 0 and h < H and w < W:
            if S[h][w] == ".":
                if maze[h][w] == -1:
                    ans.append((h, w))
    return ans


maze = [[-1] * W for _ in range(H)]
warp_n = 0
maze[Ch][Cw] = 0
walk_stack = deque(walk(Ch, Cw))
warp_set = set(warp(Ch, Cw))
# print(walk(Ch, Cw))
# print(warp(Ch, Cw))
# print('-----')
while walk_stack or warp_set:
    while walk_stack:
        h, w = walk_stack.pop()
        if maze[h][w] == -1:
            maze[h][w] = warp_n
            walk_stack.extend(walk(h, w))
            warp_set |= set(warp(h, w))
    warp_n += 1
    # print(*maze, sep='\n')
    # print('-----')
    next_warp = set()
    for h, w in warp_set:
        if maze[h][w] == -1:
            maze[h][w] = warp_n
            walk_stack.extend(walk(h, w))
            next_warp |= set(warp(h, w))
    warp_set = next_warp
print((maze[Dh][Dw]))

from collections import deque

H, W = list(map(int, input().split()))
Ch, Cw = list([int(x) - 1 for x in input().split()])
Dh, Dw = list([int(x) - 1 for x in input().split()])
S = [eval(input()) for _ in range(H)]
maze = [[-1] * W for _ in range(H)]


def walk(h, w, warp_n):
    ans = []
    if maze[h][w] != -1:
        return []
    else:
        maze[h][w] = warp_n
        ans += [(h, w)]
    return ans


queue = deque([(Ch, Cw, 0)])
while queue:
    h, w, warp_n = queue.popleft()
    if maze[h][w] != -1:
        continue
    maze[h][w] = warp_n
    # walk
    for dh, dw in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
        if h + dh < 0 or w + dw < 0 or h + dh >= H or w + dw >= W:
            continue
        if S[h + dh][w + dw] == "#":
            continue
        queue.appendleft((h + dh, w + dw, warp_n))
    # warp
    for dh in range(-2, 3):
        for dw in range(-2, 3):
            if h + dh < 0 or w + dw < 0 or h + dh >= H or w + dw >= W:
                continue
            if S[h + dh][w + dw] == "#":
                continue
            if dh == 0 and dw == 0:
                continue
            queue.append((h + dh, w + dw, warp_n + 1))
print((maze[Dh][Dw]))

+maze = [[-1] * W for _ in range(H)] -def walk(i, j): +def walk(h, w, warp_n): -    for h, w in [[i + 1, j], [i - 1, j], [i, j + 1], [i, j - 1]]: -        # print(h, w) -        if h >= 0 and w >= 0 and h < H and w < W: -            # print('hoen') -            if S[h][w] == ".": -                if maze[h][w] == -1: -                    ans.append((h, w)) +    if maze[h][w] != -1: +        return [] +    else: +        maze[h][w] = warp_n +        ans += [(h, w)] -def warp(i, j): -    tmp = [] +queue = deque([(Ch, Cw, 0)]) +while queue: +    h, w, warp_n = queue.popleft() +    if maze[h][w] != -1: +        continue +    maze[h][w] = warp_n +    # walk +    for dh, dw in [(-1, 0), (1, 0), (0, -1), (0, 1)]: +        if h + dh < 0 or w + dw < 0 or h + dh >= H or w + dw >= W: +            continue +        if S[h + dh][w + dw] == "#": +            continue +        queue.appendleft((h + dh, w + dw, warp_n)) +    # warp -            if not (dh == 0 and dw == 0): -                tmp.append([i + dh, j + dw]) -    ans = [] -    for h, w in tmp: -        if h >= 0 and w >= 0 and h < H and w < W: -            if S[h][w] == ".": -                if maze[h][w] == -1: -                    ans.append((h, w)) -    return ans - - -maze = [[-1] * W for _ in range(H)] -warp_n = 0 -maze[Ch][Cw] = 0 -walk_stack = deque(walk(Ch, Cw)) -warp_set = set(warp(Ch, Cw)) -# print(walk(Ch, Cw)) -# print(warp(Ch, Cw)) -while walk_stack or warp_set: -    while walk_stack: -        h, w = walk_stack.pop() -        if maze[h][w] == -1: -            maze[h][w] = warp_n -            walk_stack.extend(walk(h, w)) -            warp_set |= set(warp(h, w)) -    warp_n += 1 -    # print(*maze, sep='\n') -    next_warp = set() -    for h, w in warp_set: -        if maze[h][w] == -1: -            maze[h][w] = warp_n -            walk_stack.extend(walk(h, w)) -            next_warp |= set(warp(h, w)) -    warp_set = next_warp +            if h + dh < 0 or w + dw < 0 or h + dh >= H or w + dw >= W: +                continue +            if S[h + dh][w + dw] == "#": +                continue +            if dh == 0 and dw == 0: +                continue +            queue.append((h + dh, w + dw, warp_n + 1))