Problem p02579 (python)

Fixed submission:
import sys, math
import io, os
#data = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
#from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
#from collections import defaultdict as dd, deque, Counter
#from itertools import permutations,combinations
def data(): return sys.stdin.readline().strip()
def mdata(): return list(map(int, data().split()))
def outl(var) : sys.stdout.write(' '.join(map(str, var))+'\n')
def out(var) : sys.stdout.write(str(var)+'\n')
from decimal import Decimal
#from fractions import Fraction
#sys.setrecursionlimit(100000)
INF = float('inf')
mod = int(1e9)+7


h,w=mdata()
ch,cw=mdata()
dh,dw=mdata()
ch,cw,dh,dw=ch-1,cw-1,dh-1,dw-1
S=[data() for i in range(h)]
steps=[(0,1),(1,0),(-1,0),(0,-1)]
dist=[[INF]*w for i in range(h)]
dist[ch][cw]=0

queue = [(0, (ch, cw))]
while queue:
    path_len, (u, v) = heappop(queue)
    if path_len == dist[u][v]:
        for i in range(-2, 3):
            for j in range(-2, 3):
                if 0 <= u + i < h and 0 <= v + j < w and S[u + i][v + j] == '.':
                    if (i,j) in steps:
                        if path_len < dist[u+i][v+j]:
                            dist[u+i][v+j] = path_len
                            heappush(queue, (path_len, (u+i, v+j)))
                    else:
                        if 1 + path_len < dist[u + i][v + j]:
                            dist[u + i][v + j] = 1 + path_len
                            heappush(queue, (1 + path_len, (u + i, v + j)))


if dist[dh][dw]!=INF:
    out(dist[dh][dw])
else:
    out(-1)

Buggy submission (verdict: Runtime Error):
import sys, math
import io, os
#data = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
#from bisect import bisect_left as bl, bisect_right as br, insort
#from heapq import heapify, heappush, heappop
#from collections import defaultdict as dd, deque, Counter
#from itertools import permutations,combinations
def data(): return sys.stdin.readline().strip()
def mdata(): return list(map(int, data().split()))
def outl(var) : sys.stdout.write(' '.join(map(str, var))+'\n')
def out(var) : sys.stdout.write(str(var)+'\n')
from decimal import Decimal
#from fractions import Fraction
#sys.setrecursionlimit(100000)
INF = float('inf')
mod = int(1e9)+7


h,w=mdata()
ch,cw=mdata()
dh,dw=mdata()
ch-=1
dh-=1
cw-=1
dw-=1
if ch==dh and cw==dw:
    out(0)
    exit()
S=[data().split() for i in range(h)]
stack=[(dh,dw)]
steps=[(0,1),(1,0),(-1,0),(0,-1)]
moves={(dh,dw)}
while stack:
    a,b=stack.pop()
    for i,j in steps:
        if 0<=a+i<h and 0<=b+j<w and S[a+i][b+j]=='.' and (a+i,b+j) not in moves:
            moves.add((a+i,b+j))
            stack.append((a+i,b+j))
if (ch,cw) in moves:
    out(0)
    exit()
moves1={(ch,cw)}
stack=[(ch,cw)]
cnt=0
while stack:
    stack1=[]
    cnt+=1
    while stack:
        a, b = stack.pop()
        for i in range(-2,3):
            for j in range(-2,3):
                if 0 <= a + i < h and 0 <= b + j < w and S[a + i][b + j] == '.' and ((a + i, b + j) not in moves1):
                    moves1.add((a + i, b + j))
                    stack1.append((a + i, b + j))
                    if (a + i, b + j) in moves:
                        out(cnt)
                        exit()
    stack=stack1
out(-1)