Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)
# INF =  float("inf")
INF = 10**18
import bisect
import statistics
mod = 10**9+7
# mod = 998244353

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

S = []
for i in range(H):
    S.append(list(input()))

visited = [[0 for j in range(W)] for i in range(H)]
seen = [[0 for j in range(W)] for i in range(H)]
done = []

def bfs(u):
    stack = deque([u])
    seen[u[0]][u[1]] = 1

    while len(stack) > 0:
        v = stack.popleft()
        done.append(v)
        visited[v[0]][v[1]] = 1
        ###
        w1 = (v[0] + 1, v[1])
        w2 = (v[0] , v[1] + 1)
        w3 = (v[0] - 1, v[1])
        w4 = (v[0] , v[1] -1)
        ###

        ws = [w1,w2,w3,w4]

        for w in ws:
            if w[0] >= 0 and w[0] < H and w[1] >= 0 and w[1] < W:
                if S[w[0]][w[1]] == ".":
                    if seen[w[0]][w[1]] == 0:
                        stack.append(w)
                        seen[w[0]][w[1]] = 1

bfs((Ch-1,Cw-1))

count = 0


while True:
    if visited[Dh-1][Dw-1] == 1:
        print(count)
        sys.exit()
    else:
        count += 1
        pp = deque([])
        for d in done:
            i = d[0]
            j = d[1]
            for k in range(-2,3):
                for l in range(-2,3):
                    if i+k >= 0 and i+k < H and j+l >= 0 and j+l < W:
                        if S[i+k][j+l] == ".":
                            if visited[i+k][j+l] == 0:
                                pp.append((i+k,j+l))

        if len(pp) == 0:
            print(-1)
            break

        for ppp in pp:
            bfs(ppp)












Fixed submission:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)
# INF =  float("inf")
INF = 10**18
import bisect
import statistics
mod = 10**9+7
# mod = 998244353

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

S = []
for i in range(H):
    S.append(list(input()))

visited = [[0 for j in range(W)] for i in range(H)]
seen = [[0 for j in range(W)] for i in range(H)]
done = set()

def bfs(u, done):
    stack = deque([u])
    seen[u[0]][u[1]] = 1

    while len(stack) > 0:
        v = stack.popleft()
        done.add(v)
        visited[v[0]][v[1]] = 1
        ###
        w1 = (v[0] + 1, v[1])
        w2 = (v[0] , v[1] + 1)
        w3 = (v[0] - 1, v[1])
        w4 = (v[0] , v[1] -1)
        ###

        ws = [w1,w2,w3,w4]

        for w in ws:
            if w[0] >= 0 and w[0] < H and w[1] >= 0 and w[1] < W:
                if S[w[0]][w[1]] == ".":
                    if seen[w[0]][w[1]] == 0:
                        stack.append(w)
                        seen[w[0]][w[1]] = 1

bfs((Ch-1,Cw-1), done)

count = 0
# print("d",done)
# for i in range(H):
#     print(visited[i])

while True:
    if visited[Dh-1][Dw-1] == 1:
        print(count)
        sys.exit()
    else:
        count += 1
        pp = deque([])
        for d in list(done):
            i = d[0]
            j = d[1]
            for k in range(-2,3):
                for l in range(-2,3):
                    if i+k >= 0 and i+k < H and j+l >= 0 and j+l < W:
                        if S[i+k][j+l] == ".":
                            if visited[i+k][j+l] == 0:
                                pp.append((i+k,j+l))

        if len(pp) == 0:
            print(-1)
            break

        done = set()
        for ppp in pp:
            bfs(ppp, done)