from collections import deque

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

Ch, Cw = list(map(int, input().split()))

Dh, Dw = list(map(int, input().split()))



Ch += 1

Cw += 1

Dh += 1

Dw += 1



lim = 10**6



S = ['#'*(W+4)]

S.append('#'*(W+4))

for k in range(H):

  S.append('##' + eval(input()) + '##')

S.append('#'*(W+4))

S.append('#'*(W+4))



foo = [[-1 for _ in range(W+4)] for _ in range(H+4)]

for k in range(2, H+2):

  for j in range(2, W+2):

    if S[k][j] == '.':

      foo[k][j] = lim

    



foo[Ch][Cw] = 0



now = deque([])

new = deque([(Ch, Cw)])

while len(new) > 0:

  item = new.pop()

  now.append(item)

  x = item[0]

  y = item[1]

  if foo[x+1][y] == lim:

    foo[x+1][y] = 0

    new.append((x+1, y))

  if foo[x][y+1] == lim:

    foo[x][y+1] = 0

    new.append((x, y+1))

  if foo[x-1][y] == lim:

    foo[x-1][y] = 0

    new.append((x-1, y))

  if foo[x][y-1] == lim:

    foo[x][y-1] = 0

    new.append((x, y-1))





ans = 0

while foo[Dh][Dw] == lim:

  if len(now) == 0:

    print((-1))

    exit() 

  ans += 1

  while len(now) > 0:

    item = now.pop()

    x = item[0]

    y = item[1]

    if foo[x+2][y+2] > ans:

      foo[x+2][y+2] = ans

      new.append((x+2, y+2))

    if foo[x+2][y+1] > ans:

      foo[x+2][y+1] = ans

      new.append((x+2, y+1))

    if foo[x+2][y] > ans:

      foo[x+2][y] = ans

      new.append((x+2, y))

    if foo[x+2][y-1] > ans:

      foo[x+2][y-1] = ans

      new.append((x+2, y-1))

    if foo[x+2][y-2] > ans:

      foo[x+2][y-2] = ans

      new.append((x+2, y-2))

    if foo[x+1][y+2] > ans:

      foo[x+1][y+2] = ans

      new.append((x+1, y+2))

    if foo[x+1][y-2] > ans:

      foo[x+1][y-2] = ans

      new.append((x+1, y-2))

    if foo[x][y+2] > ans:

      foo[x][y+2] = ans

      new.append((x, y+2))

    if foo[x][y-2] > ans:

      foo[x][y-2] = ans

      new.append((x, y-2))

    if foo[x-2][y+2] > ans:

      foo[x-2][y+2] = ans

      new.append((x-2, y+2))

    if foo[x-2][y+1] > ans:

      foo[x-2][y+1] = ans

      new.append((x-2, y+1))

    if foo[x-2][y] > ans:

      foo[x-2][y] = ans

      new.append((x-2, y))

    if foo[x-2][y-1] > ans:

      foo[x-2][y-1] = ans

      new.append((x-2, y-1))

    if foo[x-2][y-2] > ans:

      foo[x-2][y-2] = ans

      new.append((x-2, y-2))

    if foo[x-1][y+2] > ans:

      foo[x-1][y+2] = ans

      new.append((x-1, y+2))

    if foo[x-1][y-2] > ans:

      foo[x-1][y-2] = ans

      new.append((x-1, y-2))

    if foo[x-1][y-1] > ans:

      foo[x-1][y-1] = ans

      new.append((x-1, y-1))

    if foo[x-1][y+1] > ans:

      foo[x-1][y+1] = ans

      new.append((x-1, y+1))

    if foo[x+1][y+1] > ans:

      foo[x+1][y+1] = ans

      new.append((x+1, y+1))

    if foo[x+1][y-1] > ans:

      foo[x+1][y-1] = ans

      new.append((x+1, y-1))

      

  while len(new) > 0:

    item = new.pop()

    now.append(item)

    x = item[0]

    y = item[1]

    if foo[x+1][y] == lim:

      foo[x+1][y] = 0

      new.append((x+1, y))

    if foo[x][y+1] == lim:

      foo[x][y+1] = 0

      new.append((x, y+1))

    if foo[x-1][y] == lim:

      foo[x-1][y] = 0

      new.append((x-1, y))

    if foo[x][y-1] == lim:

      foo[x][y-1] = 0

      new.append((x, y-1))

print(ans)
from collections import deque

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

Ch, Cw = list(map(int, input().split()))

Dh, Dw = list(map(int, input().split()))



