Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

h,w = map(int,readline().split())
sh,sw = map(int,readline().split())
gh,gw = map(int,readline().split())
b = read().split()

sh -= 1; sw -= 1; gh -= 1; gw -= 1

#print(b)

from collections import deque

def nbd(nh,nw,cost,dv):
    if 0 <= nh < h and 0 <= nw < w and b[nh][nw]==".":
        dv += cost
        if dv < res[nh][nw]:
            res[nh][nw] = dv
            if cost: q.append((dv,nh,nw))
            else: q.appendleft((dv,nh,nw))
    return 


INF = 1<<30
res = [[INF]*w for _ in range(h)] #startからの最短距離
res[sh][sw] = 0
q = deque([(0,sh,sw)]) #(そこまでの距離、点)
while q:
    dv,vh,vw = q.popleft()
    if res[vh][vw] < dv: continue
    if vh==gh and vw==gw: break
    nbd(vh-1,vw,0,dv)
    nbd(vh,vw-1,0,dv)
    nbd(vh+1,vw,0,dv)
    nbd(vh,vw+1,0,dv)

    nbd(vh-1,vw-1,1,dv)
    nbd(vh-1,vw+1,1,dv)
    nbd(vh+1,vw-1,1,dv)
    nbd(vh+1,vw+1,1,dv)

    for i in range(-2,3):
        nbd(vh-2,vw+i,1,dv)
        nbd(vh+2,vw+i,1,dv)

    for i in range(-1,2):
        nbd(vh+i,vw-2,1,dv)
        nbd(vh+i,vw+2,1,dv)
    
    #print(res)
        
ans = res[gh][gw]
if ans != INF:
    print(ans)
else:
    print(-1)







Fixed submission:
# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

h,w = map(int,readline().split())
sh,sw = map(int,readline().split())
gh,gw = map(int,readline().split())
b = read().split()

sh -= 1; sw -= 1; gh -= 1; gw -= 1

#print(b)

from collections import deque

def nbd(nh,nw,dv):
    if dv < res[nh][nw]:
        res[nh][nw] = dv
        q.appendleft((dv,nh,nw))
    return 




INF = 1<<30
res = [[INF]*w for _ in range(h)] #startからの最短距離
res[sh][sw] = 0
q = deque([(0,sh,sw)]) #(そこまでの距離、点)
while q:
    dv,vh,vw = q.popleft()
    if res[vh][vw] < dv: continue
    if vh==gh and vw==gw: break
    
    if vh and b[vh-1][vw]==".":
        nbd(vh-1,vw,dv)
    if vw and b[vh][vw-1]==".":
        nbd(vh,vw-1,dv)
    if vh+1<h and b[vh+1][vw]==".":
        nbd(vh+1,vw,dv)
    if vw+1<w and b[vh][vw+1]==".":
        nbd(vh,vw+1,dv)
    
    for i in range(-2,3):
        nh = vh+i
        if nh < 0 or nh >= h: continue
        for j in range(-2,3):
            nw = vw + j
            if nw < 0 or nw >= w: continue
            if abs(i)+abs(j) > 1:
                if b[nh][nw] == "." and dv+1 < res[nh][nw]:
                    res[nh][nw] = dv+1
                    q.append((dv+1,nh,nw))


ans = res[gh][gw]
if ans != INF:
    print(ans)
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

h,w = map(int,readline().split())
sh,sw = map(int,readline().split())
gh,gw = map(int,readline().split())
b = read().split()

sh -= 1; sw -= 1; gh -= 1; gw -= 1

#print(b)

from collections import deque

def nbd(nh,nw,cost,dv):
    if 0 <= nh < h and 0 <= nw < w and b[nh][nw]==".":
        dv += cost
        if dv < res[nh][nw]:
            res[nh][nw] = dv
            if cost: q.append((dv,nh,nw))
            else: q.appendleft((dv,nh,nw))
    return 


INF = 1<<30
res = [[INF]*w for _ in range(h)] #startからの最短距離
res[sh][sw] = 0
q = deque([(0,sh,sw)]) #(そこまでの距離、点)
while q:
    dv,vh,vw = q.popleft()
    if res[vh][vw] < dv: continue
    nbd(vh-1,vw,0,dv)
    nbd(vh,vw-1,0,dv)
    nbd(vh+1,vw,0,dv)
    nbd(vh,vw+1,0,dv)
    
    nbd(vh-1,vw-1,1,dv)
    nbd(vh-1,vw+1,1,dv)
    nbd(vh+1,vw-1,1,dv)
    nbd(vh+1,vw+1,1,dv)

    for i in range(-2,3):
        nbd(vh-2,vw+i,1,dv)
        nbd(vh+2,vw+i,1,dv)

    for i in range(-1,2):
        nbd(vh+i,vw-2,1,dv)
        nbd(vh+i,vw+2,1,dv)
    
    #print(res)
        
ans = res[gh][gw]
if ans != INF:
    print(ans)
else:
    print(-1)







Fixed submission:
# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

h,w = map(int,readline().split())
sh,sw = map(int,readline().split())
gh,gw = map(int,readline().split())
b = read().split()

sh -= 1; sw -= 1; gh -= 1; gw -= 1

#print(b)

from collections import deque

def nbd(nh,nw,dv):
    if dv < res[nh][nw]:
        res[nh][nw] = dv
        q.appendleft((dv,nh,nw))
    return 




INF = 1<<30
res = [[INF]*w for _ in range(h)] #startからの最短距離
res[sh][sw] = 0
q = deque([(0,sh,sw)]) #(そこまでの距離、点)
while q:
    dv,vh,vw = q.popleft()
    if res[vh][vw] < dv: continue
    if vh==gh and vw==gw: break
    
    if vh and b[vh-1][vw]==".":
        nbd(vh-1,vw,dv)
    if vw and b[vh][vw-1]==".":
        nbd(vh,vw-1,dv)
    if vh+1<h and b[vh+1][vw]==".":
        nbd(vh+1,vw,dv)
    if vw+1<w and b[vh][vw+1]==".":
        nbd(vh,vw+1,dv)
    
    for i in range(-2,3):
        nh = vh+i
        if nh < 0 or nh >= h: continue
        for j in range(-2,3):
            nw = vw + j
            if nw < 0 or nw >= w: continue
            if abs(i)+abs(j) > 1:
                if b[nh][nw] == "." and dv+1 < res[nh][nw]:
                    res[nh][nw] = dv+1
                    q.append((dv+1,nh,nw))


ans = res[gh][gw]
if ans != INF:
    print(ans)
else:
    print(-1)