dhw = [(1,0),(0,-1),(0,1),(-1,0),

( 2,-2),( 2,-1),( 2, 0),( 2, 1),( 2, 2),

( 1,-2),( 1,-1),        ( 1, 1),( 1, 2),

( 0,-2),                        ( 0, 2),

(-1,-2),(-1,-1),        (-1, 1),(-1, 2),

(-2,-2),(-2,-1),(-2, 0),(-2, 1),(-2, 2)]



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

sh, sw = [int(x)-1 for x in input().split()]

eh, ew = [int(x)-1 for x in input().split()]

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



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

dst[sh][sw] = 0



from collections import deque

dq = deque([(sh, sw)])

while dq:

    h, w = dq.popleft()



    for i in range(4):

        nh = h + dhw[i][0]; nw = w + dhw[i][1]

        if nh < 0 or H <= nh : continue

        if nw < 0 or W <= nw : continue

        if S[nh][nw] == '#': continue

        if dst[nh][nw] == -1 or dst[h][w] < dst[nh][nw]:

            dst[nh][nw] = dst[h][w]

            dq.appendleft((nh, nw))



    for i in range(4, 24):

        nh = h + dhw[i][0]; nw = w + dhw[i][1]

        if nh < 0 or H <= nh : continue

        if nw < 0 or W <= nw : continue

        if S[nh][nw] == '#': continue

        if dst[nh][nw] == -1:

            dst[nh][nw] = dst[h][w] + 1

        elif dst[h][w] + 1 < dst[nh][nw]:

            dst[nh][nw] = min(dst[nh][nw], dst[h][w] + 1)

        else: continue

        dq.append((nh, nw))



print((dst[eh][ew]))

dhw = [(1,0),(0,-1),(0,1),(-1,0),

( 2,-2),( 2,-1),( 2, 0),( 2, 1),( 2, 2),

( 1,-2),( 1,-1),        ( 1, 1),( 1, 2),

( 0,-2),                        ( 0, 2),

(-1,-2),(-1,-1),        (-1, 1),(-1, 2),

(-2,-2),(-2,-1),(-2, 0),(-2, 1),(-2, 2)]



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

sh, sw = [int(x)-1 for x in input().split()]

eh, ew = [int(x)-1 for x in input().split()]

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



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



from collections import deque

dq = deque([(sh, sw, 0)])

while dq:

    h, w, d = dq.popleft()



    for i in range(4):

        nh = h + dhw[i][0]; nw = w + dhw[i][1]

        if nh < 0 or H <= nh : continue

        if nw < 0 or W <= nw : continue

        if S[nh][nw] == '#': continue

        if dst[nh][nw] == -1 or d < dst[nh][nw]:

            dq.appendleft((nh, nw, d))

            dst[nh][nw] = d



    for i in range(4, 24):

        nh = h + dhw[i][0]; nw = w + dhw[i][1]

        if nh < 0 or H <= nh : continue

        if nw < 0 or W <= nw : continue

        if S[nh][nw] == '#': continue

        if dst[nh][nw] >= 0: continue

        dq.append((nh, nw, d + 1))

        dst[nh][nw] = d + 1



print((dst[eh][ew]))

dhw = [
    (1, 0),
    (0, -1),
    (0, 1),
    (-1, 0),
    (2, -2),
    (2, -1),
    (2, 0),
    (2, 1),
    (2, 2),
    (1, -2),
    (1, -1),
    (1, 1),
    (1, 2),
    (0, -2),
    (0, 2),
    (-1, -2),
    (-1, -1),
    (-1, 1),
    (-1, 2),
    (-2, -2),
    (-2, -1),
    (-2, 0),
    (-2, 1),
    (-2, 2),
]
H, W = list(map(int, input().split()))
sh, sw = [int(x) - 1 for x in input().split()]
eh, ew = [int(x) - 1 for x in input().split()]
S = [eval(input()) for _ in range(H)]
dst = [[-1] * W for _ in range(H)]
dst[sh][sw] = 0
from collections import deque

