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

H,W = map(int, input().split())
C = list(map(int, input().split()))
D = list(map(int, input().split()))
D_y = D[0] -1   #0スタート
D_x = D[1] -1   #0スタート
MAP = [list(input()) for i in range(H)]
visited = [[-1] * W for i in range(H)]
visited[C[0]-1][C[1]-1] =0 #初期位置
moves = [(0,1), (0,-1), (1,0), (-1,0)]

main_q = deque()
magic_q = deque()

main_q.append([C[0]-1, C[1]-1])

#BFS
while main_q:
    y, x = main_q.popleft()
    magic_q.append([y,x])
    for move in moves:
        dy, dx = move
        moved_y = y + dy
        moved_x = x + dx

        if moved_y < 0 or moved_x < 0 or moved_y > H - 1 or moved_x > W -1:
            continue
        if MAP[moved_y][moved_x] == '#':
            continue
        if MAP[moved_y][moved_x] == '.' and visited[moved_y][moved_x] == -1:
            visited[moved_y][moved_x] = visited[y][x]
            main_q.append([moved_y,moved_x])

    #マジックBFS
    if not main_q:
        while magic_q:
            y, x = magic_q.popleft()
            for dy in range(-2,3):
                for dx in range(-2,3):
                    moved_y = y + dy
                    moved_x = x + dx

                    if moved_y < 0 or moved_x < 0 or moved_y > H - 1 or moved_x > W -1:
                        continue
                    if MAP[moved_y][moved_x] == '#':
                        continue
                    if MAP[moved_y][moved_x] == '.' and visited[moved_y][moved_x] == -1:
                        visited[moved_y][moved_x] = visited[y][x] + 1
                        main_q.append([moved_y,moved_x])

anser = visited[D_y][D_x]
print(anser)

Fixed submission:
from collections import deque

H,W = map(int, input().split())
C = list(map(int, input().split()))
D = list(map(int, input().split()))
D_y = D[0] -1   #0スタート
D_x = D[1] -1   #0スタート
MAP = [list(input()) for i in range(H)]
visited = [[-1] * W for i in range(H)]
visited[C[0]-1][C[1]-1] =0 #初期位置
moves = [(0,1), (0,-1), (1,0), (-1,0)]

main_q = deque()
magic_q = deque()

main_q.append([C[0]-1, C[1]-1])

#BFS
while main_q:
    y, x = main_q.popleft()
    magic_q.append([y,x])
    for move in moves:
        dy, dx = move
        moved_y = y + dy
        moved_x = x + dx

        if moved_y < 0 or moved_x < 0 or moved_y > H - 1 or moved_x > W -1:
            continue
        if MAP[moved_y][moved_x] == '#':
            continue
        if MAP[moved_y][moved_x] == '.' and visited[moved_y][moved_x] == -1:
            visited[moved_y][moved_x] = visited[y][x]
            main_q.append([moved_y,moved_x])

    #マジックBFS
    if not main_q:
        while magic_q:
            y, x = magic_q.popleft()
            for dy in range(-2,3):
                for dx in range(-2,3):
                    moved_y = y + dy
                    moved_x = x + dx

                    if moved_y < 0 or moved_x < 0 or moved_y > H - 1 or moved_x > W -1:
                        continue
                    if MAP[moved_y][moved_x] == '#':
                        continue
                    if MAP[moved_y][moved_x] == '.' and visited[moved_y][moved_x] == -1:
                        visited[moved_y][moved_x] = visited[y][x] + 1
                        main_q.append([moved_y,moved_x])

anser = visited[D_y][D_x]
print(anser)