Problem:
 Score : 400 points    Problem Statement A maze is composed of a grid of H \times W squares - H vertical, W horizontal.  The square at the i -th row from the top and the j -th column from the left - (i,j) - is a wall if S_{ij} is # and a road if S_{ij} is . .  There is a magician in (C_h,C_w) . He can do the following two kinds of moves:   Move A: Walk to a road square that is vertically or horizontally adjacent to the square he is currently in.  Move B: Use magic to warp himself to a road square in the 5\times 5 area centered at the square he is currently in.   In either case, he cannot go out of the maze.  At least how many times does he need to use the magic to reach (D_h, D_w) ?      Constraints  1 \leq H,W \leq 10^3  1 \leq C_h,D_h \leq H  1 \leq C_w,D_w \leq W  S_{ij} is # or . .  S_{C_h C_w} and S_{D_h D_w} are . .  (C_h,C_w) \neq (D_h,D_w)         Input Input is given from Standard Input in the following format:  H  W  C_h  C_w  D_h  D_w  S_{11}\ldots S_{1W}  \vdots  S_{H1}\ldots S_{HW}       Output Print the minimum number of times the magician needs to use the magic. If he cannot reach (D_h,D_w) , print -1 instead.        Sample Input 1 4 4
1 1
4 4
..#.
..#.
.#..
.#..      Sample Output 1 1  For example, by walking to (2,2) and then using the magic to travel to (4,4) , just one use of magic is enough.  Note that he cannot walk diagonally.       Sample Input 2 4 4
1 4
4 1
.##.
####
####
.##.      Sample Output 2 -1  He cannot move from there.       Sample Input 3 4 4
2 2
3 3
....
....
....
....      Sample Output 3 0  No use of magic is needed.       Sample Input 4 4 5
1 2
2 5
#.###
####.
#..##
#..##      Sample Output 4 2

Solution (Python):

from heapq import heapify, heappush, heappop
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect
import copy

sys.setrecursionlimit(10**6)




def zz():
    return list(map(int, sys.stdin.readline().split()))


def z():
    return int(sys.stdin.readline())


def S():
    return sys.stdin.readline()[:-1]


def C(line):
    return [sys.stdin.readline() for _ in range(line)]



def main(H, W, c_h, c_w, d_h, d_w):

    que = deque([(c_h, c_w)])
    INF = 1e9
    dist = [[INF]*W for _ in range(H)]
    dist[c_h][c_w] = 0
    while que:
        
        tmp = que.popleft()
        h, w = tmp
        d = dist[h][w]
        for dy, dx in ((1, 0), (0, 1), (-1, 0), (0, -1)):
            new_h = h + dy
            new_w = w + dx
            if new_h < 0 or H <= new_h or new_w < 0 or W <= new_w:
                continue
            if board[new_h][new_w] == '
                continue
            if dist[new_h][new_w] <= d:
                continue
            dist[new_h][new_w] = d
            que.appendleft((new_h, new_w))
        
        for m_dy in range(-2, 3):
            for m_dx in range(-2, 3):
                new_h = h + m_dy
                new_w = w + m_dx
                if new_h < 0 or H <= new_h or new_w < 0 or W <= new_w:
                    continue
                if board[new_h][new_w] == '
                    continue
                if dist[new_h][new_w] <= d + 1:
                    continue
                dist[new_h][new_w] = d + 1
                que.append((new_h, new_w))

    ans = dist[d_h][d_w]
    if ans == INF:
        print(-1)
    else:
        print(ans)


H, W = zz()
c_h, c_w = zz()
d_h, d_w = zz()
board = []
c_h -= 1
c_w -= 1
d_h -= 1
d_w -= 1
for i in range(H):
    tmp = S()
    board.append(tmp)
main(H, W, c_h, c_w, d_h, d_w)

Solution (Python):
from collections import deque

H, W = map(int, input().split())
S = tuple(map(lambda x:int(x)-1, input().split()))
G = tuple(map(lambda x:int(x)-1, input().split()))
field = []
for _ in range(H):
    field.append(input())

cost = [[-1] * W for _ in range(H)]

