h, w = list(map(int,input().split()))

sx, sy = list(map(int,input().split()))

gx, gy = list(map(int,input().split()))

S = [eval(input()) for i in range(h)]



sx-=1

sy-=1

gx-=1

gy-=1



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

B = []

for i in range(-2,3):

    for j in range(-2,3):

        if i!= 0 or j!=0:

            B.append((i,j))

# print(B)



# O((V + E)log(V))

# Cは [[スタート頂点, ゴール頂点, 重み], ...] のリスト



M = [[] for i in range(h*w)]



for i in range(h):

    for j in range(w):

        if S[i][j] == ".":

            for a in A:

                ai = i+a[0]

                aj = j+a[1]

                if 0 <= ai < h and 0 <= aj < w and S[ai][aj] == ".":

                    M[i*w+j].append((ai*w+aj, 0))

            for b in B:

                bi = i+b[0]

                bj = j+b[1]

                if 0 <= bi < h and 0 <= bj < w and S[bi][bj] == ".":

                    M[i*w+j].append((bi*w+bj,1))





# for i in range(len(C)):



#     M[C[i][0]].append([C[i][1], C[i][2]])  # 無向グラフなら両方追加

#     M[C[i][1]].append([C[i][0], C[i][2]])  # 無向グラフなら両方追加



n = h*w



import heapq



big = 10**9

D = [big] * n  # 頂点0からの距離

P = [-1] * n  # 頂点0からの最短距離において、そこの直前の頂点(経路復元に利用)

D[sx*w+sy] = 0

V = [0] * n  # その頂点のD[i]が最短距離と確定したら1

Q = []  # 優先度付きキュー

# for v in range(n):

#     heapq.heappush(Q, (D[v], v))

heapq.heappush(Q, (0, sx*w+sy))



# le = n

while len(Q) > 0:

    # print(Q,le)

    q = heapq.heappop(Q)

    u = q[1]

    du = q[0]

    if V[u] == 0:

        V[u] = 1

        # le -= 1

        for i in range(len(M[u])):

            v = M[u][i][0]

            luv = M[u][i][1]

            # print(v)

            if V[v] == 0:

                alt = du + luv

                if D[v] > alt:

                    D[v] = alt

                    P[v] = u

                    heapq.heappush(Q, (alt, v))

                    # le += 1

                    # print(Q)

# print(D)

ans = D[gx*w+gy]

if ans > 10**7:

    ans = -1

print(ans)
h, w = list(map(int,input().split()))

si, sj = list(map(int,input().split()))

gi, gj = list(map(int,input().split()))

S = [eval(input()) for i in range(h)]

n = h * w



si-=1

sj-=1

gi-=1

gj-=1



# M = [[] for i in range(h*w)]



A = [[-1,0],[1,0],[0,1],[0,-1]]

B = []

for i in range(-2, 3):

    for j in range(-2, 3):

        if (i!=0 or j!=0) and (not ([i, j] in A)):

            B.append([i, j])



# for i in range(h):

#     for j in range(w):

#         if S[i][j] == ".":

#             for a in A:

#                 ai = i + a[i]

#                 aj = j + a[j]

#                 if 0 <= ai < h and 0 <= aj < w and S[ai][aj] == ".":

#                     M[i*w+j].append([ai*w+aj, 0])

#             for a in B:

#                 ai = i + a[i]

#                 aj = j + a[j]

#                 if 0 <= ai < h and 0 <= aj < w and S[ai][aj] == ".":

#                     M[i*w+j].append([ai*w+aj, 1])



from collections import deque

D = deque()



V = [0] * n

DI = [float("inf")] * n

s = si*w+sj

g = gi*w+gj

DI[s] = 0



D.append([s, 0])



while len(D) > 0:

    d = D.popleft()

    x = d[0]

    we = d[1]

    if V[x] == 0:

        V[x] = 1

        DI[x] = we

        i = x//w

        j = x%w

        for a in A:

            ai = i + a[0]

            aj = j + a[1]

            if 0 <= ai < h and 0 <= aj < w and S[ai][aj] == ".":

                D.appendleft([ai*w+aj, we])

        for a in B:

            ai = i + a[0]

            aj = j + a[1]

            if 0 <= ai < h and 0 <= aj < w and S[ai][aj] == ".":

                D.append([ai*w+aj, we+1])



# print(DI)

# print(DI[g])

ans = DI[g]

if DI[g] == float("inf"):

    ans = -1