Ch += 1

Cw += 1

Dh += 1

Dw += 1



lim = 10**6



S = ['#'*(W+4)]

S.append('#'*(W+4))

for k in range(H):

  S.append('##' + eval(input()) + '##')

S.append('#'*(W+4))

S.append('#'*(W+4))



foo = [[-1 for _ in range(W+4)] for _ in range(H+4)]

rev = [[-1 for _ in range(W+4)] for _ in range(H+4)]

for k in range(2, H+2):

  for j in range(2, W+2):

    if S[k][j] == '.':

      foo[k][j] = lim

      rev[k][j] = lim







foo[Ch][Cw] = 0

rev[Dh][Dw] = 0

jump = [(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)]

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

now = deque([])

new = deque([(Ch, Cw)])

while len(new) > 0:

  item = new.pop()

  now.append(item)

  x = item[0]

  y = item[1]

  for m in mv:

    mx = x + m[0]

    my = y + m[1]

    if rev[mx][my] == 0:

      print((0))

      exit()

    else:

      if foo[mx][my] == lim:

        foo[mx][my] = 0

        new.append((mx, my))



now_rev = deque([])

new_rev = deque([(Dh,Dw)])

while len(new_rev) > 0:

  item = new_rev.pop()

  now_rev.append(item)

  x = item[0]

  y = item[1]

  for m in mv:

    mx = x + m[0]

    my = y + m[1]

    if rev[mx][my] == lim:

      rev[mx][my] = 0

      new_rev.append((mx, my))





start = 0

while True:

  if len(now) == 0:

    print((-1))

    exit() 

  start += 1

  while len(now) > 0:

    item = now.pop()

    x = item[0]

    y = item[1]

    for j in jump:

      jx = x + j[0]

      jy = y + j[1]

      if 0 <= rev[jx][jy] < lim:

        print((foo[x][y]+rev[jx][jy]+1))

        exit()

      else:

        if foo[jx][jy] > start:

          foo[jx][jy] = start

          new.append((jx,jy))

  while len(new) > 0:

    item = new.pop()

    now.append(item)

    x = item[0]

    y = item[1]

    for m in mv:

      mx = x + m[0]

      my = y + m[1]

      if 0 <= rev[mx][my] < lim:

        print((foo[x][y] + rev[mx][my]))

        exit()

      else:

        if foo[mx][my] == lim:

          foo[mx][my] = start

          new.append((mx, my))



  if len(now_rev) == 0:

    print((-1))

    exit() 

  while len(now_rev) > 0:

    item = now_rev.pop()

    x = item[0]

    y = item[1]

    for j in jump:

      jx = x + j[0]

      jy = y + j[1]

      if 0 <= foo[jx][jy] < lim:

        print((rev[x][y]+foo[jx][jy]+1))

        exit()

      else:

        if rev[jx][jy] > start:

          rev[jx][jy] = start

          new_rev.append((jx,jy))



  while len(new_rev) > 0:

    item = new_rev.pop()

    now_rev.append(item)

    x = item[0]

    y = item[1]

    for m in mv:

      mx = x + m[0]

      my = y + m[1]

      if 0 <= foo[mx][my] < lim:

        print((rev[x][y] + foo[mx][my]))

        exit()

      else:

        if rev[mx][my] == lim:

          rev[mx][my] = start

          new_rev.append((mx, my))

    
from collections import deque

H, W = list(map(int, input().split()))
Ch, Cw = list(map(int, input().split()))
Dh, Dw = list(map(int, input().split()))
Ch += 1
Cw += 1
Dh += 1
Dw += 1
lim = 10**6
S = ["#" * (W + 4)]
S.append("#" * (W + 4))
for k in range(H):
    S.append("##" + eval(input()) + "##")
S.append("#" * (W + 4))
S.append("#" * (W + 4))
foo = [[-1 for _ in range(W + 4)] for _ in range(H + 4)]
for k in range(2, H + 2):
    for j in range(2, W + 2):
        if S[k][j] == ".":
            foo[k][j] = lim
foo[Ch][Cw] = 0
now = deque([])
new = deque([(Ch, Cw)])
while len(new) > 0:
    item = new.pop()
    now.append(item)
    x = item[0]
    y = item[1]
    if foo[x + 1][y] == lim:
        foo[x + 1][y] = 0
        new.append((x + 1, y))
    if foo[x][y + 1] == lim:
        foo[x][y + 1] = 0
        new.append((x, y + 1))
    if foo[x - 1][y] == lim:
        foo[x - 1][y] = 0
        new.append((x - 1, y))
    if foo[x][y - 1] == lim:
        foo[x][y - 1] = 0
        new.append((x, y - 1))
