Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H, W = map(int, input().split())
Ch, Cw = map(int, input().split())
Dh, Dw = 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('##' + 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))
    

Fixed submission:
H, W = map(int, input().split())
Ch, Cw = map(int, input().split())
Dh, Dw = map(int, input().split())

Ch += 1
Cw += 1
Dh += 1
Dw += 1

start = (Ch, Cw)

S = ['#'*(W+4)]
S.append('#'*(W+4))
for k in range(H):
  S.append('##' + input() + '##')
S.append('#'*(W+4))
S.append('#'*(W+4))

counter = [[-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] == '.':
      counter[k][j] = 10**7

counter[Ch][Cw] = 0

#print(counter)
#print(S)

now = [start]
now_2 = [start]
while len(now_2) > 0:
  new = []
  for item in now_2:
    x = item[0]
    y = item[1]
    if counter[x+1][y] == 10**7:
      counter[x+1][y] = 0
      new.append((x+1, y))
    if counter[x][y+1] == 10**7:
      counter[x][y+1] = 0
      new.append((x, y+1))
    if counter[x-1][y] == 10**7:
      counter[x-1][y] = 0
      new.append((x-1, y))
    if counter[x][y-1] == 10**7:
      counter[x][y-1] = 0
      new.append((x, y-1))
  now += new
  now_2 = new
#print(now)
#print(counter)
if counter[Dh][Dw] == 0:
  print(0)
  exit()
ans = 0
while counter[Dh][Dw] == 10**7:
  if len(now) == 0:
    print(-1)
    exit() 
  nextlist = []
  ans += 1
  for item in now:
    x = item[0]
    y = item[1]
    if counter[x+2][y+2] > ans:
      counter[x+2][y+2] = ans
      nextlist.append((x+2, y+2))
    if counter[x+2][y+1] > ans:
      counter[x+2][y+1] = ans
      nextlist.append((x+2, y+1))
    if counter[x+2][y] > ans:
      counter[x+2][y] = ans
      nextlist.append((x+2, y))
    if counter[x+2][y-1] > ans:
      counter[x+2][y-1] = ans
      nextlist.append((x+2, y-1))
    if counter[x+2][y-2] > ans:
      counter[x+2][y-2] = ans
      nextlist.append((x+2, y-2))
    if counter[x+1][y+2] > ans:
      counter[x+1][y+2] = ans
      nextlist.append((x+1, y+2))
    if counter[x+1][y-2] > ans:
      counter[x+1][y-2] = ans
      nextlist.append((x+1, y-2))
    if counter[x][y+2] > ans:
      counter[x][y+2] = ans
      nextlist.append((x, y+2))
    if counter[x][y-2] > ans:
      counter[x][y-2] = ans
      nextlist.append((x, y-2))
    if counter[x-2][y+2] > ans:
      counter[x-2][y+2] = ans
      nextlist.append((x-2, y+2))
    if counter[x-2][y+1] > ans:
      counter[x-2][y+1] = ans
      nextlist.append((x-2, y+1))
    if counter[x-2][y] > ans:
      counter[x-2][y] = ans
      nextlist.append((x-2, y))
    if counter[x-2][y-1] > ans:
      counter[x-2][y-1] = ans
      nextlist.append((x-2, y-1))
    if counter[x-2][y-2] > ans:
      counter[x-2][y-2] = ans
      nextlist.append((x-2, y-2))
    if counter[x-1][y+2] > ans:
      counter[x-1][y+2] = ans
      nextlist.append((x-1, y+2))
    if counter[x-1][y-2] > ans:
      counter[x-1][y-2] = ans
      nextlist.append((x-1, y-2))
    if counter[x-1][y-1] > ans:
      counter[x-1][y-1] = ans
      nextlist.append((x-1, y-1))
    if counter[x-1][y+1] > ans:
      counter[x-1][y+1] = ans
      nextlist.append((x-1, y+1))
    if counter[x+1][y+1] > ans:
      counter[x+1][y+1] = ans
      nextlist.append((x+1, y+1))
    if counter[x+1][y-1] > ans:
      counter[x+1][y-1] = ans
      nextlist.append((x+1, y-1))
      
  
  now_2 = nextlist
  while len(now_2) > 0:
    new = []
    for item in now_2:
      x = item[0]
      y = item[1]
      if counter[x+1][y] == 10**7:
        counter[x+1][y] = 0
        new.append((x+1, y))
      if counter[x][y+1] == 10**7:
        counter[x][y+1] = 0
        new.append((x, y+1))
      if counter[x-1][y] == 10**7:
        counter[x-1][y] = 0
        new.append((x-1, y))
      if counter[x][y-1] == 10**7:
        counter[x][y-1] = 0
        new.append((x, y-1))
    nextlist += new
    now_2 = new
  now = nextlist
  #print(now)
  if ans > 10**7:
    print(-1)
    exit()
print(ans)