Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int,input().split())
sy,sx = map(int,input().split())
sy-=1
sx-=1
gy,gx = map(int,input().split())
gy,gx = gy-1,gx-1
grid = [0] * H
done = [[0]*W for _ in range(H)]
cost = [[float('inf')]*W for _ in range(H)]
cost[sy][sx] = 0
done[sy][sx] =1
for i in range(H):
    grid[i] = input()

walk_q = deque()
magic_q = deque()
def walk_bfs(magic_count):
    y,x = walk_q.popleft()
    magic_q.append([y,x])
    for new_y,new_x in [[y+1,x],[y,x+1],[y-1,x],[y,x-1]]:
        if 0<=new_y<H and 0<=new_x<W and grid[new_y][new_x] == '.':
            if not done[new_y][new_x]:
                done[new_y][new_x] = 1
                cost[new_y][new_x] = min(cost[new_y][new_x],magic_count)

                walk_q.append([new_y,new_x])
                magic_q.append([new_y,new_x])

def magic_bfs(magic_count):
    y,x = magic_q.popleft()
    for y_add in range(-2,3):
        for x_add in range(-2,3):
            if y_add == 0 and x_add == 0:
                continue
            new_y = y+y_add
            new_x = x+x_add

            if 0<=new_y<H and 0<=new_x<W and grid[new_y][new_x] == '.':
                if not done[new_y][new_x]:
                    done[new_y][new_x] = 1
                    cost[new_y][new_x] = min(cost[new_y][new_x],magic_count+1)

                    walk_q.append([new_y,new_x])

walk_q.append([sy,sx])
magic_q.append([sy,sx])
magic_count = 0
# 毎回、既にqueueにあるやつ（初回を除く）が、done、costの処理をされない？
# や、初回以外は常にどっかしらで「発見」される
while walk_q or magic_q:
    while walk_q:
        walk_bfs(magic_count)
    while magic_q:
        magic_bfs(magic_count)
    magic_count += 1
if done[gy][gx]:
    print(cost[gy][gx])
else:
    print(-1)

Fixed submission:
from collections import deque
H,W = map(int,input().split())
sy,sx = map(int,input().split())
sy-=1
sx-=1
gy,gx = map(int,input().split())
gy,gx = gy-1,gx-1
grid = [0] * H
done = [[0]*W for _ in range(H)]
cost = [[float('inf')]*W for _ in range(H)]
cost[sy][sx] = 0
done[sy][sx] =1
for i in range(H):
    grid[i] = input()

walk_q = deque()
magic_q = deque()
def walk_bfs(magic_count):
    y,x = walk_q.popleft()
    magic_q.append([y,x])
    for new_y,new_x in [[y+1,x],[y,x+1],[y-1,x],[y,x-1]]:
        if 0<=new_y<H and 0<=new_x<W and grid[new_y][new_x] == '.':
            if not done[new_y][new_x]:
                done[new_y][new_x] = 1
                cost[new_y][new_x] = min(cost[new_y][new_x],magic_count)

                walk_q.append([new_y,new_x])
                magic_q.append([new_y,new_x])

def magic_bfs(magic_count):
    y,x = magic_q.popleft()
    for y_add in range(-2,3):
        for x_add in range(-2,3):
            if y_add == 0 and x_add == 0:
                continue
            new_y = y+y_add
            new_x = x+x_add

            if 0<=new_y<H and 0<=new_x<W and grid[new_y][new_x] == '.':
                if not done[new_y][new_x]:
                    done[new_y][new_x] = 1
                    cost[new_y][new_x] = min(cost[new_y][new_x],magic_count+1)

                    walk_q.append([new_y,new_x])

walk_q.append([sy,sx])
magic_q.append([sy,sx])
magic_count = 0
# 毎回、既にqueueにあるやつ（初回を除く）が、done、costの処理をされない？
# や、初回以外は常にどっかしらで「発見」される
while walk_q or magic_q:
    while walk_q:
        walk_bfs(magic_count)
    while magic_q:
        magic_bfs(magic_count)
    magic_count += 1