ans = 0
while foo[Dh][Dw] == lim:
    if len(now) == 0:
        print((-1))
        exit()
    ans += 1
    while len(now) > 0:
        item = now.pop()
        x = item[0]
        y = item[1]
        if foo[x + 2][y + 2] > ans:
            foo[x + 2][y + 2] = ans
            new.append((x + 2, y + 2))
        if foo[x + 2][y + 1] > ans:
            foo[x + 2][y + 1] = ans
            new.append((x + 2, y + 1))
        if foo[x + 2][y] > ans:
            foo[x + 2][y] = ans
            new.append((x + 2, y))
        if foo[x + 2][y - 1] > ans:
            foo[x + 2][y - 1] = ans
            new.append((x + 2, y - 1))
        if foo[x + 2][y - 2] > ans:
            foo[x + 2][y - 2] = ans
            new.append((x + 2, y - 2))
        if foo[x + 1][y + 2] > ans:
            foo[x + 1][y + 2] = ans
            new.append((x + 1, y + 2))
        if foo[x + 1][y - 2] > ans:
            foo[x + 1][y - 2] = ans
            new.append((x + 1, y - 2))
        if foo[x][y + 2] > ans:
            foo[x][y + 2] = ans
            new.append((x, y + 2))
        if foo[x][y - 2] > ans:
            foo[x][y - 2] = ans
            new.append((x, y - 2))
        if foo[x - 2][y + 2] > ans:
            foo[x - 2][y + 2] = ans
            new.append((x - 2, y + 2))
        if foo[x - 2][y + 1] > ans:
            foo[x - 2][y + 1] = ans
            new.append((x - 2, y + 1))
        if foo[x - 2][y] > ans:
            foo[x - 2][y] = ans
            new.append((x - 2, y))
        if foo[x - 2][y - 1] > ans:
            foo[x - 2][y - 1] = ans
            new.append((x - 2, y - 1))
        if foo[x - 2][y - 2] > ans:
            foo[x - 2][y - 2] = ans
            new.append((x - 2, y - 2))
        if foo[x - 1][y + 2] > ans:
            foo[x - 1][y + 2] = ans
            new.append((x - 1, y + 2))
        if foo[x - 1][y - 2] > ans:
            foo[x - 1][y - 2] = ans
            new.append((x - 1, y - 2))
        if foo[x - 1][y - 1] > ans:
            foo[x - 1][y - 1] = ans
            new.append((x - 1, y - 1))
        if foo[x - 1][y + 1] > ans:
            foo[x - 1][y + 1] = ans
            new.append((x - 1, y + 1))
        if foo[x + 1][y + 1] > ans:
            foo[x + 1][y + 1] = ans
            new.append((x + 1, y + 1))
        if foo[x + 1][y - 1] > ans:
            foo[x + 1][y - 1] = ans
            new.append((x + 1, y - 1))
    while len(new) > 0:
        item = new.pop()
        now.append(item)
        x = item[0]
        y = item[1]
        if foo[x + 1][y] == lim:
            foo[x + 1][y] = 0
            new.append((x + 1, y))
        if foo[x][y + 1] == lim:
            foo[x][y + 1] = 0
            new.append((x, y + 1))
        if foo[x - 1][y] == lim:
            foo[x - 1][y] = 0
            new.append((x - 1, y))
        if foo[x][y - 1] == lim:
            foo[x][y - 1] = 0
            new.append((x, y - 1))
print(ans)

from collections import deque

H, W = list(map(int, input().split()))
Ch, Cw = list(map(int, input().split()))
Dh, Dw = list(map(int, input().split()))
Ch += 1
Cw += 1
Dh += 1
Dw += 1
lim = 10**6
S = ["#" * (W + 4)]
S.append("#" * (W + 4))
for k in range(H):
    S.append("##" + eval(input()) + "##")
S.append("#" * (W + 4))
S.append("#" * (W + 4))
foo = [[-1 for _ in range(W + 4)] for _ in range(H + 4)]
rev = [[-1 for _ in range(W + 4)] for _ in range(H + 4)]
for k in range(2, H + 2):
    for j in range(2, W + 2):
        if S[k][j] == ".":
            foo[k][j] = lim
            rev[k][j] = lim
