import sys, math

from collections import defaultdict, deque, Counter

from bisect import bisect_left, bisect_right

from itertools import combinations, permutations, product

#from heapq import heappush, heappop

from functools import lru_cache

input = sys.stdin.readline

rs = lambda: input().strip()

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

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

mod = 1000000007

sys.setrecursionlimit(1000000)



import heapq

def dijkstra(start, end):

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

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

	dist[start[0]][start[1]] = 0

	heap = []

	heapq.heappush(heap, (0, start))

	while heap:

		d, n = heapq.heappop(heap)

		if n == end:

			return d

		if dist[n[0]][n[1]] != d:

			continue

		edges = get_edges(n)

		for nn, ed in edges:

			nd = d + ed

			ni, nj = nn

			if dist[ni][nj] > nd:

				dist[ni][nj] = nd

				heapq.heappush(heap, (nd, nn))

	return -1



def get_edges(n):

	i, j = n

	edges = []

	if M[i][j] == '#': return edges

	for di, dj in [(1, 0), (-1, 0), (0, 1), (0, -1)]:

		ni, nj = i+di, j+dj

		if not (0 <= ni < H and 0 <= nj < W): continue

		if M[ni][nj] == '#': continue

		edges.append(((ni, nj), 0))

	for di in range(-2, +3):

		for dj in range(-2, +3):

			ni, nj = i+di, j+dj

			if di == 0 and dj == 0: continue

			if (di, dj) in [(1, 0), (-1, 0), (0, 1), (0, -1)]:

				continue

			if not(0 <= ni < H and 0 <= nj < W): continue

			if M[ni][nj] == '#': continue

			edges.append(((ni, nj), 1))

	return edges



H, W = rl()

C = rl()

D = rl()

M = []

for i in range(H):

	s = rs()

	M.append(s)



start = tuple([C[0]-1, C[1]-1])

end = tuple([D[0]-1, D[1]-1])

d = dijkstra(start, end)

print(d)

import sys, math

from collections import defaultdict, deque, Counter

from bisect import bisect_left, bisect_right

from itertools import combinations, permutations, product

from heapq import heappush, heappop

from functools import lru_cache

input = sys.stdin.readline

rs = lambda: input().strip()

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

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

narr = lambda d, f: narr(d[:-1], lambda: [f() for _ in range(d[-1])]) if d else f() # narr((2,3), int)

mod = 1000000007

sys.setrecursionlimit(1000000)



H, W = rl()

C = rl()

D = rl()

M = []

for i in range(H):

	s = rs()

	M.append(s)



start = tuple([C[0]-1, C[1]-1])

end = tuple([D[0]-1, D[1]-1])



queue = deque()

queue.append((start, 0))

visited = narr((H, W), lambda: float('inf'))

visited[start[0]][start[1]] = 0



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

while queue:

	n, d = queue.popleft()

	if n == end:

		print(d)

		exit()

	i, j = n

	if visited[i][j] != d:

		continue

	nd = d

	for di, dj in ds:

		ni, nj = i+di, j+dj

		if not (0 <= ni < H and 0 <= nj < W): continue

		if M[ni][nj] == '#': continue

		if nd < visited[ni][nj]:

			queue.appendleft(((ni,nj), nd))

			visited[ni][nj] = nd

	nd = d+1

	for di in range(-2, 3):

		for dj in range(-2, 3):

			if (di, dj) == (0,0): continue

			ni, nj = i+di, j+dj

			if not (0 <= ni < H and 0 <= nj < W): continue

			if M[ni][nj] == '#': continue

			if nd < visited[ni][nj]:

				queue.append(((ni,nj), nd))

				visited[ni][nj] = nd

print((-1))

import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations, product

# from heapq import heappush, heappop
from functools import lru_cache

input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(eval(input()))
rl = lambda: list(map(int, input().split()))
mod = 1000000007
sys.setrecursionlimit(1000000)
import heapq


def dijkstra(start, end):
    # dist = defaultdict(lambda: float('inf'))
    dist = [[float("inf")] * W for _ in range(H)]
    dist[start[0]][start[1]] = 0
    heap = []
    heapq.heappush(heap, (0, start))
    while heap:
        d, n = heapq.heappop(heap)
        if n == end:
            return d
        if dist[n[0]][n[1]] != d:
            continue
        edges = get_edges(n)
        for nn, ed in edges:
            nd = d + ed
            ni, nj = nn
            if dist[ni][nj] > nd:
                dist[ni][nj] = nd
                heapq.heappush(heap, (nd, nn))
    return -1


def get_edges(n):
    i, j = n
    edges = []
    if M[i][j] == "#":
        return edges
    for di, dj in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
        ni, nj = i + di, j + dj
        if not (0 <= ni < H and 0 <= nj < W):
            continue
        if M[ni][nj] == "#":
            continue
        edges.append(((ni, nj), 0))
    for di in range(-2, +3):
        for dj in range(-2, +3):
            ni, nj = i + di, j + dj
            if di == 0 and dj == 0:
                continue
            if (di, dj) in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
                continue
            if not (0 <= ni < H and 0 <= nj < W):
                continue
            if M[ni][nj] == "#":
                continue
            edges.append(((ni, nj), 1))
    return edges


