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

H, W = map(int, input().split())
C_h, C_w = map(int, input().split())
D_h, D_w = map(int, input().split())
S = []
for i in range(H):
    S.append(input())


def exists(h, w):
    if h < 0 or h >= H or w < 0 or w >= W:
        return False
    elif S[h][w] == "#":
        return False
    else:
        return True


costs = [[-1] * W for _ in range(H)]
passed = [[0] * W for _ in range(H)]
costs[C_h - 1][C_w - 1] = 0
d = deque()
d.append((C_h - 1, C_w - 1))
while d:
    h, w = d.popleft()
    c = costs[h][w]
    if passed[h][w] == 1:
        continue
    for i, j in [(-1, 0), (1, 0), (0, 1), (0, -1)]:
        hh = h + i
        ww = w + j
        if exists(hh, ww):
            if (hh, ww) in d:
                d.remove((hh, ww))
            costs[hh][ww] = c
            d.appendleft((hh, ww))
    for i in range(5):
        for j in range(5):
            hh = h + i - 2
            ww = w + j - 2
            if exists(hh, ww):
                if costs[hh][ww] == -1:
                    costs[hh][ww] = c + 1
                    d.append((hh, ww))
    passed[h][w] = 1
print(costs[D_h - 1][D_w - 1])


Fixed submission:
from collections import deque

H, W = map(int, input().split())
C_h, C_w = map(int, input().split())
D_h, D_w = map(int, input().split())
W_ = W + 4
H_ = H + 4
S = ["#" * W_] * 2 + ["##" + input() + "##" for _ in range(H)] + ["#" * W_] * 2

INF = float('inf')
costs = [[INF] * W_ for _ in range(H_)]
costs[C_h + 1][C_w + 1] = 0
d0 = deque()
d0.append((C_h + 1, C_w + 1, 0))
d1 = deque()
while d0:
    h, w, c = d0.popleft()
    d1.append((h, w, c))
    for i, j in [(-1, 0), (1, 0), (0, 1), (0, -1)]:
        hh = h + i
        ww = w + j
        if S[hh][ww] == '.' and c < costs[hh][ww]:
            costs[hh][ww] = c
            d0.appendleft((hh, ww, c))
    if len(d0) == 0:
        while d1:
            h, w, c = d1.popleft()
            for i in range(5):
                for j in range(5):
                    hh = h + i - 2
                    ww = w + j - 2
                    if S[hh][ww] == '.' and c + 1 < costs[hh][ww]:
                        costs[hh][ww] = c + 1
                        d0.append((hh, ww, c + 1))

if costs[D_h + 1][D_w + 1] != INF:
    ans = costs[D_h + 1][D_w + 1]
else:
    ans = -1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import numpy as np

H, W = map(int, input().split())
C_h, C_w = map(int, input().split())
D_h, D_w = map(int, input().split())
S = []
for i in range(H):
    S.append(input())


def exists(h, w):
    if h < 0 or h >= H or w < 0 or w >= W:
        return False
    elif S[h][w] == "#":
        return False
    else:
        return True


costs = -1 * np.ones((H, W), dtype=np.int)
passed = np.zeros((H, W), dtype=np.int)
costs[C_h - 1, C_w - 1] = 0
d = deque()
d.append((C_h - 1, C_w - 1))
while d:
    h, w = d.popleft()
    c = costs[h, w]
    if passed[h, w] == 1:
        continue
    for i, j in [(-1, 0), (1, 0), (0, 1), (0, -1)]:
        hh = h + i
        ww = w + j
        if exists(hh, ww):
            if (hh, ww) in d:
                d.remove((hh, ww))
            costs[hh, ww] = c
            d.appendleft((hh, ww))
    for i in range(5):
        for j in range(5):
            hh = h + i - 2
            ww = w + j - 2
            if exists(hh, ww):
                if costs[hh, ww] == -1:
                    costs[hh, ww] = c + 1
                    d.append((hh, ww))
    passed[h, w] = 1
print(costs[D_h - 1, D_w - 1])


Fixed submission:
from collections import deque

H, W = map(int, input().split())
C_h, C_w = map(int, input().split())
D_h, D_w = map(int, input().split())
W_ = W + 4
H_ = H + 4
S = ["#" * W_] * 2 + ["##" + input() + "##" for _ in range(H)] + ["#" * W_] * 2

INF = float('inf')
costs = [[INF] * W_ for _ in range(H_)]
costs[C_h + 1][C_w + 1] = 0
d0 = deque()
d0.append((C_h + 1, C_w + 1, 0))
d1 = deque()
while d0:
    h, w, c = d0.popleft()
    d1.append((h, w, c))
    for i, j in [(-1, 0), (1, 0), (0, 1), (0, -1)]:
        hh = h + i
        ww = w + j
        if S[hh][ww] == '.' and c < costs[hh][ww]:
            costs[hh][ww] = c
            d0.appendleft((hh, ww, c))
    if len(d0) == 0:
        while d1:
            h, w, c = d1.popleft()
            for i in range(5):
                for j in range(5):
                    hh = h + i - 2
                    ww = w + j - 2
                    if S[hh][ww] == '.' and c + 1 < costs[hh][ww]:
                        costs[hh][ww] = c + 1
                        d0.append((hh, ww, c + 1))

if costs[D_h + 1][D_w + 1] != INF:
    ans = costs[D_h + 1][D_w + 1]
else:
    ans = -1
print(ans)