from collections import deque

from itertools import product



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

Ch, Cw = map(lambda x: int(x)-1, input().split())

Dh, Dw = map(lambda x: int(x)-1, input().split())

maze = [list(input()) for _ in range(H)] # 迷路

walkFrom = deque([(Ch, Cw)]) # 歩く出発地点の候補集

warpFrom = deque() # ワープ出発地点の候補集

WalkRange = [(1, 0), (-1, 0), (0, 1), (0, -1)]

WarpRange = list(filter(lambda x: x != (0, 0), product(range(-2, 3), repeat=2)))

warpCount = 0



def move(h_from, w_from, moveRange):

  for h_dist, w_dist in moveRange:

    h_to, w_to = h_from + h_dist, w_from + w_dist

    if 0 <= h_to < H and 0 <= w_to < W and maze[h_to][w_to] == '.':

      maze[h_to][w_to] = warpCount

      walkFrom.append((h_to, w_to))



# 迷路上の到達可能な全ての場所について、道(".")を必要なワープ回数で上書きする

maze[Ch][Cw] = warpCount

while walkFrom:

  # 歩ける範囲をすべて歩く

  while walkFrom:

    h, w = walkFrom.popleft()

    warpFrom.append((h, w))

    move(h, w, WalkRange)

  # 一度のワープで到達できる未踏の場所すべてにワープする

  warpCount += 1

  while warpFrom:

    h, w = warpFrom.popleft()

    move(h, w, WarpRange)



goal = maze[Dh][Dw]

print('-1') if goal in ['.', '#'] else print(goal)

from collections import deque

from itertools import product



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

Ch, Cw = map(lambda x: int(x)-1, input().split())

Dh, Dw = map(lambda x: int(x)-1, input().split())

maze = [list(input()) for _ in range(H)] # 迷路

walkFrom = deque([(Ch, Cw)]) # 歩く出発地点の候補集

warpFrom = deque() # ワープ出発地点の候補集

WalkRange = [(1, 0), (-1, 0), (0, 1), (0, -1)]

WarpRange = list(filter(lambda x: not x in WalkRange + [(0, 0)], product(range(-2, 3), repeat=2)))

warpCount = 0



def move(h_from, w_from, moveRange):

  for h_dist, w_dist in moveRange:

    h_to, w_to = h_from + h_dist, w_from + w_dist

    if 0 <= h_to < H and 0 <= w_to < W and maze[h_to][w_to] == '.':

      maze[h_to][w_to] = warpCount

      walkFrom.append((h_to, w_to))



# 迷路上の到達可能な全ての場所について、道(".")を必要なワープ回数で上書きする

maze[Ch][Cw] = warpCount

while walkFrom:

  # 歩ける範囲をすべて歩く

  while walkFrom:

    h, w = walkFrom.popleft()

    warpFrom.append((h, w))

    move(h, w, WalkRange)

  # 一度のワープで到達できる未踏の場所すべてにワープする

  warpCount += 1

  while warpFrom:

    h, w = warpFrom.popleft()

    move(h, w, WarpRange)



goal = maze[Dh][Dw]

print('-1') if goal in ['.', '#'] else print(goal)

from collections import deque
from itertools import product

H, W = map(int, input().split())
Ch, Cw = map(lambda x: int(x) - 1, input().split())
Dh, Dw = map(lambda x: int(x) - 1, input().split())
maze = [list(input()) for _ in range(H)]  # 迷路
walkFrom = deque([(Ch, Cw)])  # 歩く出発地点の候補集
warpFrom = deque()  # ワープ出発地点の候補集
WalkRange = [(1, 0), (-1, 0), (0, 1), (0, -1)]
WarpRange = list(filter(lambda x: x != (0, 0), product(range(-2, 3), repeat=2)))
warpCount = 0


def move(h_from, w_from, moveRange):
    for h_dist, w_dist in moveRange:
        h_to, w_to = h_from + h_dist, w_from + w_dist
        if 0 <= h_to < H and 0 <= w_to < W and maze[h_to][w_to] == ".":
            maze[h_to][w_to] = warpCount
            walkFrom.append((h_to, w_to))


# 迷路上の到達可能な全ての場所について、道(".")を必要なワープ回数で上書きする
maze[Ch][Cw] = warpCount
while walkFrom:
    # 歩ける範囲をすべて歩く
    while walkFrom:
        h, w = walkFrom.popleft()
        warpFrom.append((h, w))
        move(h, w, WalkRange)
    # 一度のワープで到達できる未踏の場所すべてにワープする
    warpCount += 1
    while warpFrom:
        h, w = warpFrom.popleft()
        move(h, w, WarpRange)
goal = maze[Dh][Dw]
print("-1") if goal in [".", "#"] else print(goal)

from collections import deque
from itertools import product

H, W = map(int, input().split())
Ch, Cw = map(lambda x: int(x) - 1, input().split())
Dh, Dw = map(lambda x: int(x) - 1, input().split())
maze = [list(input()) for _ in range(H)]  # 迷路
walkFrom = deque([(Ch, Cw)])  # 歩く出発地点の候補集
warpFrom = deque()  # ワープ出発地点の候補集
WalkRange = [(1, 0), (-1, 0), (0, 1), (0, -1)]
WarpRange = list(
    filter(lambda x: not x in WalkRange + [(0, 0)], product(range(-2, 3), repeat=2))
)
warpCount = 0


def move(h_from, w_from, moveRange):
    for h_dist, w_dist in moveRange:
        h_to, w_to = h_from + h_dist, w_from + w_dist
        if 0 <= h_to < H and 0 <= w_to < W and maze[h_to][w_to] == ".":
            maze[h_to][w_to] = warpCount
            walkFrom.append((h_to, w_to))


# 迷路上の到達可能な全ての場所について、道(".")を必要なワープ回数で上書きする
maze[Ch][Cw] = warpCount
while walkFrom:
    # 歩ける範囲をすべて歩く
    while walkFrom:
        h, w = walkFrom.popleft()
        warpFrom.append((h, w))
        move(h, w, WalkRange)
    # 一度のワープで到達できる未踏の場所すべてにワープする
    warpCount += 1
    while warpFrom:
        h, w = warpFrom.popleft()
        move(h, w, WarpRange)
goal = maze[Dh][Dw]
print("-1") if goal in [".", "#"] else print(goal)

-WarpRange = list(filter(lambda x: x != (0, 0), product(range(-2, 3), repeat=2))) +WarpRange = list( +    filter(lambda x: not x in WalkRange + [(0, 0)], product(range(-2, 3), repeat=2)) +)