foo[Ch][Cw] = 0
rev[Dh][Dw] = 0
jump = [
    (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),
]
mv = [(1, 0), (-1, 0), (0, 1), (0, -1)]
now = deque([])
new = deque([(Ch, Cw)])
while len(new) > 0:
    item = new.pop()
    now.append(item)
    x = item[0]
    y = item[1]
    for m in mv:
        mx = x + m[0]
        my = y + m[1]
        if rev[mx][my] == 0:
            print((0))
            exit()
        else:
            if foo[mx][my] == lim:
                foo[mx][my] = 0
                new.append((mx, my))
now_rev = deque([])
new_rev = deque([(Dh, Dw)])
while len(new_rev) > 0:
    item = new_rev.pop()
    now_rev.append(item)
    x = item[0]
    y = item[1]
    for m in mv:
        mx = x + m[0]
        my = y + m[1]
        if rev[mx][my] == lim:
            rev[mx][my] = 0
            new_rev.append((mx, my))
start = 0
while True:
    if len(now) == 0:
        print((-1))
        exit()
    start += 1
    while len(now) > 0:
        item = now.pop()
        x = item[0]
        y = item[1]
        for j in jump:
            jx = x + j[0]
            jy = y + j[1]
            if 0 <= rev[jx][jy] < lim:
                print((foo[x][y] + rev[jx][jy] + 1))
                exit()
            else:
                if foo[jx][jy] > start:
                    foo[jx][jy] = start
                    new.append((jx, jy))
    while len(new) > 0:
        item = new.pop()
        now.append(item)
        x = item[0]
        y = item[1]
        for m in mv:
            mx = x + m[0]
            my = y + m[1]
            if 0 <= rev[mx][my] < lim:
                print((foo[x][y] + rev[mx][my]))
                exit()
            else:
                if foo[mx][my] == lim:
                    foo[mx][my] = start
                    new.append((mx, my))
    if len(now_rev) == 0:
        print((-1))
        exit()
    while len(now_rev) > 0:
        item = now_rev.pop()
        x = item[0]
        y = item[1]
        for j in jump:
            jx = x + j[0]
            jy = y + j[1]
            if 0 <= foo[jx][jy] < lim:
                print((rev[x][y] + foo[jx][jy] + 1))
                exit()
            else:
                if rev[jx][jy] > start:
                    rev[jx][jy] = start
                    new_rev.append((jx, jy))
    while len(new_rev) > 0:
        item = new_rev.pop()
        now_rev.append(item)
        x = item[0]
        y = item[1]
        for m in mv:
            mx = x + m[0]
            my = y + m[1]
            if 0 <= foo[mx][my] < lim:
                print((rev[x][y] + foo[mx][my]))
                exit()
            else:
                if rev[mx][my] == lim:
                    rev[mx][my] = start
                    new_rev.append((mx, my))

