Buggy python submission (verdict: Time Limit Exceeded):
f=lambda:map(int,input().split())
h,w=f()
ch,cw=f()
dh,dw=f()
g=[[*input()] for _ in range(h)]
from collections import *
q=deque([(0,ch-1,cw-1)])
while q:
  d,x,y=q.popleft()
  if g[x][y]=='.': g[x][y]=d
  for dx in range(-2,3):
    for dy in range(-2,3):
      j=abs(dx)+abs(dy)
      nx,ny=x+dx,y+dy
      if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and j:
        if j>1: q.append((d+1,nx,ny))
        else: q.appendleft((d,nx,ny))
a=g[dh-1][dw-1]
print([a,-1][a=='.'])

Fixed submission:
INF=10**6
f=lambda:map(int,input().split())
h,w=f()
ch,cw=f()
ch-=1
cw-=1
dh,dw=f()
dh-=1
dw-=1
g=[input() for _ in range(h)]
from collections import *
def grid_01_bfs(x,y):
  d=[[INF]*w for _ in range(h)]
  d[x][y]=0
  q=deque([(0,x,y)])
  while q:
    td,tx,ty=q.popleft()
    if d[tx][ty]<td: continue
    for dx,dy in [(1,0),(0,1),(-1,0),(0,-1)]:
      nx,ny=tx+dx,ty+dy
      if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and d[nx][ny]>td:
        d[nx][ny]=td
        q.appendleft((d[nx][ny],nx,ny))
    for dx in range(-2,3):
      for dy in range(-2,3):
        nx,ny=tx+dx,ty+dy
        if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and d[nx][ny]>td+1:
          d[nx][ny]=td+1
          q.append((d[nx][ny],nx,ny))
  return d
a=grid_01_bfs(ch,cw)[dh][dw]
print([-1,a][a<INF])

Buggy python submission (verdict: Time Limit Exceeded):
INF=10**6
f=lambda:map(int,input().split())
h,w=f()
ch,cw=f()
dh,dw=f()
g=[input() for _ in range(h)]
d=[[INF]*w for _ in range(h)]
d[ch-1][cw-1]=0
from collections import *
q=deque([(0,ch-1,cw-1)])
while q:
  td,tx,ty=q.popleft()
  if d[tx][ty]<td: continue
  for dx,dy in [(1,0),(0,1),(-1,0),(0,-1)]:
    nx,ny=tx+dx,ty+dy
    if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and d[nx][ny]>td:
      d[nx][ny]=td
      q.appendleft((td,nx,ny))
  for dx in range(-2,3):
    for dy in range(-2,3):
      nx,ny=tx+dx,ty+dy
      if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and d[nx][ny]>td+1:
        d[nx][ny]=td+1
        q.append((td+1,nx,ny))
a=d[dh-1][dw-1]
print([-1,a][a<INF])

Fixed submission:
INF=10**6
f=lambda:map(int,input().split())
h,w=f()
ch,cw=f()
ch-=1
cw-=1
dh,dw=f()
dh-=1
dw-=1
g=[input() for _ in range(h)]
from collections import *
def grid_01_bfs(x,y):
  d=[[INF]*w for _ in range(h)]
  d[x][y]=0
  q=deque([(0,x,y)])
  while q:
    td,tx,ty=q.popleft()
    if d[tx][ty]<td: continue
    for dx,dy in [(1,0),(0,1),(-1,0),(0,-1)]:
      nx,ny=tx+dx,ty+dy
      if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and d[nx][ny]>td:
        d[nx][ny]=td
        q.appendleft((d[nx][ny],nx,ny))
    for dx in range(-2,3):
      for dy in range(-2,3):
        nx,ny=tx+dx,ty+dy
        if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and d[nx][ny]>td+1:
          d[nx][ny]=td+1
          q.append((d[nx][ny],nx,ny))
  return d
a=grid_01_bfs(ch,cw)[dh][dw]
print([-1,a][a<INF])

Buggy python submission (verdict: Time Limit Exceeded):
INF=10**6
f=lambda:map(int,input().split())
h,w=f()
ch,cw=f()
ch-=1
cw-=1
dh,dw=f()
dh-=1
dw-=1
g=[input() for _ in range(h)]
from collections import *
def grid_01_bfs(x,y):
  d=[[INF]*w for _ in range(h)]
  d[x][y]=0
  q=deque([(0,x,y)])
  while q:
    td,tx,ty=q.popleft()
    if d[tx][ty]<td: continue
    for dx,dy in [(1,0),(0,1),(-1,0),(0,-1)]:
      nx,ny=tx+dx,ty+dy
      if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and d[nx][ny]>td:
        d[nx][ny]=td
        q.appendleft((d[nx][ny],nx,ny))
    for dx in range(-2,3):
      for dy in range(-2,3):
        nx,ny=tx+dx,ty+dy
        if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and d[nx][ny]>td+1:
          d[nx][ny]=td+1
          q.append((d[nx][ny],nx,ny))
  return d
a=grid_01_bfs(ch,cw)[dh][dw]
print([-1,a][a<INF])