dq = deque([(sh, sw)])
while dq:
    h, w = dq.popleft()
    for i in range(4):
        nh = h + dhw[i][0]
        nw = w + dhw[i][1]
        if nh < 0 or H <= nh:
            continue
        if nw < 0 or W <= nw:
            continue
        if S[nh][nw] == "#":
            continue
        if dst[nh][nw] == -1 or dst[h][w] < dst[nh][nw]:
            dst[nh][nw] = dst[h][w]
            dq.appendleft((nh, nw))
    for i in range(4, 24):
        nh = h + dhw[i][0]
        nw = w + dhw[i][1]
        if nh < 0 or H <= nh:
            continue
        if nw < 0 or W <= nw:
            continue
        if S[nh][nw] == "#":
            continue
        if dst[nh][nw] == -1:
            dst[nh][nw] = dst[h][w] + 1
        elif dst[h][w] + 1 < dst[nh][nw]:
            dst[nh][nw] = min(dst[nh][nw], dst[h][w] + 1)
        else:
            continue
        dq.append((nh, nw))
print((dst[eh][ew]))

dhw = [
    (1, 0),
    (0, -1),
    (0, 1),
    (-1, 0),
    (2, -2),
    (2, -1),
    (2, 0),
    (2, 1),
    (2, 2),
    (1, -2),
    (1, -1),
    (1, 1),
    (1, 2),
    (0, -2),
    (0, 2),
    (-1, -2),
    (-1, -1),
    (-1, 1),
    (-1, 2),
    (-2, -2),
    (-2, -1),
    (-2, 0),
    (-2, 1),
    (-2, 2),
]
H, W = list(map(int, input().split()))
sh, sw = [int(x) - 1 for x in input().split()]
eh, ew = [int(x) - 1 for x in input().split()]
S = [eval(input()) for _ in range(H)]
dst = [[-1] * W for _ in range(H)]
from collections import deque

dq = deque([(sh, sw, 0)])
while dq:
    h, w, d = dq.popleft()
    for i in range(4):
        nh = h + dhw[i][0]
        nw = w + dhw[i][1]
        if nh < 0 or H <= nh:
            continue
        if nw < 0 or W <= nw:
            continue
        if S[nh][nw] == "#":
            continue
        if dst[nh][nw] == -1 or d < dst[nh][nw]:
            dq.appendleft((nh, nw, d))
            dst[nh][nw] = d
    for i in range(4, 24):
        nh = h + dhw[i][0]
        nw = w + dhw[i][1]
        if nh < 0 or H <= nh:
            continue
        if nw < 0 or W <= nw:
            continue
        if S[nh][nw] == "#":
            continue
        if dst[nh][nw] >= 0:
            continue
        dq.append((nh, nw, d + 1))
        dst[nh][nw] = d + 1
print((dst[eh][ew]))

-dst[sh][sw] = 0 -dq = deque([(sh, sw)]) +dq = deque([(sh, sw, 0)]) -    h, w = dq.popleft() +    h, w, d = dq.popleft() -        if dst[nh][nw] == -1 or dst[h][w] < dst[nh][nw]: -            dst[nh][nw] = dst[h][w] -            dq.appendleft((nh, nw)) +        if dst[nh][nw] == -1 or d < dst[nh][nw]: +            dq.appendleft((nh, nw, d)) +            dst[nh][nw] = d -        if dst[nh][nw] == -1: -            dst[nh][nw] = dst[h][w] + 1 -        elif dst[h][w] + 1 < dst[nh][nw]: -            dst[nh][nw] = min(dst[nh][nw], dst[h][w] + 1) -        else: +        if dst[nh][nw] >= 0: -        dq.append((nh, nw)) +        dq.append((nh, nw, d + 1)) +        dst[nh][nw] = d + 1