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

t = [[i for i in range(w)] for j in range(h)]

visited = [[0 for i in range(w)] for j in range(h)] # いったところ

ans = 0

for i in range(h):

  temp = eval(input())

  for j in range(w):

    t[i][j] = temp[j]



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

ans = 0

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



while True:

  # 移動A

  stemp = set()

  while len(d) != 0:

    Th, Tw = d.popleft()

    # s.add((Th, Tw))

    stemp.add((Th, Tw))

    if (Th,Tw) == (Dh, Dw):

      print(ans)

      exit()

    for i, j in a:

      _h,_w = Th + i, Tw + j

      _p = (_h, _w)

      if 0 <= _h < h and 0 <= _w < w and t[_h][_w] == '.' and visited[_h][_w] == 0:

        d.append(_p)

        visited[_h][_w] = 1

  # 移動B

  ans += 1

  for Th,Tw in stemp:

    for i in range(-2, 3) :

      for j in range(-2, 3) :

        _h,_w = Th + i, Tw + j

        _p = (_h, _w)

        if 0 <= _h < h and 0 <=  _w < w and t[_h][_w] == '.' and visited[_h][_w] == 0:

          d.append(_p)

          visited[_h][_w] = 1

  if len(d) == 0:

    print('-1')

    exit()

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

visited = [[0 for i in range(w)] for j in range(h)] # いったところ

t = [list(eval(input())) for _ in range(h)]

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

d2 = deque()

ans = 0

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

b = list(range(-2, 3))



while True:

  # 移動A

  while len(d) != 0:

    Th, Tw = d.popleft()

    d2.append((Th, Tw))

    if (Th,Tw) == (Dh, Dw):

      print(ans)

      exit()

    for i, j in a:

      _h,_w = Th + i, Tw + j

      _p = (_h, _w)

      if 0 <= _h < h and 0 <= _w < w and t[_h][_w] == '.' and visited[_h][_w] == 0:

        d.append(_p)

        visited[_h][_w] = 1

  # 移動B

  ans += 1

  while len(d2) != 0:

    Th, Tw = d2.popleft()

    for i in b :

      for j in b :

        _h,_w = Th + i, Tw + j

        _p = (_h, _w)

        if 0 <= _h < h and 0 <=  _w < w and t[_h][_w] == '.' and visited[_h][_w] == 0:

          d.append(_p)

          visited[_h][_w] = 1

  if len(d) == 0:

    print('-1')

    exit()

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
t = [[i for i in range(w)] for j in range(h)]
visited = [[0 for i in range(w)] for j in range(h)]  # いったところ
ans = 0
for i in range(h):
    temp = eval(input())
    for j in range(w):
        t[i][j] = temp[j]
d = deque([(Ch, Cw)])
ans = 0
a = [(1, 0), (-1, 0), (0, 1), (0, -1)]
while True:
    # 移動A
    stemp = set()
    while len(d) != 0:
        Th, Tw = d.popleft()
        # s.add((Th, Tw))
        stemp.add((Th, Tw))
        if (Th, Tw) == (Dh, Dw):
            print(ans)
            exit()
        for i, j in a:
            _h, _w = Th + i, Tw + j
            _p = (_h, _w)
            if (
                0 <= _h < h
                and 0 <= _w < w
                and t[_h][_w] == "."
                and visited[_h][_w] == 0
            ):
                d.append(_p)
                visited[_h][_w] = 1
    # 移動B
    ans += 1
    for Th, Tw in stemp:
        for i in range(-2, 3):
            for j in range(-2, 3):
                _h, _w = Th + i, Tw + j
                _p = (_h, _w)
                if (
                    0 <= _h < h
                    and 0 <= _w < w
                    and t[_h][_w] == "."
                    and visited[_h][_w] == 0
                ):
                    d.append(_p)
                    visited[_h][_w] = 1
    if len(d) == 0:
        print("-1")
        exit()

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
visited = [[0 for i in range(w)] for j in range(h)]  # いったところ
t = [list(eval(input())) for _ in range(h)]
d = deque([(Ch, Cw)])
d2 = deque()
ans = 0
a = [(1, 0), (-1, 0), (0, 1), (0, -1)]
b = list(range(-2, 3))
while True:
    # 移動A
    while len(d) != 0:
        Th, Tw = d.popleft()
        d2.append((Th, Tw))
        if (Th, Tw) == (Dh, Dw):
            print(ans)
            exit()
        for i, j in a:
            _h, _w = Th + i, Tw + j
            _p = (_h, _w)
            if (
                0 <= _h < h
                and 0 <= _w < w
                and t[_h][_w] == "."
                and visited[_h][_w] == 0
            ):
                d.append(_p)
                visited[_h][_w] = 1
    # 移動B
    ans += 1
    while len(d2) != 0:
        Th, Tw = d2.popleft()
        for i in b:
            for j in b:
                _h, _w = Th + i, Tw + j
                _p = (_h, _w)
                if (
                    0 <= _h < h
                    and 0 <= _w < w
                    and t[_h][_w] == "."
                    and visited[_h][_w] == 0
                ):
                    d.append(_p)
                    visited[_h][_w] = 1
    if len(d) == 0:
        print("-1")
        exit()

-t = [[i for i in range(w)] for j in range(h)] -ans = 0 -for i in range(h): -    temp = eval(input()) -    for j in range(w): -        t[i][j] = temp[j] +t = [list(eval(input())) for _ in range(h)] +d2 = deque() +b = list(range(-2, 3)) -    stemp = set() -        # s.add((Th, Tw)) -        stemp.add((Th, Tw)) +        d2.append((Th, Tw)) -    for Th, Tw in stemp: -        for i in range(-2, 3): -            for j in range(-2, 3): +    while len(d2) != 0: +        Th, Tw = d2.popleft() +        for i in b: +            for j in b: