Buggy python submission (verdict: Runtime Error):
from collections import deque

H,W=map(int,input().split())
Sx,Sy=map(int,input().split())
Gx,Gy=map(int,input().split())
Sx-=1
Sy-=1
Gx-=1
Gy-=1
tmp=False

M=[[0 if j=='.' else 1 for j in input()]for i in range(H)]

# 0通路 1壁 2確認済み

ans=0
check.append([Sx,Sy])
while True:
    hit=set()
    while check:
        x,y = check.pop()
        #goul
        if x==Gx and y==Gy:
            tmp=True
            break
        M[x][y]=2
        #壁があるか
        for dx,dy in [ [x+1,y],[x-1,y],[x,y-1],[x,y+1] ]:
            if not(0<=dx and dx<H and 0<=dy and dy<W):
                continue
            if M[dx][dy]==1:
                hit.add((x,y))
            if M[dx][dy]==0:
                check.append([dx,dy])
    if tmp:
        print(ans)
        break
    if len(hit)==0:
        print(-1)
        break
    for e in hit:
        x,y=e
        for i in range(-2,3):
            for j in range(-2,3):
                if 0<=x+i and x+i<H and 0<=y+j and y+j<W and M[x+i][y+j]==0:
                    check.append([x+i,y+j])
    if len(check)==0:
        print(-1)
        break

    ans+=1

Fixed submission:
from collections import deque

H,W=map(int,input().split())
Sx,Sy=map(int,input().split())
Gx,Gy=map(int,input().split())
Sx-=1
Sy-=1
Gx-=1
Gy-=1
tmp=False
check=deque()

M=[[0 if j=='.' else 1 for j in input()]for i in range(H)]

# 0通路 1壁 2確認済み

ans=0
check.append([Sx,Sy])
while True:
    hit=set()
    while check:
        x,y = check.pop()
        #goul
        if x==Gx and y==Gy:
            tmp=True
            break
        M[x][y]=2
        #壁があるか
        for dx,dy in [ [x+1,y],[x-1,y],[x,y-1],[x,y+1] ]:
            if not(0<=dx and dx<H and 0<=dy and dy<W):
                continue
            if M[dx][dy]==1:
                hit.add((x,y))
            if M[dx][dy]==0:
                check.append([dx,dy])
    if tmp:
        print(ans)
        break
    if len(hit)==0:
        print(-1)
        break
    for e in hit:
        x,y=e
        for i in range(-2,3):
            for j in range(-2,3):
                if 0<=x+i and x+i<H and 0<=y+j and y+j<W and M[x+i][y+j]==0:
                    check.append([x+i,y+j])
    if len(check)==0:
        print(-1)
        break

    ans+=1

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

H,W=map(int,input().split())
Sx,Sy=map(int,input().split())
Gx,Gy=map(int,input().split())
Sx-=1
Sy-=1
Gx-=1
Gy-=1
tmp=False
check=deque()
hit=set()

M=[[0 if j=='.' else 1 for j in input()]for i in range(H)]

# 0通路 1壁 2確認済み

ans=0
check.append([Sx,Sy])
while True:
    while check:
        x,y = check.pop()
        #goul
        if x==Gx and y==Gy:
            tmp=True
            break
        M[x][y]=2
        #壁があるか
        for dx,dy in [ [x+1,y],[x-1,y],[x,y-1],[x,y+1] ]:
            if not(0<=dx and dx<H and 0<=dy and dy<W):
                continue
            if M[dx][dy]==1:
                hit.add((x,y))
            if M[dx][dy]==0:
                check.append([dx,dy])
    if tmp:
        print(ans)
        break
    if len(hit)==0:
        print(-1)
        break
    for e in hit:
        x,y=e
        for i in range(-2,3):
            for j in range(-2,3):
                if 0<=x+i and x+i<H and 0<=y+j and y+j<W and M[x+i][y+j]==0:
                    check.append([x+i,y+j])
    if len(check)==0:
        print(-1)
        break

    ans+=1



Fixed submission:
from collections import deque

H,W=map(int,input().split())
Sx,Sy=map(int,input().split())
Gx,Gy=map(int,input().split())
Sx-=1
Sy-=1
Gx-=1
Gy-=1
tmp=False
check=deque()

M=[[0 if j=='.' else 1 for j in input()]for i in range(H)]

# 0通路 1壁 2確認済み

ans=0
check.append([Sx,Sy])
while True:
    hit=set()
    while check:
        x,y = check.pop()
        #goul
        if x==Gx and y==Gy:
            tmp=True
            break
        M[x][y]=2
        #壁があるか
        for dx,dy in [ [x+1,y],[x-1,y],[x,y-1],[x,y+1] ]:
            if not(0<=dx and dx<H and 0<=dy and dy<W):
                continue
            if M[dx][dy]==1:
                hit.add((x,y))
            if M[dx][dy]==0:
                check.append([dx,dy])
    if tmp:
        print(ans)
        break
    if len(hit)==0:
        print(-1)
        break
    for e in hit:
        x,y=e
        for i in range(-2,3):
            for j in range(-2,3):
                if 0<=x+i and x+i<H and 0<=y+j and y+j<W and M[x+i][y+j]==0:
                    check.append([x+i,y+j])
    if len(check)==0:
        print(-1)
        break

    ans+=1