def can_go(pos):
    if pos[0] >= 0 and pos[0] < H and pos[1] >= 0 and pos[1] < W:
        if field[pos[0]][pos[1]] == '.' and cost[pos[0]][pos[1]] == -1:
            return True
        else:
            return False
    else:
        return False

def recursive_walk(pos):
    dests = set()
    stack = [pos]
    while stack:
        pos = stack.pop()
        if pos in dests:
            continue
        dests.add(pos)
        stack += walk(pos)
    return dests

def walk(pos):
    dests = []
    for d in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
        new_pos = (pos[0]+d[0], pos[1]+d[1])
        if can_go(new_pos):
            dests.append(new_pos)
    return dests

def warp(pos):
    dests = []
    for dy in range(-2, 3):
        for dx in range(-2, 3):
            new_pos = (pos[0]+dy, pos[1]+dx)
            if can_go(new_pos):
                dests.append(new_pos)
    return dests

q = deque()
q.append([S, 0])

while q:
    pos, c = q.popleft()
    if cost[pos[0]][pos[1]] != -1:
        continue
    cost[pos[0]][pos[1]] = c
    if pos == G:
        break
    warp_from = [pos]
    for dest in recursive_walk(pos):
        if cost[dest[0]][dest[1]] == -1:
            cost[dest[0]][dest[1]] = c
            warp_from.append(dest)
    warp_to = set()
    for fr in warp_from:
        for dest in warp(fr):
            if cost[dest[0]][dest[1]] == -1:
                warp_to.add(dest)
    for dest in warp_to:
        q.append((dest, c+1))
print(cost[G[0]][G[1]])

Solution (Python):
def main():
    import sys,heapq
    input = sys.stdin.buffer.readline
 
    h,w = map(int,input().split())
    cx,cy = map(int,input().split())
    dx,dy = map(int,input().split())
    cx -= 1
    cy -= 1
    dx -= 1
    dy -= 1
 
    start = cx*w + cy
    goal = dx*w + dy
 
 
    s = []
    for _ in range(h):
        s.append(list(input()))
 
    dg = 10**7
 
 
    edge = [[] for _ in range(h*w)]
    for xy in range(h*w):
        x,y = divmod(xy,w)
        if s[x][y] == 35:
            continue
        for i in range(-2,3):
            for j in range(-2,3):
                if i == 0 and j == 0:
                    continue
                if 0 <= x+i < h and 0 <= y+j < w and s[x+i][y+j] == 46:
                    
                    if (i == 0 and abs(j) == 1) or (j == 0 and abs(i) == 1):
                        edge[xy].append( (x+i)*w + y+j )
                    else:
                        edge[xy].append(dg + (x+i)*w + y+j )
 
    d = [float("inf")]*(h*w)
    d[start] = 0
 
    tank = []
 
    for e in edge[start]:
        heapq.heappush(tank,e)
 
 
    while tank:
        now = heapq.heappop(tank)
        cost,xy = divmod(now,dg)
        if d[xy] != float("inf"):
            continue
        d[xy] = cost
        if xy == goal:
            break
        for nxt in edge[xy]:
            if nxt >= dg:
                if d[nxt-dg] == float("inf"):
                    heapq.heappush(tank,(cost+1)*dg + nxt-dg)
            else:
                if d[nxt] == float("inf"):
                    heapq.heappush(tank,cost*dg + nxt)
 
    if d[goal] == float("inf"):
        print(-1)
    else:
        print(d[goal])
 
if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

H, W = map(int, input().split())
cy, cx = map(int, input().split())
sy, sx = map(int, input().split())
tiles = [list(input()) for _ in range(H)]


INF = 10**4
arrived = [[INF for _ in range(W)] for __ in range(H)]
paths = deque()
warps = deque()
dx = [1, 0, -1, 0]
dy = [0, -1, 0, 1]
moves = [-2, -1, 0, 1, 2]
arrived[cy-1][cx-1] = 0
paths.append([cx-1, cy-1])
count = 0