Fixed submission:
INF=10**6
f=lambda:map(int,input().split())
h,w=f()
ch,cw=f()
ch-=1
cw-=1
dh,dw=f()
dh-=1
dw-=1
g=[input() for _ in range(h)]
from collections import *
def grid_01_bfs(x,y):
  d=[[INF]*w for _ in range(h)]
  d[x][y]=0
  q=deque([(0,x,y)])
  while q:
    td,tx,ty=q.popleft()
    if d[tx][ty]<td: continue
    for dx,dy in [(1,0),(0,1),(-1,0),(0,-1)]:
      nx,ny=tx+dx,ty+dy
      if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and d[nx][ny]>td:
        d[nx][ny]=td
        q.appendleft((d[nx][ny],nx,ny))
    for dx in range(-2,3):
      for dy in range(-2,3):
        nx,ny=tx+dx,ty+dy
        if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and d[nx][ny]>td+1:
          d[nx][ny]=td+1
          q.append((d[nx][ny],nx,ny))
  return d
a=grid_01_bfs(ch,cw)[dh][dw]
print([-1,a][a<INF])

Buggy python submission (verdict: Time Limit Exceeded):
INF=10**9
f=lambda:map(int,input().split())
h,w=f()
ch,cw=f()
ch-=1
cw-=1
dh,dw=f()
dh-=1
dw-=1
g=[input() for _ in range(h)]
from heapq import *
def dijkstra_grid(x,y):
  d=[[INF]*w for _ in range(h)]
  d[x][y]=0
  q=[(0,x,y)]
  while q:
    td,tx,ty=heappop(q)
    if d[tx][ty]<td: continue
    for dx,dy in [(1,0),(0,1),(-1,0),(0,-1)]:
      nx,ny=tx+dx,ty+dy
      if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and d[nx][ny]>td:
        d[nx][ny]=td
        heappush(q,(td,nx,ny))
    for dx in range(-2,3):
      for dy in range(-2,3):
        nx,ny=tx+dx,ty+dy
        if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and d[nx][ny]>td+1:
          d[nx][ny]=td+1
          heappush(q,(td+1,nx,ny))
  return d
a=dijkstra_grid(ch,cw)[dh][dw]
print([-1,a][a<INF])

Fixed submission:
INF=10**6
f=lambda:map(int,input().split())
h,w=f()
ch,cw=f()
ch-=1
cw-=1
dh,dw=f()
dh-=1
dw-=1
g=[input() for _ in range(h)]
from collections import *
def grid_01_bfs(x,y):
  d=[[INF]*w for _ in range(h)]
  d[x][y]=0
  q=deque([(0,x,y)])
  while q:
    td,tx,ty=q.popleft()
    if d[tx][ty]<td: continue
    for dx,dy in [(1,0),(0,1),(-1,0),(0,-1)]:
      nx,ny=tx+dx,ty+dy
      if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and d[nx][ny]>td:
        d[nx][ny]=td
        q.appendleft((d[nx][ny],nx,ny))
    for dx in range(-2,3):
      for dy in range(-2,3):
        nx,ny=tx+dx,ty+dy
        if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and d[nx][ny]>td+1:
          d[nx][ny]=td+1
          q.append((d[nx][ny],nx,ny))
  return d
a=grid_01_bfs(ch,cw)[dh][dw]
print([-1,a][a<INF])

Buggy python submission (verdict: Time Limit Exceeded):
INF=10**9
f=lambda:map(int,input().split())
h,w=f()
ch,cw=f()
ch-=1
cw-=1
dh,dw=f()
dh-=1
dw-=1
g=[input() for _ in range(h)]
from heapq import *
def dijkstra_grid(x,y):
  d=[[INF]*w for _ in range(h)]
  d[x][y]=0
  q=[(0,x,y)]
  while q:
    td,tx,ty=heappop(q)
    for dx,dy in [(1,0),(0,1),(-1,0),(0,-1)]:
      nx,ny=tx+dx,ty+dy
      if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and d[nx][ny]>td:
        d[nx][ny]=td
        heappush(q,(td,nx,ny))
    for dx in range(-2,3):
      for dy in range(-2,3):
        nx,ny=tx+dx,ty+dy
        if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and d[nx][ny]>td+1:
          d[nx][ny]=td+1
          heappush(q,(td+1,nx,ny))
  return d
a=dijkstra_grid(ch,cw)[dh][dw]
print([-1,a][a<INF])

Fixed submission:
INF=10**6
f=lambda:map(int,input().split())
h,w=f()
ch,cw=f()
ch-=1
cw-=1
dh,dw=f()
dh-=1
dw-=1
g=[input() for _ in range(h)]
from collections import *
def grid_01_bfs(x,y):
  d=[[INF]*w for _ in range(h)]
  d[x][y]=0
  q=deque([(0,x,y)])
  while q:
    td,tx,ty=q.popleft()
    if d[tx][ty]<td: continue
    for dx,dy in [(1,0),(0,1),(-1,0),(0,-1)]:
      nx,ny=tx+dx,ty+dy
      if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and d[nx][ny]>td:
        d[nx][ny]=td
        q.appendleft((d[nx][ny],nx,ny))
    for dx in range(-2,3):
      for dy in range(-2,3):
        nx,ny=tx+dx,ty+dy
        if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and d[nx][ny]>td+1:
          d[nx][ny]=td+1
          q.append((d[nx][ny],nx,ny))
  return d
a=grid_01_bfs(ch,cw)[dh][dw]
print([-1,a][a<INF])