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

H, W = map(int, input().split())
sy, sx = map(lambda x: int(x)-1, input().split())
gy, gx = map(lambda x: int(x)-1, input().split())
maze = [input() for _ in range(H)]

INF = float("INF")
cnt = [[INF]*W for _ in range(H)]

q = deque()
q.append([sy, sx])
cnt[sy][sx] = 0
move = [[1, 0], [-1, 0], [0, 1], [0, -1]]

while q:
    y, x = q.popleft()
    for dy, dx in move:
        ny = y + dy
        nx = x + dx
        if ny < 0 or ny > H-1 or nx < 0 or nx > W-1:
            continue
        if maze[ny][nx] == "." and cnt[ny][nx] > cnt[y][x]:
            cnt[ny][nx] = cnt[y][x]
            q.appendleft([ny, nx])
    for i in range(-2, 2+1):
        for j in range(-2, 2+1):
            wy = y + i
            wx = x + j
            if wy < 0 or wy > H-1 or wx < 0 or wx > W-1:
                continue
            if maze[wy][wx] == "." and cnt[wy][wx] > cnt[y][x] + 1:
                cnt[wy][wx] = cnt[y][x] + 1
                q.append([wy, wx])

ans = cnt[gy][gx] if cnt[gy][gx] != INF else -1
print(ans)

Fixed submission:
from collections import deque

H, W = map(int, input().split())
sy, sx = map(lambda x: int(x)-1, input().split())
gy, gx = map(lambda x: int(x)-1, input().split())
maze = [input() for _ in range(H)]

INF = float("INF")
cnt = [[INF]*W for _ in range(H)]

q = deque()
q.append([sy, sx])
cnt[sy][sx] = 0
move = [[1, 0], [-1, 0], [0, 1], [0, -1]]

while q:
    y, x = q.popleft()
    for dy, dx in move:
        ny = y + dy
        nx = x + dx
        if ny < 0 or ny > H-1 or nx < 0 or nx > W-1:
            continue
        if maze[ny][nx] == "." and cnt[ny][nx] > cnt[y][x]:
            cnt[ny][nx] = cnt[y][x]
            q.appendleft([ny, nx])
        for i in range(-2, 2+1):
            for j in range(-2, 2+1):
                wy = y + i
                wx = x + j
                if wy < 0 or wy > H-1 or wx < 0 or wx > W-1:
                    continue
                if maze[wy][wx] == "." and cnt[wy][wx] > cnt[y][x] + 1:
                    cnt[wy][wx] = cnt[y][x] + 1
                    q.append([wy, wx])

ans = cnt[gy][gx] if cnt[gy][gx] != INF else -1
print(ans)

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

H, W = map(int, input().split())
sy, sx = map(lambda x: int(x)-1, input().split())
gy, gx = map(lambda x: int(x)-1, input().split())
maze = [input() for _ in range(H)]

INF = float("INF")
cnt = [[INF]*W for _ in range(H)]

q = deque()
q.append([sy, sx])
cnt[sy][sx] = 0
move = [[1, 0], [-1, 0], [0, 1], [0, -1]]

while q:
    y, x = q.popleft()
    for dy, dx in move:
        ny = y + dy
        nx = x + dx
        if ny < 0 or ny > H-1 or nx < 0 or nx > W-1:
            continue
        if maze[ny][nx] == "." and cnt[ny][nx] > cnt[y][x]:
            cnt[ny][nx] = cnt[y][x]
            q.appendleft([ny, nx])
        for i in range(-2, 2+1):
            for j in range(-2, 2+1):
                wy = y + i
                wx = x + j
                if wy < 0 or wy > H-1 or wx < 0 or wx > W-1:
                    continue
                if maze[wy][wx] == "." and cnt[wy][wx] > cnt[y][x]:
                    cnt[wy][wx] = cnt[y][x] + 1
                    q.append([wy, wx])

ans = -1 if cnt[gy][gx] == INF else cnt[gy][gx]
print(ans)

Fixed submission:
from collections import deque

H, W = map(int, input().split())
sy, sx = map(lambda x: int(x)-1, input().split())
gy, gx = map(lambda x: int(x)-1, input().split())
maze = [input() for _ in range(H)]

INF = float("INF")
cnt = [[INF]*W for _ in range(H)]

q = deque()
q.append([sy, sx])
cnt[sy][sx] = 0
move = [[1, 0], [-1, 0], [0, 1], [0, -1]]

while q:
    y, x = q.popleft()
    for dy, dx in move:
        ny = y + dy
        nx = x + dx
        if ny < 0 or ny > H-1 or nx < 0 or nx > W-1:
            continue
        if maze[ny][nx] == "." and cnt[ny][nx] > cnt[y][x]:
            cnt[ny][nx] = cnt[y][x]
            q.appendleft([ny, nx])
        for i in range(-2, 2+1):
            for j in range(-2, 2+1):
                wy = y + i
                wx = x + j
                if wy < 0 or wy > H-1 or wx < 0 or wx > W-1:
                    continue
                if maze[wy][wx] == "." and cnt[wy][wx] > cnt[y][x] + 1:
                    cnt[wy][wx] = cnt[y][x] + 1
                    q.append([wy, wx])

ans = cnt[gy][gx] if cnt[gy][gx] != INF else -1
print(ans)