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 in PyPy3 (7.3.0) (judge verdict: Time Limit Exceeded)

import sys
import re
import queue
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []

def isIn(H,W,x,y):
	if 0<= x and x <= H-1 and 0<=y and y<=W-1:
		return True
	else:
		return False

def main():
	H,W = i_map()

	C_h,C_w = i_map()
	D_h,D_w = i_map()

	S = []
	for i in range(0,H):
		tmp = s_input()
		tmp2 = []
		for j in range(0,W):
			tmp2.append(tmp[j])
		S.append(tmp2)

	start = [C_h-1,C_w-1]
	exit = [D_h-1,D_w-1]

	rel_matrix = [([[] for i in range(W)]) for i in range(H)]
	dir = [[-1,0],[1,0],[0,-1],[0,1]]
	for i in range(0,H):
		for j in range(0,W):
			for v_dir in dir:
				if isIn(H,W,i+v_dir[0],j+v_dir[1]):
					if(S[i][j] == '.' and S[i+v_dir[0]][j+v_dir[1]] == '.'):
						rel_matrix[i][j].append((i+v_dir[0],j+v_dir[1]))

	color = [['white' for i in range(W)] for j in range(H)]
	cost = [[-1 for i in range(W)] for j in range(H)]
	
	q =  queue.Queue()
	cost[start[0]][start[1]] = 0
	color[start[0]][start[1]]='gray'
	q.put(start)
	while(not q.empty()):
		u = q.get()
		for rel in rel_matrix[u[0]][u[1]]:
			if color[rel[0]][rel[1]] == 'white':
				cost[rel[0]][rel[1]] = 0
				color[rel[0]][rel[1]]  = 'gray'
				q.put(rel)
		color[u[0]][u[1]] = 'black'

	current_cost = 0
	while True:
		currentList = []
		for i in range(0,H):
			for j in range(0,W):
				if(cost[i][j] == current_cost):
					currentList.append((i,j))
		if(len(currentList)==0):
			break

		nextList  = []
		for kouho in currentList:
			for i in range(-2,3):
				for j in range(-2,3):
					if(kouho[0]+i >= 0 and kouho[0]+i <= H-1 and kouho[1] + j >= 0 and kouho[1] + j <= W-1):
						if(S[kouho[0]+i][kouho[1]+j]=='.' and color[kouho[0]+i][kouho[1]+j] == 'white'):
							nextList.append((kouho[0]+i,kouho[1]+j))
		nextList = set(nextList)
		for next in nextList:
			if(color[next[0]][next[1]]=='white'):
				q2 = queue.Queue()
				cost[next[0]][next[1]] =current_cost+1
				color[next[0]][next[1]] = 'gray'
				q2.put(next)
				while(not q2.empty()):
					u = q2.get()
					for rel in rel_matrix[u[0]][u[1]]:
						if color[rel[0]][rel[1]] == 'white':
							cost[rel[0]][rel[1]] = current_cost+1
							color[rel[0]][rel[1]]  = 'gray'
							q2.put(rel)
					color[u[0]][u[1]] = 'black'
		current_cost += 1


	print(cost[D_h-1][D_w-1])



if __name__ == '__main__':
	main()