H, W = rl()
C = rl()
D = rl()
M = []
for i in range(H):
    s = rs()
    M.append(s)
start = tuple([C[0] - 1, C[1] - 1])
end = tuple([D[0] - 1, D[1] - 1])
d = dijkstra(start, end)
print(d)

import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations, product
from heapq import heappush, heappop
from functools import lru_cache

input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(eval(input()))
rl = lambda: list(map(int, input().split()))
narr = (
    lambda d, f: narr(d[:-1], lambda: [f() for _ in range(d[-1])]) if d else f()
)  # narr((2,3), int)
mod = 1000000007
sys.setrecursionlimit(1000000)
H, W = rl()
C = rl()
D = rl()
M = []
for i in range(H):
    s = rs()
    M.append(s)
start = tuple([C[0] - 1, C[1] - 1])
end = tuple([D[0] - 1, D[1] - 1])
queue = deque()
queue.append((start, 0))
visited = narr((H, W), lambda: float("inf"))
visited[start[0]][start[1]] = 0
ds = [(1, 0), (-1, 0), (0, 1), (0, -1)]
while queue:
    n, d = queue.popleft()
    if n == end:
        print(d)
        exit()
    i, j = n
    if visited[i][j] != d:
        continue
    nd = d
    for di, dj in ds:
        ni, nj = i + di, j + dj
        if not (0 <= ni < H and 0 <= nj < W):
            continue
        if M[ni][nj] == "#":
            continue
        if nd < visited[ni][nj]:
            queue.appendleft(((ni, nj), nd))
            visited[ni][nj] = nd
    nd = d + 1
    for di in range(-2, 3):
        for dj in range(-2, 3):
            if (di, dj) == (0, 0):
                continue
            ni, nj = i + di, j + dj
            if not (0 <= ni < H and 0 <= nj < W):
                continue
            if M[ni][nj] == "#":
                continue
            if nd < visited[ni][nj]:
                queue.append(((ni, nj), nd))
                visited[ni][nj] = nd
print((-1))

- -# from heapq import heappush, heappop +from heapq import heappush, heappop +narr = ( +    lambda d, f: narr(d[:-1], lambda: [f() for _ in range(d[-1])]) if d else f() +)  # narr((2,3), int) -import heapq - - -def dijkstra(start, end): -    # dist = defaultdict(lambda: float('inf')) -    dist = [[float("inf")] * W for _ in range(H)] -    dist[start[0]][start[1]] = 0 -    heap = [] -    heapq.heappush(heap, (0, start)) -    while heap: -        d, n = heapq.heappop(heap) -        if n == end: -            return d -        if dist[n[0]][n[1]] != d: -            continue -        edges = get_edges(n) -        for nn, ed in edges: -            nd = d + ed -            ni, nj = nn -            if dist[ni][nj] > nd: -                dist[ni][nj] = nd -                heapq.heappush(heap, (nd, nn)) -    return -1 - - -def get_edges(n): -    i, j = n -    edges = [] -    if M[i][j] == "#": -        return edges -    for di, dj in [(1, 0), (-1, 0), (0, 1), (0, -1)]: -        ni, nj = i + di, j + dj -        if not (0 <= ni < H and 0 <= nj < W): -            continue -        if M[ni][nj] == "#": -            continue -        edges.append(((ni, nj), 0)) -    for di in range(-2, +3): -        for dj in range(-2, +3): -            ni, nj = i + di, j + dj -            if di == 0 and dj == 0: -                continue -            if (di, dj) in [(1, 0), (-1, 0), (0, 1), (0, -1)]: -                continue -            if not (0 <= ni < H and 0 <= nj < W): -                continue -            if M[ni][nj] == "#": -                continue -            edges.append(((ni, nj), 1)) -    return edges - - -d = dijkstra(start, end) -print(d) +queue = deque() +queue.append((start, 0)) +visited = narr((H, W), lambda: float("inf")) +visited[start[0]][start[1]] = 0 +ds = [(1, 0), (-1, 0), (0, 1), (0, -1)] +while queue: +    n, d = queue.popleft() +    if n == end: +        print(d) +        exit() +    i, j = n +    if visited[i][j] != d: +        continue +    nd = d +    for di, dj in ds: +        ni, nj = i + di, j + dj +        if not (0 <= ni < H and 0 <= nj < W): +            continue +        if M[ni][nj] == "#": +            continue +        if nd < visited[ni][nj]: +            queue.appendleft(((ni, nj), nd)) +            visited[ni][nj] = nd +    nd = d + 1 +    for di in range(-2, 3): +        for dj in range(-2, 3): +            if (di, dj) == (0, 0): +                continue +            ni, nj = i + di, j + dj +            if not (0 <= ni < H and 0 <= nj < W): +                continue +            if M[ni][nj] == "#": +                continue +            if nd < visited[ni][nj]: +                queue.append(((ni, nj), nd)) +                visited[ni][nj] = nd +print((-1))