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: Wrong Answer)

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtCoder2 {
	class Program {
		static void Main(string[] args) {

			//出力の都度更新をやめる
			var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
			Console.SetOut(sw);


			Ans();

			//更新する
			Console.Out.Flush();
			
		}
		public static void Ans() {

			var inputArray = retreiveLongArray();
			var inputCount = 2;

			// 不正な入力は処理しない
			if (inputArray.Length != inputCount) {
				Console.WriteLine("invalid input");
				return;
			}
			var H = (int)inputArray[0];
			var W = (int)inputArray[1];

			var CArray = retreiveLongArray();
			var ch = (int)CArray[0] - 1;
			var cw = (int)CArray[1] - 1;


			var DArray = retreiveLongArray();
			var dh = (int)DArray[0] - 1;
			var dw = (int)DArray[1] - 1;


			var map = new bool[H, W];
			for (int i = 0; i < H; i++) {
				var a = retreiveStringArray()[0];
				for (int j = 0; j < W; j++) {
					map[i, j] = a[j] == '.';
				}
			}

			var isReached = new bool[H, W];
			for (int i = 0; i < H; i++) {
				for (int j = 0; j < W; j++) {
					isReached[i, j] = false;
				}
			}


			int[][] steps = {
				new int[]{ -1,0},
				new int[]{ 1,0},
				new int[]{ 0,-1},
				new int[]{ 0,1}
			};
			var tohoSearching = new List<Point>();
			var warpSearching = new List<Point>();

			tohoSearching.Add(new Point(ch, cw));

			for (int ans = 0; ans < H*W; ans++) {
				if (tohoSearching.Count()==0 && warpSearching.Count()==0) {
					Console.WriteLine(-1);
					return;
				}

				//まず徒歩圏内でBFS
				for (int i = 0; i < tohoSearching.Count; i++) {

					var h = tohoSearching[i]._h;
					var w = tohoSearching[i]._w;
					foreach (var step in steps) {

						var nexth = h + step[0];
						var nextw = w + step[1];
						if (h == nexth && w == nextw) {
							Console.WriteLine(ans);
							return;
						}
						if (0 <= nexth && nexth < H && 0 <= nextw && nextw < W) {

							if (!map[nexth,nextw]) {
								continue;
							}
							if (isReached[nexth, nextw]) {
								continue;
							}
							isReached[nexth, nextw] = true;
							tohoSearching.Add(new Point(nexth, nextw));
						}
					}

				}

				//次にワープで行ける場所を調べる

				foreach (var point in tohoSearching) {
					var hw = point._h;
					var ww = point._w;

					for (int i = Math.Max(0,hw-2); i < Math.Min(H,hw+3); i++) {
						for (int j = Math.Max(0, ww - 2); j < Math.Min(W, ww + 3); j++) {
							if (i==dh && j==dw) {
								Console.WriteLine(ans+1);
								return;
							}


							if (!map[i, j]) {
								continue;
							}
							if (isReached[i, j]) {
								continue;
							}
							isReached[i, j] = true;
							warpSearching.Add(new Point(i, j));
						}
					}
				}
				tohoSearching = warpSearching;
				warpSearching = new List<Point>();
			}

		}



		public  struct Point
		{
			public readonly int _h;
			public readonly int _w;

			public Point(int h,int w) {
				_h=h;
				_w = w;
			}
		}

		/// <summary>
		/// スペースで区切られた文字列配列の入力を受け取る
		/// </summary>
		/// <returns></returns>
		private static string[] retreiveStringArray() {
			var inputArray = Console.ReadLine().Split(' ');
			return inputArray;
		}

		/// <summary>
		/// スペースで区切られた整数配列の入力を受け取る
		/// </summary>
		/// <returns></returns>
		private static long[] retreiveLongArray() {
			var inputArray = Console.ReadLine().Split(' ');
			var longArray = inputArray.Select(s => long.Parse(s));
			return longArray.ToArray();
		}


		/// <summary>
		/// 2組みずつの入力データ(数値)からTuppleListを作成する
		/// </summary>
		/// <param name="N"></param>
		/// <returns></returns>
		private static List<Tuple<long, long>> createLongTuppleList(long N) {
			var inpList = new List<Tuple<long, long>>();

			for (int i = 0; i < N; i++) {
				var inp = retreiveLongArray();
				var item1 = inp[0];
				var item2 = inp[1];
				inpList.Add(new Tuple<long, long>(item1, item2));
			}
			return inpList;
		}
	}
}