+rev = [[-1 for _ in range(W + 4)] for _ in range(H + 4)] +            rev[k][j] = lim +rev[Dh][Dw] = 0 +jump = [ +    (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), +] +mv = [(1, 0), (-1, 0), (0, 1), (0, -1)] -    if foo[x + 1][y] == lim: -        foo[x + 1][y] = 0 -        new.append((x + 1, y)) -    if foo[x][y + 1] == lim: -        foo[x][y + 1] = 0 -        new.append((x, y + 1)) -    if foo[x - 1][y] == lim: -        foo[x - 1][y] = 0 -        new.append((x - 1, y)) -    if foo[x][y - 1] == lim: -        foo[x][y - 1] = 0 -        new.append((x, y - 1)) -ans = 0 -while foo[Dh][Dw] == lim: +    for m in mv: +        mx = x + m[0] +        my = y + m[1] +        if rev[mx][my] == 0: +            print((0)) +            exit() +        else: +            if foo[mx][my] == lim: +                foo[mx][my] = 0 +                new.append((mx, my)) +now_rev = deque([]) +new_rev = deque([(Dh, Dw)]) +while len(new_rev) > 0: +    item = new_rev.pop() +    now_rev.append(item) +    x = item[0] +    y = item[1] +    for m in mv: +        mx = x + m[0] +        my = y + m[1] +        if rev[mx][my] == lim: +            rev[mx][my] = 0 +            new_rev.append((mx, my)) +start = 0 +while True: -    ans += 1 +    start += 1 -        if foo[x + 2][y + 2] > ans: -            foo[x + 2][y + 2] = ans -            new.append((x + 2, y + 2)) -        if foo[x + 2][y + 1] > ans: -            foo[x + 2][y + 1] = ans -            new.append((x + 2, y + 1)) -        if foo[x + 2][y] > ans: -            foo[x + 2][y] = ans -            new.append((x + 2, y)) -        if foo[x + 2][y - 1] > ans: -            foo[x + 2][y - 1] = ans -            new.append((x + 2, y - 1)) -        if foo[x + 2][y - 2] > ans: -            foo[x + 2][y - 2] = ans -            new.append((x + 2, y - 2)) -        if foo[x + 1][y + 2] > ans: -            foo[x + 1][y + 2] = ans -            new.append((x + 1, y + 2)) -        if foo[x + 1][y - 2] > ans: -            foo[x + 1][y - 2] = ans -            new.append((x + 1, y - 2)) -        if foo[x][y + 2] > ans: -            foo[x][y + 2] = ans -            new.append((x, y + 2)) -        if foo[x][y - 2] > ans: -            foo[x][y - 2] = ans -            new.append((x, y - 2)) -        if foo[x - 2][y + 2] > ans: -            foo[x - 2][y + 2] = ans -            new.append((x - 2, y + 2)) -        if foo[x - 2][y + 1] > ans: -            foo[x - 2][y + 1] = ans -            new.append((x - 2, y + 1)) -        if foo[x - 2][y] > ans: -            foo[x - 2][y] = ans -            new.append((x - 2, y)) -        if foo[x - 2][y - 1] > ans: -            foo[x - 2][y - 1] = ans -            new.append((x - 2, y - 1)) -        if foo[x - 2][y - 2] > ans: -            foo[x - 2][y - 2] = ans -            new.append((x - 2, y - 2)) -        if foo[x - 1][y + 2] > ans: -            foo[x - 1][y + 2] = ans -            new.append((x - 1, y + 2)) -        if foo[x - 1][y - 2] > ans: -            foo[x - 1][y - 2] = ans -            new.append((x - 1, y - 2)) -        if foo[x - 1][y - 1] > ans: -            foo[x - 1][y - 1] = ans -            new.append((x - 1, y - 1)) -        if foo[x - 1][y + 1] > ans: -            foo[x - 1][y + 1] = ans -            new.append((x - 1, y + 1)) -        if foo[x + 1][y + 1] > ans: -            foo[x + 1][y + 1] = ans -            new.append((x + 1, y + 1)) -        if foo[x + 1][y - 1] > ans: -            foo[x + 1][y - 1] = ans -            new.append((x + 1, y - 1)) +        for j in jump: +            jx = x + j[0] +            jy = y + j[1] +            if 0 <= rev[jx][jy] < lim: +                print((foo[x][y] + rev[jx][jy] + 1)) +                exit() +            else: +                if foo[jx][jy] > start: +                    foo[jx][jy] = start +                    new.append((jx, jy)) -        if foo[x + 1][y] == lim: -            foo[x + 1][y] = 0 -            new.append((x + 1, y)) -        if foo[x][y + 1] == lim: -            foo[x][y + 1] = 0 -            new.append((x, y + 1)) -        if foo[x - 1][y] == lim: -            foo[x - 1][y] = 0 -            new.append((x - 1, y)) -        if foo[x][y - 1] == lim: -            foo[x][y - 1] = 0 -            new.append((x, y - 1)) -print(ans) +        for m in mv: +            mx = x + m[0] +            my = y + m[1] +            if 0 <= rev[mx][my] < lim: +                print((foo[x][y] + rev[mx][my])) +                exit() +            else: +                if foo[mx][my] == lim: +                    foo[mx][my] = start +                    new.append((mx, my)) +    if len(now_rev) == 0: +        print((-1)) +        exit() +    while len(now_rev) > 0: +        item = now_rev.pop() +        x = item[0] +        y = item[1] +        for j in jump: +            jx = x + j[0] +            jy = y + j[1] +            if 0 <= foo[jx][jy] < lim: +                print((rev[x][y] + foo[jx][jy] + 1)) +                exit() +            else: +                if rev[jx][jy] > start: +                    rev[jx][jy] = start +                    new_rev.append((jx, jy)) +    while len(new_rev) > 0: +        item = new_rev.pop() +        now_rev.append(item) +        x = item[0] +        y = item[1] +        for m in mv: +            mx = x + m[0] +            my = y + m[1] +            if 0 <= foo[mx][my] < lim: +                print((rev[x][y] + foo[mx][my])) +                exit() +            else: +                if rev[mx][my] == lim: +                    rev[mx][my] = start +                    new_rev.append((mx, my))