Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
def main():
    h, w = map(int, input().split())
    S = list(map(lambda x: int(x) + 1, input().split()))
    G = list(map(lambda x: int(x) + 1, input().split()))
    A = ['#' * (w + 4)] * 2 + ['##' + input() + '##' for _ in range(h)] + ['#' * (w + 4)] * 2
    INF = 1000000
    score = [[INF] * (w + 2) for _ in range(h + 2)]
    score[S[0]][S[1]] = 0
    stack = deque([S])
    walk = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    magic = [(i, j) for i in range(-2, 3) for j in range(-2, 3) if (i, j) not in walk + [(0, 0)]]
    while stack:
        y, x = stack.popleft()
        for dx, dy in walk:
            X, Y = x + dx, y + dy 
            if A[Y][X] == '#': continue
            if score[Y][X] > (s := score[y][x]):
                stack.appendleft([Y, X])
                score[Y][X] = s
        for dx, dy in magic:
            X, Y = x + dx, y + dy 
            if A[Y][X] == '#': continue
            if score[Y][X] > (s := score[y][x] + 1):
                stack.append([Y, X])
                score[Y][X] = s
    print(ans if (ans := score[G[0]][G[1]]) != INF else -1)

if __name__ == '__main__':
    main()

Fixed submission:
from collections import deque
def main():
    h, w = map(int, input().split())
    S = list(map(lambda x: int(x) - 1, input().split()))
    G = list(map(lambda x: int(x) - 1, input().split()))
    A = [list(input()) for _ in range(h)]
    INF = 100000000
    score = [[INF] * w for _ in range(h)]
    score[S[0]][S[1]] = 0
    visited = [[False] * w for _ in range(h)]
    stack = deque([S])
    dxy = [[1, 0], [-1, 0], [0, -1], [0, 1]]
    while stack:
        y, x = stack.popleft()
        if visited[y][x]:
            continue
        else:
            visited[y][x] = True
        count = 0
        for dx, dy in dxy:
            X = x + dx
            Y = y + dy
            if Y < 0 or Y >= h or X < 0 or X >= w:
                continue
            elif A[Y][X] == '.':
                stack.appendleft([Y, X])
                score[Y][X] = min(score[Y][X], score[y][x])
        if count == 0 and A[y][x] == '.':
            for i in range(-2, 3):
                for j in range(-2, 3):
                    X = x + j
                    Y = y + i
                    if Y < 0 or Y >= h or X < 0 or X >= w:
                        continue
                    if A[Y][X] == '.' and score[Y][X] > score[y][x] + 1:
                        stack.append([Y, X])
                        score[Y][X] = min(score[Y][X], score[y][x] + 1)
    print(score[G[0]][G[1]]) if score[G[0]][G[1]] != INF else print(-1)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
def main():
    h, w = map(int, input().split())
    S = list(map(lambda x: int(x) + 1, input().split()))
    G = list(map(lambda x: int(x) + 1, input().split()))
    A = ['#' * (w + 4)] * 2 + ['##' + input() + '##' for _ in range(h)] + ['#' * (w + 4)] * 2
    INF = 1000000
    score = [[INF] * (w + 2) for _ in range(h + 2)]
    score[S[0]][S[1]] = 0
    stack = deque([S])
    walk = ((1, 0), (-1, 0), (0, 1), (0, -1))
    warp = tuple((i, j) for i in range(-2, 3) for j in range(-2, 3) if (i, j) not in (walk + ((0, 0), )))
    visited = set()
    while stack:
        y, x = stack.popleft()
        if (y, x) in visited:
            continue
        else:
            visited.add((y, x))
        for dx, dy in walk:
            X = x + dx
            Y = y + dy
            if A[Y][X] == '#': continue
            if score[Y][X] > (s := score[y][x]):
                stack.appendleft([Y, X])
                score[Y][X] = s
        for i, j in warp:
            X = x + j
            Y = y + i
            if A[Y][X] == '#': continue
            if score[Y][X] > (s := score[y][x] + 1):
                stack.append([Y, X])
                score[Y][X] = s
    print(ans if (ans := score[G[0]][G[1]]) != INF else -1)

if __name__ == '__main__':
    main()


Fixed submission:
from collections import deque
def main():
    h, w = map(int, input().split())
    S = list(map(lambda x: int(x) - 1, input().split()))
    G = list(map(lambda x: int(x) - 1, input().split()))
    A = [list(input()) for _ in range(h)]
    INF = 100000000
    score = [[INF] * w for _ in range(h)]
    score[S[0]][S[1]] = 0
    visited = [[False] * w for _ in range(h)]
    stack = deque([S])
    dxy = [[1, 0], [-1, 0], [0, -1], [0, 1]]
    while stack:
        y, x = stack.popleft()
        if visited[y][x]:
            continue
        else:
            visited[y][x] = True
        count = 0
        for dx, dy in dxy:
            X = x + dx
            Y = y + dy
            if Y < 0 or Y >= h or X < 0 or X >= w:
                continue
            elif A[Y][X] == '.':
                stack.appendleft([Y, X])
                score[Y][X] = min(score[Y][X], score[y][x])
        if count == 0 and A[y][x] == '.':
            for i in range(-2, 3):
                for j in range(-2, 3):
                    X = x + j
                    Y = y + i
                    if Y < 0 or Y >= h or X < 0 or X >= w:
                        continue
                    if A[Y][X] == '.' and score[Y][X] > score[y][x] + 1:
                        stack.append([Y, X])
                        score[Y][X] = min(score[Y][X], score[y][x] + 1)
    print(score[G[0]][G[1]]) if score[G[0]][G[1]] != INF else print(-1)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