if done[gy][gx]:
    print(cost[gy][gx])
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int,input().split())
sy,sx = map(int,input().split())
sy -= 1
sx -= 1
gy,gx = map(int,input().split())
grid = [0]*H
for i in range(H):
    grid[i] = input()
cost = [[float('inf')]*W for _ in range(H)]
cost[sy][sx] = 0
done = [[0]*W for _ in range(H)]
done[sy][sx] = 1
walk_q = deque()
walk_q.append([sy,sx])
magic_q = deque()
magic_q.append([sy,sx])
def walk(y,x):
    for new_y, new_x in [[y+1,x],[y,x+1],[y-1,x],[y,x-1]]:
        if 0 <= new_y < H and 0 <= new_x < W and grid[new_y][new_x] == '.':
            if cost[new_y][new_x] > cost[y][x]:
                cost[new_y][new_x] = cost[y][x]
                walk_q.append([new_y,new_x])
                magic_q.append([new_y,new_x])
                done[new_y][new_x] = 1
def magic(y,x):
    for y_add in [i for i in range(-2,3)]:
        for x_add in [i for i in range(-2,3)]:
            if 0 <= y+y_add < H and 0 <= x+x_add < W and grid[y+y_add][x+x_add] == '.':
                if cost[y+y_add][x+x_add] > cost[y][x]+1:
                    cost[y+y_add][x+x_add] = min(cost[y+y_add][x+x_add],cost[y][x]+1)
                    walk_q.append([y+y_add,x+x_add])
                    magic_q.append([y+y_add,x+x_add])
                    done[y+y_add][x+x_add] = 1

while walk_q or magic_q:
    while walk_q:
        y,x = walk_q.popleft()
        walk(y,x)
    while magic_q:
        y,x = magic_q.popleft()
        magic(y,x)
if not done[gy-1][gx-1]:
    print(-1)
else:
    print(cost[gy-1][gx-1])





Fixed submission:
from collections import deque
H,W = map(int,input().split())
sy,sx = map(int,input().split())
sy-=1
sx-=1
gy,gx = map(int,input().split())
gy,gx = gy-1,gx-1
grid = [0] * H
done = [[0]*W for _ in range(H)]
cost = [[float('inf')]*W for _ in range(H)]
cost[sy][sx] = 0
done[sy][sx] =1
for i in range(H):
    grid[i] = input()

walk_q = deque()
magic_q = deque()
def walk_bfs(magic_count):
    y,x = walk_q.popleft()
    magic_q.append([y,x])
    for new_y,new_x in [[y+1,x],[y,x+1],[y-1,x],[y,x-1]]:
        if 0<=new_y<H and 0<=new_x<W and grid[new_y][new_x] == '.':
            if not done[new_y][new_x]:
                done[new_y][new_x] = 1
                cost[new_y][new_x] = min(cost[new_y][new_x],magic_count)

                walk_q.append([new_y,new_x])
                magic_q.append([new_y,new_x])

def magic_bfs(magic_count):
    y,x = magic_q.popleft()
    for y_add in range(-2,3):
        for x_add in range(-2,3):
            if y_add == 0 and x_add == 0:
                continue
            new_y = y+y_add
            new_x = x+x_add

            if 0<=new_y<H and 0<=new_x<W and grid[new_y][new_x] == '.':
                if not done[new_y][new_x]:
                    done[new_y][new_x] = 1
                    cost[new_y][new_x] = min(cost[new_y][new_x],magic_count+1)

                    walk_q.append([new_y,new_x])

walk_q.append([sy,sx])
magic_q.append([sy,sx])
magic_count = 0
# 毎回、既にqueueにあるやつ（初回を除く）が、done、costの処理をされない？
# や、初回以外は常にどっかしらで「発見」される
while walk_q or magic_q:
    while walk_q:
        walk_bfs(magic_count)
    while magic_q:
        magic_bfs(magic_count)
    magic_count += 1
if done[gy][gx]:
    print(cost[gy][gx])
else:
    print(-1)