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 C# (.NET Core 3.1.201) (judge verdict: Runtime Error)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;
public class MainClass
{
	public const long Giri = 1000000007;
	public const long DpInf = 99999999999999;
	public class Pos
	{
		public int X{get;set;}
		public int Y{get;set;}
	}
	
	public class Node
	{
		public int Index {get;set;}
		public List<int> Edges{get;set;}
	}
	
	public class Far
	{
		public int Index {get;set;}
		public long Distance {get;set;}
	}
	
	public static void Main(string[] args)
	{
		var p = Input().ToInt();
		var h = p[0];
		var w = p[1];
		p = Input().ToInt();
		var cy = p[0]-1;
		var cx = p[1]-1;
		p = Input().ToInt();
		
		var dy = p[0]-1;
		var dx = p[1]-1;
		
		var map = new string[h];
		for(int i = 0; i < h; i++)
		{
			map[i] = Console.ReadLine();
		}

		var area = new int[h,w];
		int areaNext = 1;
		

		
		//var walls = new Stack<Pos>();
		for(int y = 0; y < h; y++)
		{
			for(int x = 0;x < w; x++)
			{
				if(map[y][x] == '#')
				{
					area[y, x] = -1;
					//walls.Push(new Pos(){X=x, Y=y});
					continue;
				}
				
				if(area[y, x] != 0)
					continue;
				
				var queue = new Queue<Pos>();
				queue.Enqueue(new Pos(){X=x, Y=y});
				bool areaWrite = false;
				while(queue.Count > 0)
				{
					var pos = queue.Dequeue();
					if(pos.X < 0 || pos.Y < 0 || pos.X >= w || pos.Y >= h)
						continue;
					if(map[pos.Y][pos.X] == '#')
					{
						area[pos.Y, pos.X] = -1;
						//walls.Push(new Pos(){X=x, Y=y});
						continue;
					}
					if(area[pos.Y, pos.X] != 0)
						continue;
						
					queue.Enqueue(new Pos(){X=pos.X+0, Y=pos.Y+1});
					queue.Enqueue(new Pos(){X=pos.X+0, Y=pos.Y-1});
					queue.Enqueue(new Pos(){X=pos.X+1, Y=pos.Y+0});
					queue.Enqueue(new Pos(){X=pos.X-1, Y=pos.Y+0});
					area[pos.Y, pos.X] = areaNext;
					areaWrite = true;
				}
				if(areaWrite)
					areaNext++;
				
			}
			
		}
		
		var edges = new bool[areaNext, areaNext];
		//var edges = new HashSet<int>[areaNext] ;
		/*
		for(int i = 0; i < areaNext; i++)
			for(int j = 0; j < areaNext; j++)
				edges[i, j] = DpInf;
				*/
		/*
		for(int y = 0; y < h; y++)
		{
			for(int x = 0; x < w; x++)
			{
				Console.Write("{0}, ", area[y, x]);
			}
			Console.WriteLine();
			}*/
		/*
		while(walls.Count > 0)
		{
			var pos = walls.Pop();
			var x = pos.X;
			var y = pos.Y;
			var inareas = new HashSet<int>();
			for(int i = -1; i <= 1; i++)
			{
				if(i == 0) continue;
				if(y + i < 0 || y + i >= h) continue;
				for(int j = -1; j <= 1; j++)
				{
					if(j == 0) continue;
					if(x + j < 0 || x + j >= w) continue;
					if(area[y+i, x+j] == -1) continue;
					inareas.Add(area[y+i, x+j]);
				}
			}
					//Console.WriteLine(" > {0}, {1}", x, y);
			
			foreach(int t in inareas)
			{
				foreach(int s in inareas)
				{
					//Console.WriteLine("{0}, {1}", s, t);
				
					edges[s, t] = true;
					edges[t, s] = true;
				}
			}
		}*/
		
		
		for(int y = 0; y < h; y++)
		{
			for(int x = 0; x < w; x++)
			{
				if(area[y, x] <= 0)
					continue;
				//Console.WriteLine(">{0} {1}", x, y);
				for(int i = -2; i <= 2; i++)
				{
					if(y + i < 0 || y + i >= h) continue;
					for(int j = -2; j <= 2; j++)
					{
						if(x + j < 0 || x+j >= w) continue;
						if(area[y+i, x+j] == -1) continue;
						if(area[y, x] == area[y+i, x+j]) continue;
						edges[area[y, x], area[y+i, x+j]] = true;
						edges[area[y+i, x+j], area[y, x]] = true;
					}
				}
			}
		}
		/*
		for(int y = 0; y < areaNext; y++)
		{
			for(int x = 0; x < areaNext; x++)
			{
				Console.Write("{0}, ", edges[y, x]);
			}
			Console.WriteLine();
		}
			*/
		
		//var mat = new long[areaNext, areaNext];
		
		//warshal
		/*
		for(int i = 1; i < areaNext; i++)
		{
			for(int j = 1; j < areaNext; j++)
			{
				for(int k = 1; k < areaNext; k++)
				{
					edges[j, k] = Math.Min(edges[j, k], edges[j, i] + edges[i, k]);
				}
			}
		}
		*/
		
		int goalArea = area[dy,dx];
		
		var c = new Queue<Far>();
		c.Enqueue(new Far(){Index=area[cy,cx], Distance=0});
		var visited = new long[areaNext];
		for(int i = 0 ; i < visited.Length; i++)
			visited[i] = int.MaxValue;
		while(c.Count > 0)
		{
			Far f = c.Dequeue();
			int cur = f.Index;
			if(visited[cur] <= f.Distance)
				continue;
			visited[cur] = f.Distance;
			
			for(int i = 1; i < areaNext; i++)
			{
				if(cur != i && edges[cur, i])
				{
					c.Enqueue(new Far(){Index=i, Distance=f.Distance+1});
				}
			}
			
		
		}
		/*
		for(int i = 1; i < areaNext; i++)
		{
			for(int j = 1; j < areaNext; j++)
			{
				
			}
		}
		*/
		/*
		for(int y = 0; y < h; y++)
		{
			for(int x = 0; x < w; x++)
			{
				Console.Write("{0}, ", area[y, x]);
			}
			Console.WriteLine();
		}*/
		
		if(area[cy,cx] == goalArea)
		{
			Console.WriteLine(0);
		}
		else
		{
			long ttt = visited[goalArea];
			if(ttt == int.MaxValue)
				ttt = -1;
			Console.WriteLine(ttt);
		}
		/*
		else if(edges[area[cy,cx], area[dy,dx]] >= DpInf)
		{
			Console.WriteLine(-1);
		}
		else
		{
			Console.WriteLine(edges[area[cy-1,cx-1], area[dy-1,dx-1]]);
		}
		*/
	}

	
	
	static long GCD(long a, long b)
	{
		var v = new[] { a, b };
		while (v[1] != 0) { v = new[] { v[1], v[0] % v[1] }; }
		return v[0];
	}
	public static string[] Input() { return Console.ReadLine().Split(); }
}

public static class Smarter
{
	public static int ToInt(this string str) { return int.Parse(str); }
	public static long ToLong(this string str) { return long.Parse(str); }
	public static int[] ToInt(this string[] str, int offset = 0)
	{
		var r = new int[str.Length];
		for(int i = 0; i < str.Length; i++) r[i] = str[i].ToInt() + offset;
		return r;
	}
}