Buggy python submission (verdict: Time Limit Exceeded):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

H, W = MAP()
sy, sx = MAP()
gy, gx = MAP()
sx += 1
sy += 1
gx += 1
gy += 1
S = ["#"*(W+4)]*2+["##"+input()+"##" for _ in range(H)]+["#"*(W+4)]*2
dist = [[10**6]*(W+4) for _ in range(H+4)]
dist[sy][sx] = 0
q = deque()
q.append((sy, sx, 0))

while q:
    y, x, cnt = q.popleft()
    if dist[y][x] != cnt:
        continue
    for dx in range(-2, 3):
        for dy in range(-2, 3):
            ny = y+dy
            nx = x+dx
            if abs(dx)+abs(dy) > 1:
                if S[ny][nx] == "." and dist[ny][nx] >  dist[y][x]+1:
                    dist[ny][nx] = cnt + 1
                    q.append((ny, nx, cnt+1))
            else:
                if S[ny][nx] == "." and dist[ny][nx] > dist[y][x]:
                    dist[ny][nx] = cnt
                    q.appendleft((ny, nx, cnt))
# print(*dist, sep="\n")
print(dist[gy][gx] if dist[gy][gx] != 10**6 else -1)


Fixed submission:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

H, W = MAP()
sy, sx = MAP()
gy, gx = MAP()
sx += 1
sy += 1
gx += 1
gy += 1
S = ["#"*(W+4)]*2+["##"+input()+"##" for _ in range(H)]+["#"*(W+4)]*2
dist = [[10**6]*(W+4) for _ in range(H+4)]
dist[sy][sx] = 0
q = deque()
q.append((sy, sx, 0))

while q:
    y, x, cnt = q.popleft()
    if dist[y][x] != cnt:
        continue
    for dx in range(-2, 3):
        for dy in range(-2, 3):
            ny = y+dy
            nx = x+dx
            if abs(dx)+abs(dy) > 1:
                if S[ny][nx] == "." and dist[ny][nx] >  dist[y][x]+1:
                    dist[ny][nx] = cnt + 1
                    q.append((ny, nx, cnt+1))
            else:
                if S[ny][nx] == "." and dist[ny][nx] > dist[y][x]:
                    dist[ny][nx] = cnt
                    q.appendleft((ny, nx, cnt))
# print(*dist, sep="\n")
print(dist[gy][gx] if dist[gy][gx] != 10**6 else -1)