Buggy python submission (verdict: Time Limit Exceeded):
import heapq
def main():
    h, w = map(int, input().split())
    strt_y, strt_x = map(int, input().split())
    goal_y, goal_x = map(int, input().split())
    stage = [[-1]*(w+4) for _ in range(h+4)]
    for i in range(h):
        s = input()
        for j in range(w):
            if s[j] == ".":
                stage[i+2][j+2] = float("inf")
    strt_y += 1
    strt_x += 1
    goal_y += 1
    goal_x += 1
    hq = [[0, strt_y, strt_x]]
    stage[strt_y][strt_x] = 0
    while len(hq) > 0:
        pt, crr_y, crr_x = heapq.heappop(hq)
        if stage[crr_y][crr_x] < pt:
            continue
        for i in range(crr_y-2, crr_y+2+1):
            for j in range(crr_x-2, crr_x+2+1):
                if abs(crr_y-i) + abs(crr_x-j) == 1:
                    if pt < stage[i][j]:
                        stage[i][j] = pt
                        heapq.heappush(hq, [pt, i, j])
                else:
                    if pt+1 < stage[i][j]:
                        stage[i][j] = pt+1
                        heapq.heappush(hq, [pt+1, i, j])
    if stage[goal_y][goal_x] == float("inf"):
        print(-1)
    else:
        print(stage[goal_y][goal_x])

if __name__ == "__main__":
    main()

Fixed submission:
import heapq
def main():
    h, w = map(int, input().split())
    strt_y, strt_x = map(int, input().split())
    goal_y, goal_x = map(int, input().split())
    stage = [[-1]*(w+4) for _ in range(h+4)]
    for i in range(h):
        s = input()
        for j in range(w):
            if s[j] == ".":
                stage[i+2][j+2] = float("inf")
    strt_y += 1
    strt_x += 1
    goal_y += 1
    goal_x += 1
    hq = [[0, strt_y, strt_x]]
    stage[strt_y][strt_x] = 0
    while len(hq) > 0:
        pt, crr_y, crr_x = heapq.heappop(hq)
        if stage[crr_y][crr_x] < pt:
            continue
        if crr_y == goal_y and crr_x == goal_x:
            break
        for i in range(crr_y-2, crr_y+2+1):
            for j in range(crr_x-2, crr_x+2+1):
                if abs(crr_y-i) + abs(crr_x-j) == 1:
                    if pt < stage[i][j]:
                        stage[i][j] = pt
                        heapq.heappush(hq, [pt, i, j])
                else:
                    if pt+1 < stage[i][j]:
                        stage[i][j] = pt+1
                        heapq.heappush(hq, [pt+1, i, j])
    if stage[goal_y][goal_x] == float("inf"):
        print(-1)
    else:
        print(stage[goal_y][goal_x])

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
def main():
    h, w = map(int, input().split())
    strt_y, strt_x = map(int, input().split())
    goal_y, goal_x = map(int, input().split())
    stage = [[-1]*(w+4) for _ in range(h+4)]
    for i in range(h):
        s = input()
        for j in range(w):
            if s[j] == ".":
                stage[i+2][j+2] = float("inf")
    strt_y += 1
    strt_x += 1
    goal_y += 1
    goal_x += 1
    hq = [[0, strt_y, strt_x]]
    stage[strt_y][strt_x] = 0
    while len(hq) > 0:
        pt, crr_y, crr_x = heapq.heappop(hq)
        if stage[crr_y][crr_x] < pt:
            continue
        for i in range(crr_y-2, crr_y+2+1):
            for j in range(crr_x-2, crr_x+2+1):
                if abs(crr_y-i) + abs(crr_x-j) == 1:
                    if pt < stage[i][j]:
                        stage[i][j] = pt
                        heapq.heappush(hq, [pt, i, j])
                else:
                    if pt+1 < stage[i][j]:
                        stage[i][j] = pt+1
                        heapq.heappush(hq, [pt+1, i, j])
    if stage[goal_y][goal_x] == float("inf"):
        print(-1)
    else:
        print(stage[goal_y][goal_x])

if __name__ == "__main__":
    main()

Fixed submission:
import heapq
def main():
    h, w = map(int, input().split())
    strt_y, strt_x = map(int, input().split())
    goal_y, goal_x = map(int, input().split())
    stage = [[-1]*(w+4) for _ in range(h+4)]
    for i in range(h):
        s = input()
        for j in range(w):
            if s[j] == ".":
                stage[i+2][j+2] = float("inf")
    strt_y += 1
    strt_x += 1
    goal_y += 1
    goal_x += 1
    hq = [[0, strt_y, strt_x]]
    stage[strt_y][strt_x] = 0
    while len(hq) > 0:
        pt, crr_y, crr_x = heapq.heappop(hq)
        if stage[crr_y][crr_x] < pt:
            continue
        if crr_y == goal_y and crr_x == goal_x:
            break
        for i in range(crr_y-2, crr_y+2+1):
            for j in range(crr_x-2, crr_x+2+1):
                if abs(crr_y-i) + abs(crr_x-j) == 1:
                    if pt < stage[i][j]:
                        stage[i][j] = pt
                        heapq.heappush(hq, [pt, i, j])
                else:
                    if pt+1 < stage[i][j]:
                        stage[i][j] = pt+1
                        heapq.heappush(hq, [pt+1, i, j])
    if stage[goal_y][goal_x] == float("inf"):
        print(-1)
    else:
        print(stage[goal_y][goal_x])

if __name__ == "__main__":
    main()