Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
"""
Created on Sun Sep  6 00:28:43 2020

@author: liang
"""


H, W = map(int, input().split())
sh, sw = map(int, input().split())
sh -= 1
sw -= 1

gh, gw = map(int, input().split())
gh -= 1
gw -= 1

field = [input() for i in range(H)]

d = [[-1]*W for _ in range(H)]
visited = [[False]*W for _ in range(H)]
adj1 = [(1,0), (-1,0), (0,1), (0,-1)]
adj2 = [(x,y) for x in range(-2,3) for y in range(-2,3)]

from collections import deque

def BFS():
    def isValid(p):
        if p[0] < 0 or p[0] >= H or p[1] < 0 or p[1] >= W:
            return False
        if field[p[0]][p[1]] == "#":
            return False
        if visited[p[0]][p[1]] :
            return False
        return True
    
    q = deque()
    q.append((sh, sw))
    d[sh][sw] = 0
    visited[sh][sw] = True
    while q:
        cur = q.popleft()
        #normal move
        for a in adj1:
            nex = (cur[0] + a[0], cur[1] + a[1])
            if isValid(nex) and (d[nex[0]][nex[1]] == -1 or d[nex[0]][nex[1]] > d[cur[0]][cur[1]]):
                 d[nex[0]][nex[1]] = d[cur[0]][cur[1]]
                 visited[nex[0]][nex[1]] = True
                 q.append((nex[0], nex[1]))
        #warp
        for a in adj2:
            nex = (cur[0] + a[0], cur[1] + a[1])
            if isValid(nex):
                d[nex[0]][nex[1]] = d[cur[0]][cur[1]] + 1
                q.append((nex[0], nex[1]))
                    
BFS()
#print(d)
#print(visited)
print(d[gh][gw])
    
            
                 

Fixed submission:
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 11 11:57:24 2020

@author: liang
"""

H, W = map(int, input().split())
sy, sx = map(int, input().split())
sy -= 1
sx -= 1
gy, gx = map(int, input().split())
gy -= 1
gx -= 1
field = [input() for _ in range(H)]

from collections import deque
q = deque()
adj1 = [(0,1), (0,-1), (1,0), (-1,0)]
visited = [[False]*W for _ in range(H)]

q.append((sy, sx, 0))

while q:
    cy, cx, cost = q.popleft()
    #print(cy,cx,cost)
    if (cy,cx) == (gy,gx):
        print(cost)
        break
    if not visited[cy][cx]:
        #print("visit!")
        visited[cy][cx] = True
        #cost 0
        for a in adj1:
            ny, nx = cy + a[0], cx + a[1]
            if ny >= H or ny < 0:
                continue
            if nx >= W or nx < 0:
                continue
            if field[ny][nx] == "#":
                continue
            if not visited[ny][nx]:
                q.appendleft((ny, nx, cost))
        #cost 1
        for i in range(-2,3):
            for j in range(-2,3):
                ny, nx = cy + i, cx + j
                if ny >= H or ny < 0:
                    continue
                if nx >= W or nx < 0:
                    continue
                if field[ny][nx] == "#":
                    continue
                if not visited[ny][nx]:
                    q.append((ny, nx, cost+1))
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 11 11:57:24 2020

@author: liang
"""

H, W = map(int, input().split())
sy, sx = map(int, input().split())
sy -= 1
sx -= 1
gy, gx = map(int, input().split())
gy -= 1
gx -= 1
field = [input() for _ in range(H)]

from collections import deque
q = deque()
adj1 = [(0,1), (0,-1), (1,0), (-1,0)]
visited = [[False]*W for _ in range(H)]

q.append((sy, sx, 0))

while q:
    cy, cx, cost = q.popleft()
    #print(cy,cx,cost)
    if (cy,cx) == (gy,gx):
        print(cost)
        break
    if not visited[cy][cx]:
        #print("visit!")
        visited[cy][cx] = True
        #cost 0
        for a in adj1:
            ny, nx = cy + a[0], cx + a[1]
            if ny >= H or ny < 0:
                continue
            if nx >= W or nx < 0:
                continue
            if field[ny][nx] == "#":
                continue
            if not visited[ny][nx]:
                q.appendleft((ny, nx, cost))
        #cost 1
        for i in range(-2,3):
            for j in range(-2,3):
                ny, nx = cy + i, cx + j
                if ny >= H or ny < 0:
                    continue
                if nx >= W or nx < 0:
                    continue
                if field[ny][nx] == "#":
                    continue
                if not visited[ny][nx]:
                    q.append((ny, nx, cost+1))
else:
    print(-1)

Fixed submission:
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 11 11:57:24 2020

@author: liang
"""

H, W = map(int, input().split())
sy, sx = map(int, input().split())
sy -= 1
sx -= 1
gy, gx = map(int, input().split())
gy -= 1
gx -= 1
field = [input() for _ in range(H)]

from collections import deque
q = deque()
adj1 = [(0,1), (0,-1), (1,0), (-1,0)]
visited = [[False]*W for _ in range(H)]

q.append((sy, sx, 0))

while q:
    cy, cx, cost = q.popleft()
    #print(cy,cx,cost)
    if (cy,cx) == (gy,gx):
        print(cost)
        break
    if not visited[cy][cx]:
        #print("visit!")
        visited[cy][cx] = True
        #cost 0
        for a in adj1:
            ny, nx = cy + a[0], cx + a[1]
            if ny >= H or ny < 0:
                continue
            if nx >= W or nx < 0:
                continue
            if field[ny][nx] == "#":
                continue
            if not visited[ny][nx]:
                q.appendleft((ny, nx, cost))
        #cost 1
        for i in range(-2,3):
            for j in range(-2,3):
                ny, nx = cy + i, cx + j
                if ny >= H or ny < 0:
                    continue
                if nx >= W or nx < 0:
                    continue
                if field[ny][nx] == "#":
                    continue
                if not visited[ny][nx]:
                    q.append((ny, nx, cost+1))
else:
    print(-1)