Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys

H, W = map(int, input().split())
c_h, c_w = map(int, input().split())
d_h, d_w = map(int, input().split())
c_h -= 1
c_w -= 1
d_h -= 1
d_w -= 1
maze = []

for i in range(H):
    maze.append(list(input()))

check = [[-1 for i in range(W)] for j in range(H)]
number = 0

def bfs(sy, sx):
    global number
    check[sy][sx] = number
    d = deque([[sy, sx]])
    
    while d:
        y, x = d.popleft()
        
        for k, l in ([1, 0], [0, 1], [-1, 0], [0, -1]):
            new_y, new_x = y + k, x + l
            
            if new_y < 0 or H <= new_y or new_x < 0 or W <= new_x:
                continue
            
            if check[new_y][new_x] != -1:
                continue
            
            if maze[new_y][new_x] == '#':
                continue
            
            d.append([new_y, new_x])
            check[new_y][new_x] = number
    
    number += 1
    
for i in range(H):
    for j in range(W):
        if check[i][j] == -1 and maze[i][j] == '.':
            bfs(i, j)


load = [[] for i in range(number)]
for i in range(H):
    for j in range(W):
        
        if check[i][j] != -1:
            
            for k in range(-2, 3):
                for l in range(-2, 3):
                    new_y, new_x = i + k, j + l
                    
                    if new_y < 0 or H <= new_y or new_x < 0 or W <= new_x:
                        continue
                    
                    f_num = check[i][j]
                    to_num = check[new_y][new_x]
                    
                    if to_num != -1 and to_num != f_num:
                        if not to_num in load[f_num]:
                            load[f_num].append(to_num)
                            load[to_num].append(f_num)

s_num = check[c_h][c_w]
g_num = check[d_h][d_w]

if s_num == g_num:
    print(0)
    sys.exit()

dp = [-1] * number
dp[s_num] = 0

d = deque([s_num])
ans = -1
flg = False
while d:
    if flg:
        break
    
    num = d.popleft()
    
    for i in load[num]:
        if i == g_num:
            ans = dp[num] + 1
            flg = True
            break
        
        if dp[i] != -1:
            continue
            
        dp[i] = dp[num] + 1
        d.append(i)
        
print(ans)

Fixed submission:
from collections import deque
import sys

H, W = map(int, input().split())
c_h, c_w = map(int, input().split())
d_h, d_w = map(int, input().split())
c_h -= 1
c_w -= 1
d_h -= 1
d_w -= 1
maze = []

for i in range(H):
    maze.append(list(input()))

check = [[-1 for i in range(W)] for j in range(H)]
number = 0

def bfs(sy, sx):
    global number
    check[sy][sx] = number
    d = deque([[sy, sx]])
    
    while d:
        y, x = d.popleft()
        
        for k, l in ([1, 0], [0, 1], [-1, 0], [0, -1]):
            new_y, new_x = y + k, x + l
            
            if new_y < 0 or H <= new_y or new_x < 0 or W <= new_x:
                continue
            
            if check[new_y][new_x] != -1:
                continue
            
            if maze[new_y][new_x] == '#':
                continue
            
            d.append([new_y, new_x])
            check[new_y][new_x] = number
    
    number += 1
    
for i in range(H):
    for j in range(W):
        if check[i][j] == -1 and maze[i][j] == '.':
            bfs(i, j)


load = [[] for i in range(number)]
for i in range(H):
    for j in range(W):
        
        if check[i][j] != -1:
            
            for k in range(-2, 3):
                for l in range(-2, 3):
                    new_y, new_x = i + k, j + l
                    
                    if new_y < 0 or H <= new_y or new_x < 0 or W <= new_x:
                        continue
                    
                    f_num = check[i][j]
                    to_num = check[new_y][new_x]
                    
                    if to_num != -1 and to_num != f_num:
                        if not to_num in load[f_num]:
                            load[f_num].append(to_num)
                            load[to_num].append(f_num)

s_num = check[c_h][c_w]
g_num = check[d_h][d_w]

if s_num == g_num:
    print(0)
    sys.exit()

dp = [-1] * number
dp[s_num] = 0

d = deque([s_num])
ans = -1
flg = False
while d:
    if flg:
        break
    
    num = d.popleft()
    
    for i in load[num]:
        if i == g_num:
            ans = dp[num] + 1
            flg = True
            break
        
        if dp[i] != -1:
            continue
            
        dp[i] = dp[num] + 1
        d.append(i)
        
print(ans)