print(ans)
h, w = list(map(int, input().split()))
sx, sy = list(map(int, input().split()))
gx, gy = list(map(int, input().split()))
S = [eval(input()) for i in range(h)]
sx -= 1
sy -= 1
gx -= 1
gy -= 1
A = [(-1, 0), (1, 0), (0, -1), (0, 1)]
B = []
for i in range(-2, 3):
    for j in range(-2, 3):
        if i != 0 or j != 0:
            B.append((i, j))
# print(B)
# O((V + E)log(V))
# Cは [[スタート頂点, ゴール頂点, 重み], ...] のリスト
M = [[] for i in range(h * w)]
for i in range(h):
    for j in range(w):
        if S[i][j] == ".":
            for a in A:
                ai = i + a[0]
                aj = j + a[1]
                if 0 <= ai < h and 0 <= aj < w and S[ai][aj] == ".":
                    M[i * w + j].append((ai * w + aj, 0))
            for b in B:
                bi = i + b[0]
                bj = j + b[1]
                if 0 <= bi < h and 0 <= bj < w and S[bi][bj] == ".":
                    M[i * w + j].append((bi * w + bj, 1))
# for i in range(len(C)):
#     M[C[i][0]].append([C[i][1], C[i][2]])  # 無向グラフなら両方追加
#     M[C[i][1]].append([C[i][0], C[i][2]])  # 無向グラフなら両方追加
n = h * w
import heapq

big = 10**9
D = [big] * n  # 頂点0からの距離
P = [-1] * n  # 頂点0からの最短距離において、そこの直前の頂点(経路復元に利用)
D[sx * w + sy] = 0
V = [0] * n  # その頂点のD[i]が最短距離と確定したら1
Q = []  # 優先度付きキュー
# for v in range(n):
#     heapq.heappush(Q, (D[v], v))
heapq.heappush(Q, (0, sx * w + sy))
# le = n
while len(Q) > 0:
    # print(Q,le)
    q = heapq.heappop(Q)
    u = q[1]
    du = q[0]
    if V[u] == 0:
        V[u] = 1
        # le -= 1
        for i in range(len(M[u])):
            v = M[u][i][0]
            luv = M[u][i][1]
            # print(v)
            if V[v] == 0:
                alt = du + luv
                if D[v] > alt:
                    D[v] = alt
                    P[v] = u
                    heapq.heappush(Q, (alt, v))
                    # le += 1
                    # print(Q)
# print(D)
ans = D[gx * w + gy]
if ans > 10**7:
    ans = -1
print(ans)

h, w = list(map(int, input().split()))
si, sj = list(map(int, input().split()))
gi, gj = list(map(int, input().split()))
S = [eval(input()) for i in range(h)]
n = h * w
si -= 1
sj -= 1
gi -= 1
gj -= 1
# M = [[] for i in range(h*w)]
A = [[-1, 0], [1, 0], [0, 1], [0, -1]]
B = []
for i in range(-2, 3):
    for j in range(-2, 3):
        if (i != 0 or j != 0) and (not ([i, j] in A)):
            B.append([i, j])
# for i in range(h):
#     for j in range(w):
#         if S[i][j] == ".":
#             for a in A:
#                 ai = i + a[i]
#                 aj = j + a[j]
#                 if 0 <= ai < h and 0 <= aj < w and S[ai][aj] == ".":
#                     M[i*w+j].append([ai*w+aj, 0])
#             for a in B:
#                 ai = i + a[i]
#                 aj = j + a[j]
#                 if 0 <= ai < h and 0 <= aj < w and S[ai][aj] == ".":
#                     M[i*w+j].append([ai*w+aj, 1])
from collections import deque

D = deque()
V = [0] * n
DI = [float("inf")] * n
s = si * w + sj
g = gi * w + gj
DI[s] = 0
D.append([s, 0])
while len(D) > 0:
    d = D.popleft()
    x = d[0]
    we = d[1]
    if V[x] == 0:
        V[x] = 1
        DI[x] = we
        i = x // w
        j = x % w
        for a in A:
            ai = i + a[0]
            aj = j + a[1]
            if 0 <= ai < h and 0 <= aj < w and S[ai][aj] == ".":
                D.appendleft([ai * w + aj, we])
        for a in B:
            ai = i + a[0]
            aj = j + a[1]
            if 0 <= ai < h and 0 <= aj < w and S[ai][aj] == ".":
                D.append([ai * w + aj, we + 1])