def main():
    h, w = map(int, input().split())
    S = list(map(lambda x: int(x) + 1, input().split()))
    G = list(map(lambda x: int(x) + 1, input().split()))
    A = [['#'] * (w + 4)] * 2 + [['#', '#'] + list(input()) + ['#', '#'] for _ in range(h)] + [['#'] * (w + 4)] * 2
    INF = 100000000
    score = [[INF] * (w + 2) for _ in range(h + 2)]
    score[S[0]][S[1]] = 0
    stack = deque([S])
    dxy = ((1, 0), (-1, 0), (0, 1), (0, -1))
    while stack:
        y, x = stack.popleft()
        for dx, dy in dxy:
            X = x + dx
            Y = y + dy
            if A[Y][X] == '#': continue
            if score[Y][X] > (s := score[y][x]):
                stack.appendleft([Y, X])
                score[Y][X] = s
        for i in range(-2, 3):
            for j in range(-2, 3):
                X = x + j
                Y = y + i
                if A[Y][X] == '#': continue
                if score[Y][X] > (s := score[y][x]) + 1:
                    stack.append([Y, X])
                    score[Y][X] = s + 1
    print(ans if (ans := score[G[0]][G[1]]) != INF else -1)

if __name__ == '__main__':
    main()


Fixed submission:
from collections import deque
def main():
    h, w = map(int, input().split())
    S = list(map(lambda x: int(x) - 1, input().split()))
    G = list(map(lambda x: int(x) - 1, input().split()))
    A = [list(input()) for _ in range(h)]
    INF = 100000000
    score = [[INF] * w for _ in range(h)]
    score[S[0]][S[1]] = 0
    visited = [[False] * w for _ in range(h)]
    stack = deque([S])
    dxy = [[1, 0], [-1, 0], [0, -1], [0, 1]]
    while stack:
        y, x = stack.popleft()
        if visited[y][x]:
            continue
        else:
            visited[y][x] = True
        count = 0
        for dx, dy in dxy:
            X = x + dx
            Y = y + dy
            if Y < 0 or Y >= h or X < 0 or X >= w:
                continue
            elif A[Y][X] == '.':
                stack.appendleft([Y, X])
                score[Y][X] = min(score[Y][X], score[y][x])
        if count == 0 and A[y][x] == '.':
            for i in range(-2, 3):
                for j in range(-2, 3):
                    X = x + j
                    Y = y + i
                    if Y < 0 or Y >= h or X < 0 or X >= w:
                        continue
                    if A[Y][X] == '.' and score[Y][X] > score[y][x] + 1:
                        stack.append([Y, X])
                        score[Y][X] = min(score[Y][X], score[y][x] + 1)
    print(score[G[0]][G[1]]) if score[G[0]][G[1]] != INF else print(-1)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
def main():
    h, w = map(int, input().split())
    S = list(map(lambda x: int(x) - 1, input().split()))
    G = list(map(lambda x: int(x) - 1, input().split()))
    A = [list(input()) for _ in range(h)]
    INF = 100000000
    score = [[INF] * w for _ in range(h)]
    score[S[0]][S[1]] = 0
    visited = [[False] * w for _ in range(h)]
    stack = deque([S])
    dxy = [[1, 0], [-1, 0], [0, -1], [0, 1]]
    while stack:
        y, x = stack.popleft()
        if visited[y][x]:
            continue
        else:
            visited[y][x] = True
        count = 0
        for dx, dy in dxy:
            X = x + dx
            Y = y + dy
            if Y < 0 or Y >= h or X < 0 or X >= w:
                continue
            elif A[Y][X] == '.':
                stack.appendleft([Y, X])
                score[Y][X] = min(score[Y][X], score[y][x])
        if count == 0 and A[y][x] == '.':
            for i in range(-2, 3):
                for j in range(-2, 3):
                    X = x + j
                    Y = y + i
                    if Y < 0 or Y >= h or X < 0 or X >= w:
                        continue
                    if A[Y][X] == '.':
                        stack.append([Y, X])
                        score[Y][X] = min(score[Y][X], score[y][x] + 1)
    print(s) if (s := score[G[0]][G[1]]) != INF else print(-1)


if __name__ == '__main__':
    main()


Fixed submission:
from collections import deque
def main():
    h, w = map(int, input().split())
    S = list(map(lambda x: int(x) - 1, input().split()))
    G = list(map(lambda x: int(x) - 1, input().split()))
    A = [list(input()) for _ in range(h)]
    INF = 100000000
    score = [[INF] * w for _ in range(h)]
    score[S[0]][S[1]] = 0
    visited = [[False] * w for _ in range(h)]
    stack = deque([S])
    dxy = [[1, 0], [-1, 0], [0, -1], [0, 1]]
    while stack:
        y, x = stack.popleft()
        if visited[y][x]:
            continue
        else:
            visited[y][x] = True
        count = 0
        for dx, dy in dxy:
            X = x + dx
            Y = y + dy
            if Y < 0 or Y >= h or X < 0 or X >= w:
                continue
            elif A[Y][X] == '.':
                stack.appendleft([Y, X])
                score[Y][X] = min(score[Y][X], score[y][x])
        if count == 0 and A[y][x] == '.':
            for i in range(-2, 3):
                for j in range(-2, 3):
                    X = x + j
                    Y = y + i
                    if Y < 0 or Y >= h or X < 0 or X >= w:
                        continue
                    if A[Y][X] == '.' and score[Y][X] > score[y][x] + 1:
                        stack.append([Y, X])
                        score[Y][X] = min(score[Y][X], score[y][x] + 1)
    print(score[G[0]][G[1]]) if score[G[0]][G[1]] != INF else print(-1)


if __name__ == '__main__':
    main()