Buggy python submission (verdict: Time Limit Exceeded):
def main():
  import sys
  input = lambda: sys.stdin.readline().rstrip()
  from heapq import heappop,heappush
  h,w=map(int,input().split())
  n=h*w
  hw=lambda hh,ww:hh*w+ww
  ch,cw=map(int,input().split())
  s=hw(ch-1,cw-1)
  dh,dw=map(int,input().split())
  g=hw(dh-1,dw-1)
  b=[list(input())for _ in range(h)]
  edge=[[]for _ in range(h*w)]
  for i in range(h):
    for j in range(w):
      if b[i][j]=="#":continue
      if i!=h-1:
        if b[i+1][j]==".":
          edge[hw(i,j)].append((hw(i+1,j),0))
          edge[hw(i+1,j)].append((hw(i,j),0))
      if j!=w-1:
        if b[i][j+1]==".":
          edge[hw(i,j)].append((hw(i,j+1),0))
          edge[hw(i,j+1)].append((hw(i,j),0))
      for ii in range(max(0,i-2),min(h,i+3)):
        for jj in range(max(0,j-2),min(w,j+3)):
          if i==ii and j==jj:continue
          if b[ii][jj]==".":
            edge[hw(i,j)].append((hw(ii,jj),1))
  inf = 10**20
  ans = [inf]*n
  ans[s] = 0
  hh = [(0, s)]
  while hh:
    c, v = heappop(hh)
    if ans[v] < c:
      continue
    for u, t in edge[v]:
      if c+t < ans[u]:
        ans[u] = c+t
        heappush(hh, (c+t, u))
  if ans[g]==10**20:ans[g]=-1
  print(ans[g])
if __name__ == "__main__":
  main()

Fixed submission:
import sys
input = lambda: sys.stdin.readline().rstrip()
h,w=map(int,input().split())
ch,cw=map(int,input().split())
ch-=1
cw-=1
stack=[(ch,cw)]
dh,dw=map(int,input().split())
dh-=1
dw-=1
b=[w*[-1]for _ in range(h)]
for i in range(h):
  for j,t in enumerate(input()):
    if t=="#":b[i][j]=10**10
b[ch][cw]=0
dxy=((-1,0),(1,0),(0,-1),(0,1))
for ans in range(10**6):
  for x,y in stack:
    b[x][y]=1
    if x==dh and y==dw:
      print(ans)
      exit()
    for dx,dy in dxy:
      if 0<=x+dx<h and 0<=y+dy<w:
        if b[x+dx][y+dy]==10**10:continue
        if b[x+dx][y+dy]==-1:
          stack.append((x+dx,y+dy))
          b[x+dx][y+dy]=1
  stackk=[]
  for x,y in stack:
    for xx in range(max(0,x-2),min(h,x+3)):
      for yy in range(max(0,y-2),min(w,y+3)):
        if b[xx][yy]==-1:stackk.append((xx,yy))
  stack=stackk
print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = lambda: sys.stdin.readline().rstrip()
from heapq import heappop,heappush
def dijkstra(s, n, edge):
  inf = 10**20
  ans = [inf]*n
  ans[s] = 0
  root = [-1]*n
  h = [[0, s]]
  while h:
    c, v = heappop(h)
    if ans[v] < c:
      continue
    for u, t in edge[v]:
      if c+t < ans[u]:
        ans[u] = c+t
        root[u] = v
        heappush(h, [c+t, u])
  return ans
h,w=map(int,input().split())
hw=lambda hh,ww:hh*w+ww
ch,cw=map(int,input().split())
s=hw(ch-1,cw-1)
dh,dw=map(int,input().split())
g=hw(dh-1,dw-1)
b=[list(input())for _ in range(h)]
edge=[[]for _ in range(h*w)]
for i in range(h):
  for j in range(w):
    if b[i][j]=="#":continue
    if i!=h-1:
      if b[i+1][j]==".":
        edge[hw(i,j)].append((hw(i+1,j),0))
        edge[hw(i+1,j)].append((hw(i,j),0))
    if j!=w-1:
      if b[i][j+1]==".":
        edge[hw(i,j)].append((hw(i,j+1),0))
        edge[hw(i,j+1)].append((hw(i,j),0))
    for ii in range(max(0,i-2),min(h,i+3)):
      for jj in range(max(0,j-2),min(w,j+3)):
        if i==ii and j==jj:continue
        if b[ii][jj]==".":
          edge[hw(i,j)].append((hw(ii,jj),1))
ans=dijkstra(s,h*w,edge)[g]
if ans==10**20:ans=-1
print(ans)

Fixed submission:
import sys
input = lambda: sys.stdin.readline().rstrip()
h,w=map(int,input().split())
ch,cw=map(int,input().split())
ch-=1
cw-=1
stack=[(ch,cw)]
dh,dw=map(int,input().split())
dh-=1
dw-=1
b=[w*[-1]for _ in range(h)]
for i in range(h):
  for j,t in enumerate(input()):
    if t=="#":b[i][j]=10**10
b[ch][cw]=0
dxy=((-1,0),(1,0),(0,-1),(0,1))
for ans in range(10**6):
  for x,y in stack:
    b[x][y]=1
    if x==dh and y==dw:
      print(ans)
      exit()
    for dx,dy in dxy:
      if 0<=x+dx<h and 0<=y+dy<w:
        if b[x+dx][y+dy]==10**10:continue
        if b[x+dx][y+dy]==-1:
          stack.append((x+dx,y+dy))
          b[x+dx][y+dy]=1
  stackk=[]
  for x,y in stack:
    for xx in range(max(0,x-2),min(h,x+3)):
      for yy in range(max(0,y-2),min(w,y+3)):
        if b[xx][yy]==-1:stackk.append((xx,yy))
  stack=stackk
print(-1)