Buggy python submission (verdict: Time Limit Exceeded):
# D - Wizard in Maze

import heapq

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([c for c in input()])

queue = [(0, ch - 1, cw - 1)]  # (dist, y, x)

while len(queue) > 0:
    dist, y, x = heapq.heappop(queue)
    if s[y][x] != '.':
        continue
    s[y][x] = dist
    for vy in range(-2, 3):
        for vx in range(-2, 3):
            if 0 <= y + vy < h and 0 <= x + vx < w and \
               s[y + vy][x + vx] == '.':
                cost = 0 if abs(vx) + abs(vy) <= 1 else 1
                heapq.heappush(queue, (dist + cost, y + vy, x + vx))

dist = s[dh - 1][dw - 1]
print(dist if dist != '.' else -1)


Fixed submission:
# D - Wizard in Maze

from collections import deque

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([c for c in input()])

def dfs(y, x, area_id):
    stack = [(y, x)]
    while len(stack) > 0:
        y, x = stack.pop()
        if s[y][x] == '.':
            s[y][x] = area_id
            for vy, vx in [(-1, 0), (0, 1), (1, 0), (0, -1)]:
                if (0 <= x + vx < w and 0 <= y + vy < h and
                    s[y + vy][x + vx] == '.'):
                    stack.append((y + vy, x + vx))

n_area = 0
for y in range(h):
    for x in range(w):
        if s[y][x] == '.':
            dfs(y, x, n_area)
            n_area += 1

# links between areas
adj = [set() for i in range(n_area)]
for y in range(h):
    for x in range(w):
        if s[y][x] == '#':
            continue
        for vy in range(0, 3):
            for vx in range(-2, 3):
                if (0 <= x + vx < w and 0 <= y + vy < h and
                    s[y + vy][x + vx] != '#'):
                    adj[s[y][x]].add(s[y + vy][x + vx])
                    adj[s[y + vy][x + vx]].add(s[y][x])

c = s[ch - 1][cw - 1]
d = s[dh - 1][dw - 1]
dist = [float('inf') for i in range(n_area)]
dist[c] = 0

q = deque()
q.append((c, 0))
while len(q) > 0:
    c, cost = q.popleft()
    for x in adj[c]:
        if dist[x] > cost + 1:
            dist[x] = cost + 1
            q.append((x, cost + 1))

print(dist[d] if dist[d] < float('inf') else -1)

Buggy python submission (verdict: Time Limit Exceeded):
# D - Wizard in Maze

import heapq

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([c for c in input()])

queue = [(0, ch - 1, cw - 1)]  # (dist, y, x)

while len(queue) > 0:
    dist, y, x = heapq.heappop(queue)
    if s[y][x] != '.':
        continue
    s[y][x] = dist
    for vy in range(-2, 3):
        for vx in range(-2, 3):
            if 0 <= y + vy < h and 0 <= x + vx < w:
                cost = 0 if abs(vx) + abs(vy) <= 1 else 1
                heapq.heappush(queue, (dist + cost, y + vy, x + vx))

dist = s[dh - 1][dw - 1]
print(dist if dist != '.' else -1)


Fixed submission:
# D - Wizard in Maze

from collections import deque

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([c for c in input()])

def dfs(y, x, area_id):
    stack = [(y, x)]
    while len(stack) > 0:
        y, x = stack.pop()
        if s[y][x] == '.':
            s[y][x] = area_id
            for vy, vx in [(-1, 0), (0, 1), (1, 0), (0, -1)]:
                if (0 <= x + vx < w and 0 <= y + vy < h and
                    s[y + vy][x + vx] == '.'):
                    stack.append((y + vy, x + vx))

n_area = 0
for y in range(h):
    for x in range(w):
        if s[y][x] == '.':
            dfs(y, x, n_area)
            n_area += 1

# links between areas
adj = [set() for i in range(n_area)]
for y in range(h):
    for x in range(w):
        if s[y][x] == '#':
            continue
        for vy in range(0, 3):
            for vx in range(-2, 3):
                if (0 <= x + vx < w and 0 <= y + vy < h and
                    s[y + vy][x + vx] != '#'):
                    adj[s[y][x]].add(s[y + vy][x + vx])
                    adj[s[y + vy][x + vx]].add(s[y][x])

c = s[ch - 1][cw - 1]
d = s[dh - 1][dw - 1]
dist = [float('inf') for i in range(n_area)]
dist[c] = 0

q = deque()
q.append((c, 0))
while len(q) > 0:
    c, cost = q.popleft()
    for x in adj[c]:
        if dist[x] > cost + 1:
            dist[x] = cost + 1
            q.append((x, cost + 1))

print(dist[d] if dist[d] < float('inf') else -1)