# print(DI)
# print(DI[g])
ans = DI[g]
if DI[g] == float("inf"):
    ans = -1
print(ans)

-sx, sy = list(map(int, input().split())) -gx, gy = list(map(int, input().split())) +si, sj = list(map(int, input().split())) +gi, gj = list(map(int, input().split())) -sx -= 1 -sy -= 1 -gx -= 1 -gy -= 1 -A = [(-1, 0), (1, 0), (0, -1), (0, 1)] +n = h * w +si -= 1 +sj -= 1 +gi -= 1 +gj -= 1 +# M = [[] for i in range(h*w)] +A = [[-1, 0], [1, 0], [0, 1], [0, -1]] -        if i != 0 or j != 0: -            B.append((i, j)) -# print(B) -# O((V + E)log(V)) -# Cは [[スタート頂点, ゴール頂点, 重み], ...] のリスト -M = [[] for i in range(h * w)] -for i in range(h): -    for j in range(w): -        if S[i][j] == ".": -            for a in A: -                ai = i + a[0] -                aj = j + a[1] -                if 0 <= ai < h and 0 <= aj < w and S[ai][aj] == ".": -                    M[i * w + j].append((ai * w + aj, 0)) -            for b in B: -                bi = i + b[0] -                bj = j + b[1] -                if 0 <= bi < h and 0 <= bj < w and S[bi][bj] == ".": -                    M[i * w + j].append((bi * w + bj, 1)) -# for i in range(len(C)): -#     M[C[i][0]].append([C[i][1], C[i][2]])  # 無向グラフなら両方追加 -#     M[C[i][1]].append([C[i][0], C[i][2]])  # 無向グラフなら両方追加 -n = h * w -import heapq +        if (i != 0 or j != 0) and (not ([i, j] in A)): +            B.append([i, j]) +# for i in range(h): +#     for j in range(w): +#         if S[i][j] == ".": +#             for a in A: +#                 ai = i + a[i] +#                 aj = j + a[j] +#                 if 0 <= ai < h and 0 <= aj < w and S[ai][aj] == ".": +#                     M[i*w+j].append([ai*w+aj, 0]) +#             for a in B: +#                 ai = i + a[i] +#                 aj = j + a[j] +#                 if 0 <= ai < h and 0 <= aj < w and S[ai][aj] == ".": +#                     M[i*w+j].append([ai*w+aj, 1]) +from collections import deque -big = 10**9 -D = [big] * n  # 頂点0からの距離 -P = [-1] * n  # 頂点0からの最短距離において、そこの直前の頂点(経路復元に利用) -D[sx * w + sy] = 0 -V = [0] * n  # その頂点のD[i]が最短距離と確定したら1 -Q = []  # 優先度付きキュー -# for v in range(n): -#     heapq.heappush(Q, (D[v], v)) -heapq.heappush(Q, (0, sx * w + sy)) -# le = n -while len(Q) > 0: -    # print(Q,le) -    q = heapq.heappop(Q) -    u = q[1] -    du = q[0] -    if V[u] == 0: -        V[u] = 1 -        # le -= 1 -        for i in range(len(M[u])): -            v = M[u][i][0] -            luv = M[u][i][1] -            # print(v) -            if V[v] == 0: -                alt = du + luv -                if D[v] > alt: -                    D[v] = alt -                    P[v] = u -                    heapq.heappush(Q, (alt, v)) -                    # le += 1 -                    # print(Q) -# print(D) -ans = D[gx * w + gy] -if ans > 10**7: +D = deque() +V = [0] * n +DI = [float("inf")] * n +s = si * w + sj +g = gi * w + gj +DI[s] = 0 +D.append([s, 0]) +while len(D) > 0: +    d = D.popleft() +    x = d[0] +    we = d[1] +    if V[x] == 0: +        V[x] = 1 +        DI[x] = we +        i = x // w +        j = x % w +        for a in A: +            ai = i + a[0] +            aj = j + a[1] +            if 0 <= ai < h and 0 <= aj < w and S[ai][aj] == ".": +                D.appendleft([ai * w + aj, we]) +        for a in B: +            ai = i + a[0] +            aj = j + a[1] +            if 0 <= ai < h and 0 <= aj < w and S[ai][aj] == ".": +                D.append([ai * w + aj, we + 1]) +# print(DI) +# print(DI[g]) +ans = DI[g] +if DI[g] == float("inf"):