while(len(paths) > 0):
  while(len(paths) > 0):
    nx, ny = paths.popleft()
    if ny == sy-1 and nx == sx-1:
      print(count)
      exit()
    for i in range(4):
      if 0 <= nx+dx[i] < W and 0 <= ny+dy[i] < H and arrived[ny+dy[i]][nx+dx[i]] > count and tiles[ny+dy[i]][nx+dx[i]] == '.':
        paths.append([nx+dx[i], ny+dy[i]])
        arrived[ny+dy[i]][nx+dx[i]] = count
    for x in moves:
      for y in moves:
        if 0 <= nx+x < W and 0 <= ny+y < H and arrived[ny+y][nx+x] == INF and tiles[ny+y][nx+x] == '.':
          warps.append([nx+x, ny+y])
  count += 1
  while(len(warps) > 0):
    nx, ny = warps.popleft()
    if arrived[ny][nx] == INF:
      arrived[ny][nx] = count
      paths.append([nx, ny])
print(-1)

Solution (Python):
from collections import deque

H, W = map(int, input().split())
ch, cw = map(lambda x: int(x)-1, input().split())
dh, dw = map(lambda x: int(x)-1, input().split())
s = [input() for _ in range(H)]

INF = 10**9
m = [[INF]*W for _ in range(H)]
m[ch][cw] = 0

q = deque()
q.append((ch, cw, 0))

cnt = 0
while q:
    cnt += 1
    h, w, n = q.popleft()
    for i, j in [(0,1), (0,-1), (1,0), (-1,0)]:
        if (h+i<0 or w+j<0) or (h+i>=H or w+j>=W):
            continue
        if m[h+i][w+j] <= n:
            continue
        if s[h+i][w+j] == '
            continue
        m[h+i][w+j] = n
        q.appendleft((h+i, w+j, n))

    for i in range(-2, 3):
        for j in range(-2, 3):
            if (h+i<0 or w+j<0) or (h+i>=H or w+j>=W):
                continue
            if m[h+i][w+j] <= n+1:
                continue
            if s[h+i][w+j] == '
                continue
            m[h+i][w+j] = n + 1
            q.append((h+i, w+j, n+1))
if m[dh][dw] == INF:
    print(-1)
else:
    print(m[dh][dw])

Solution (Python):
import sys
input = sys.stdin.readline
 
H,W = map(int , input().split())
sh,sw = map(int , input().split())
eh,ew = map(int , input().split())
mv_a_pts = [[-1,0],[1,0],[0,-1],[0,1]]
mv_b_pts = [(-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2),
        (-1, -2), (-1, -1), (-1, 0), (-1, 1), (-1, 2),
        (0, -2), (0, -1), (0, 1), (0, 2),
        (1, -2), (1, -1), (1, 0), (1, 1), (1, 2),
        (2, -2), (2, -1), (2, 0), (2, 1), (2, 2),]
maze = [input() for _ in range(H)]
warp_cnts = [[None]*(W) for j in range(H)]
 
def mv_a():
  while len(mv_a_tgs):
    h,w = mv_a_tgs.pop()
    mv_b_tgs.append([h,w])
    warp_cnts[h][w] = cnt
    for vh,vw in mv_a_pts:
      tgh = h + vh
      tgw = w + vw
      if 0 <= tgh <= H-1 and 0 <= tgw <= W-1 :
        if warp_cnts[tgh][tgw] is None and maze[tgh][tgw] != "
          mv_a_tgs.append([tgh,tgw])

def mv_b():
  while len(mv_b_tgs):
    h,w = mv_b_tgs.pop()
    for vh,vw in mv_b_pts:
      tgh = h + vh
      tgw = w + vw
      if 0 <= tgh <= H-1 and 0 <= tgw <= W-1 :
        if warp_cnts[tgh][tgw] is None and maze[tgh][tgw] != "
          mv_a_tgs.append([tgh,tgw])
        
mv_b_tgs=[]
mv_a_tgs = [[sh-1,sw-1]]
cnt = 0
while len(mv_a_tgs)>0:
  mv_a()
  mv_b()
  cnt+=1
print(-1 if warp_cnts[eh-1][ew-1] is None else warp_cnts[eh-1][ew-1])

Solution (Python):
from collections import deque

H, W = map(int, input().split())
C_h, C_w = map(int, input().split())
D_h, D_w = map(int, input().split())
W_ = W + 4
H_ = H + 4
S = ["

INF = float('inf')
costs = [[INF] * W_ for _ in range(H_)]
costs[C_h + 1][C_w + 1] = 0
d0 = deque()
d0.append((C_h + 1, C_w + 1, 0))
d1 = deque()
while d0:
    h, w, c = d0.popleft()
    d1.append((h, w, c))
    for i, j in [(-1, 0), (1, 0), (0, 1), (0, -1)]:
        hh = h + i
        ww = w + j
        if S[hh][ww] == '.' and c < costs[hh][ww]:
            costs[hh][ww] = c
            d0.appendleft((hh, ww, c))
    if len(d0) == 0:
        while d1:
            h, w, c = d1.popleft()
            for i in range(5):
                for j in range(5):
                    hh = h + i - 2
                    ww = w + j - 2
                    if S[hh][ww] == '.' and c + 1 < costs[hh][ww]:
                        costs[hh][ww] = c + 1
                        d0.append((hh, ww, c + 1))

if costs[D_h + 1][D_w + 1] != INF:
    ans = costs[D_h + 1][D_w + 1]
else:
    ans = -1
print(ans)

Solution (Python):

import sys 
from collections import deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
H,W = map(int,readline().split())
sx,sy = map(int,readline().split())
gx,gy = map(int,readline().split())
sx -= 1; sy -= 1
gx -= 1; gy -= 1

grid = [readline().decode().rstrip() for _ in range(H)]
INF = 10 ** 31
dist = [[INF]*W for _ in range(H)]

X = [(0,1),(-1,0),(0,-1),(1,0)]
Y = [(-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2), (-1, -2), (-1, -1), (-1, 1), (-1, 2), (0, -2), (0, 2), (1, -2), (1, -1), (1, 1), (1, 2), (2, -2), (2, -1), (2, 0), (2, 1), (2, 2)]

def ZeroOne_bfs():
    while q:
        x,y = q.popleft()
        for dx,dy in X:
            nx = x + dx; ny = y + dy
            if not(0 <= nx < H and 0 <= ny < W):
                continue
            d = dist[x][y]
            if grid[nx][ny] == '.':
                if d < dist[nx][ny]:
                    dist[nx][ny] = d
                    q.appendleft((nx,ny))
                    
        for dx,dy in Y:
            nx = x + dx; ny = y + dy
            if not(0 <= nx < H and 0 <= ny < W):
                continue
            d = dist[x][y] + 1
            if grid[nx][ny] == '.':
                if d < dist[nx][ny]:
                    dist[nx][ny] = d 
                    q.append((nx,ny))

q = deque([(sx,sy)])
dist[sx][sy] = 0
ZeroOne_bfs()
ans = dist[gx][gy]

print(ans if ans != INF else -1)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
import heapq
INF = 10**18

def dijkstra(start, G):
    done = [False]*(len(G))
    dist = [INF]*(len(G))
    dist[start] = 0
    h = []
    heapq.heappush(h, [0, start])
    while h:
        _, cur = heapq.heappop(h)
        if done[cur]: continue
        done[cur] = True
        for to, cost in G[cur]:
            if dist[cur]+cost < dist[to]:
                dist[to] = dist[cur]+cost
                heapq.heappush(h, [dist[to], to])
    return dist

H, W = map(int, input().split())
sh, sw = map(lambda x: int(x)-1, input().split())
gh, gw = map(lambda x: int(x)-1, input().split())
S = [input() for _ in range(H)]
done = [[-1]*W for _ in range(H)]
def dfs(h, w, num):
    done[h][w] = num
    for dh, dw in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
        nh, nw = h+dh, w+dw
        if not (0 <= nh < H and 0 <= nw < W and S[nh][nw] == "."):
            continue
        if done[nh][nw] == -1:
            dfs(nh, nw, num)
num = 0
for i in range(H):
    for j in range(W):
        if done[i][j] == -1 and S[i][j] == ".":
            dfs(i, j, num)
            num += 1
G = [[] for _ in range(num)]
done2 = {}
for h in range(H):
    for w in range(W):
        if S[h][w] != ".": continue
        fr = done[h][w]
        for ii in range(-2, 3):
            for jj in range(-2, 3):
                nh, nw = h+ii, w+jj
                if not (0 <= nh < H and 0 <= nw < W and S[nh][nw] == "."):
                    continue
                to = done[nh][nw]
                if fr != to and (not (fr, to) in done2):
                    done2[(fr, to)] = True
                    done2[(to, fr)] = True
                    G[fr].append((to, 1))
                    G[to].append((fr, 1))
D = dijkstra(done[sh][sw], G)
ans = D[done[gh][gw]]
if ans != INF:
    print(ans)
else:
    print(-1)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

from collections import deque
import heapq
INF = 10**18

def dijkstra(start, G):
    done = [False]*(len(G))
    dist = [INF]*(len(G))
    dist[start] = 0
    h = []
    heapq.heappush(h, [0, start])
    while h:
        _, cur = heapq.heappop(h)
        if done[cur]: continue
        done[cur] = True
        for to, cost in G[cur]:
            if dist[cur]+cost < dist[to]:
                dist[to] = dist[cur]+cost
                heapq.heappush(h, [dist[to], to])
    return dist

H, W = map(int, input().split())
sh, sw = map(lambda x: int(x)-1, input().split())
gh, gw = map(lambda x: int(x)-1, input().split())
S = [input() for _ in range(H)]
done = [[-1]*W for _ in range(H)]
q = deque()
num = 0
for ii in range(H):
    for jj in range(W):
        if done[ii][jj] != -1: continue
        q.append((ii, jj))
        done[ii][jj] = num
        while q:
            h, w = q.popleft()
            for dh, dw in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
                nh, nw = h+dh, w+dw
                if not (0 <= nh < H and 0 <= nw < W and S[nh][nw] == "."):
                    continue
                if done[nh][nw] == -1:
                    q.append((nh, nw))
                    done[nh][nw] = num
        num += 1
G = [[] for _ in range(num)]
done2 = {}
for h in range(H):
    for w in range(W):
        if S[h][w] != ".": continue
        fr = done[h][w]
        for ii in range(-2, 3):
            for jj in range(-2, 3):
                nh, nw = h+ii, w+jj
                if not (0 <= nh < H and 0 <= nw < W and S[nh][nw] == "."):
                    continue
                to = done[nh][nw]
                if fr != to and (not (fr, to) in done2):
                    done2[(fr, to)] = True
                    done2[(to, fr)] = True
                    G[fr].append((to, 1))
                    G[to].append((fr, 1))
D = dijkstra(done[sh][sw], G)
ans = D[done[gh][gw]]
if ans != INF:
    print(ans)
else:
    print(-1)

Solution (Python):
H, W = map(int, input().split())
sy, sx = map(lambda x: int(x) - 1, input().split())
gy, gx = map(lambda x: int(x) - 1, input().split())
maze = [input() for _ in range(H)]
seen = [[-1] * W for _ in range(H)] 
from collections import deque

def bfs(maze, seen, sy, sx, gy, gx):
    queue = deque()
    queue.append([sy, sx, 0])
    while queue:
        y, x, c = queue.popleft()
        if seen[y][x] > -1:
            continue
        seen[y][x] = c
        if seen[gy][gx] > -1:
            return c
        for i in range(-2, 3):
            for j in range(-2, 3):
                if (i == -1 and j == 0) or (i == 1 and j == 0) or (i == 0 and j == -1) or (i == 0 and j == 1):   
                    dy = y + i
                    dx = x + j
                    if dy < 0 or H <= dy or dx < 0 or W <= dx or seen[dy][dx] > -1:
                        continue
                    if maze[dy][dx] == '
                        continue
                    queue.appendleft([dy, dx, c])
                dy = y + i
                dx = x + j
                if dy < 0 or H <= dy or dx < 0 or W <= dx or seen[dy][dx] > -1:
                    continue
                if maze[dy][dx] == '
                    continue
                queue.append([dy, dx, seen[y][x] + 1])
    return -1

print(bfs(maze, seen, sy, sx, gy, gx))

Solution (Python):
from collections import deque

h, w = map(int, input().split())
si, sj = map(int, input().split())
ti, tj = map(int, input().split())
s = [input() for _ in range(h)]
si -= 1
sj -= 1
ti -= 1
tj -= 1

INF = 10**9
di = [-1, 0, 1, 0]
dj = [0, -1, 0, 1]
dist = [[INF] * w for _ in range(h)]
q = deque()
dist[si][sj] = 0
q.appendleft([si, sj])

while q:
    i, j = q.popleft()
    d = dist[i][j]
    for v in range(4):
        ni = i + di[v]
        nj = j + dj[v]
        if ni < 0 or ni >= h or nj < 0 or nj >= w: continue
        if s[ni][nj] == '
        if dist[ni][nj] <= d: continue
        dist[ni][nj] = d
        q.appendleft([ni, nj])
    for ei in range(-2, 3):
        for ej in range(-2, 3):
            ni = i + ei
            nj = j + ej
            if ni < 0 or ni >= h or nj < 0 or nj >= w: continue
            if s[ni][nj] == '
            if dist[ni][nj] <= d+1: continue
            dist[ni][nj] = d+1
            q.append([ni, nj])

ans = dist[ti][tj]
if ans == INF: ans = -1
print(ans)

Solution (Python):
from collections import deque
 
 
walk = [(1, 0), (-1, 0), (0, 1), (0, -1)]
warp = [(-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2),
        (-1, -2), (-1, -1), (-1, 0), (-1, 1), (-1, 2),
        (0, -2), (0, -1), (0, 1), (0, 2),
        (1, -2), (1, -1), (1, 0), (1, 1), (1, 2),
        (2, -2), (2, -1), (2, 0), (2, 1), (2, 2),]
 
 
def get_warped(walked, map_):
    warped = deque()
    while walked:
        h, w = walked.popleft()
        for i, j in warp:
            h2 = h + i
            w2 = w + j
            if -1 < h2 < H and -1 < w2 < W and map_[h2][w2] == '.':
                warped.append((h2, w2))
                map_[h2][w2] = map_[h][w] + 1
    return warped, map_
 
 
def get_walked(candidate, map_):
    walked = deque()
    while candidate:
        h, w = candidate.popleft()
        walked.append((h, w))
        for i, j in walk:
            h2 = h + i
            w2 = w + j
            if -1 < h2 < H and -1 < w2 < W and map_[h2][w2] == '.':
                candidate.append((h2, w2))
                map_[h2][w2] = map_[h][w]
    return walked, map_
 
H, W = map(int, input().split())
s_h, s_w = [i-1 for i in map(int, input().split())]
d_h, d_w = [i-1 for i in map(int, input().split())]
map_ = [list(input()) for i in range(H)]
 
candidate = deque([(s_h, s_w)])
map_[s_h][s_w] = 0
 
while candidate:
    walked, map_ = get_walked(candidate, map_)
    candidate, map_ = get_warped(walked, map_)
 
for h in range(H):
    for w in range(W):
        if map_[h][w] == '.':
            map_[h][w] = -1
 
print(map_[d_h][d_w])

Solution (Python):
from collections import deque
 
 
walk = [(1, 0), (-1, 0), (0, 1), (0, -1)]
warp = [(-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2),
        (-1, -2), (-1, -1), (-1, 0), (-1, 1), (-1, 2),
        (0, -2), (0, -1), (0, 1), (0, 2),
        (1, -2), (1, -1), (1, 0), (1, 1), (1, 2),
        (2, -2), (2, -1), (2, 0), (2, 1), (2, 2),]
 
 
def get_warped(walked, map_):
    warped = deque()
    while walked:
        h, w = walked.popleft()
        for i, j in warp:
            h2 = h + i
            w2 = w + j
            if -1 < h2 < H and -1 < w2 < W and map_[h2][w2] == '.':
                warped.append((h2, w2))
                map_[h2][w2] = map_[h][w] + 1
    return warped, map_
 
 
def get_walked(candidate, map_):
    walked = deque()
    while candidate:
        h, w = candidate.popleft()
        walked.append((h, w))
        for i, j in walk:
            h2 = h + i
            w2 = w + j
            if -1 < h2 < H and -1 < w2 < W and map_[h2][w2] == '.':
                candidate.append((h2, w2))
                map_[h2][w2] = map_[h][w]
    return walked, map_
 
H, W = map(int, input().split())
s_h, s_w = [i-1 for i in map(int, input().split())]
d_h, d_w = [i-1 for i in map(int, input().split())]
map_ = [list(input()) for i in range(H)]
 
candidate = deque([(s_h, s_w)])
map_[s_h][s_w] = 0
 
while candidate:
    walked, map_ = get_walked(candidate, map_)
    candidate, map_ = get_warped(walked, map_)
 
for h in range(H):
    for w in range(W):
        if map_[h][w] == '.':
            map_[h][w] = -1
 
print(map_[d_h][d_w])

Solution (Python):
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce, lru_cache
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def MAP1()  : return map(lambda x:int(x)-1,input().split())
def LIST()  : return list(MAP())
def LIST1() : return list(MAP1())

def bfs():
    d = [[inf]*w for i in range(h)]

    dy_dx0 = [[0,1],[1,0],[0,-1],[-1,0]] 
    dy_dx1 = [[2,2],[2,1],[2,0],[2,-1],[2,-2],[1,2],[1,1],[1,-1],[1,-2],[0,2],[0,-2],[-1,2],[-1,1],[-1,-1],[-1,-2],[-2,2],[-2,1],[-2,0],[-2,-1],[-2,-2]]

    que = deque([])
    que.append((sy, sx))
    d[sy][sx] = 0
 
    while que:
        p = que.popleft()
        if p[0] == gy and p[1] == gx:
            break
        for i in range(4):
            ny = p[0] + dy_dx0[i][0]
            nx = p[1] + dy_dx0[i][1]
 
            if 0 <= ny < h and 0 <= nx < w and s[ny][nx] != '
                que.appendleft((ny,nx))
                d[ny][nx] = d[p[0]][p[1]]

        for i in range(20):
            ny = p[0] + dy_dx1[i][0]
            nx = p[1] + dy_dx1[i][1]
 
            if 0 <= ny < h and 0 <= nx < w and s[ny][nx] != '
                que.append((ny,nx))
                d[ny][nx] = d[p[0]][p[1]] + 1 
 
    return d[gy][gx]

h, w = MAP()
sy, sx = MAP1()
gy, gx = MAP1()
s = [input() for _ in range(h)]
ans = bfs()
if ans == inf:
    print(-1)
else:
    print(ans)

Solution (Python):
from collections import deque

h, w = map(int, input().split())
c = list(map(int, input().split()))
d = list(map(int, input().split()))
s = [list(input()) for _ in range(h)]

c = [i-1 for i in c]
d = [i-1 for i in d]

t0 = [(0, 1), (0, -1), (1, 0), (-1, 0)]
t1 = []
for i in range(-2, 3):
    for j in range(-2, 3):
        t1.append((i, j))
for i in t0:
    t1.remove(i)
t1.remove((0, 0))

check = [[-1]*w for _ in range(h)]
q = deque()
q.appendleft((c[0], c[1], 0))

while q:
    y, x, b = q.pop()
    if x == d[1] and y == d[0]:
        print(b)
        exit()
    if check[y][x] == -1:
        check[y][x] = b
        for dy, dx in t0:
            ydy, xdx = y+dy, x+dx
            if (0 <= xdx < w) and (0 <= ydy < h):
                if s[ydy][xdx] == '.' and check[ydy][xdx] == -1:
                    q.append((ydy, xdx, b))
        for dy, dx in t1:
            ydy, xdx = y+dy, x+dx
            if (0 <= xdx < w) and (0 <= ydy < h):
                if s[ydy][xdx] == '.' and check[ydy][xdx] == -1:
                    q.appendleft((ydy, xdx, b+1))
print(-1)