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++ (GCC 9.2.1) (judge verdict: Time Limit Exceeded)

#pragma GCC optimize("-O3")
/*
				C++
				encoding: UTF-8
				Last Modified: 22/Aug/20 06:53:25 PM
*/

#include <bits/stdc++.h>
#ifdef H4wk3ye
#	include <prettypr.hpp>
#endif

using namespace std;

#define int int64_t

template < typename T > void print (const T& t) {
	cout << t << '\n';
}

template < typename T, typename U, typename... ARGS >
void print (const T& t, const U& u, const ARGS&... args) {
	cout << t << " ";
	print (u, args...);
}

const int N = 1e3 + 5;
map< pair< int, int >, int > mp;
char arr[N][N];
int vis[N][N];
int ans[N][N];
int n, m;
int x, y;
int g, h;
int scc;
vector< pair< int, int > > edges;

bool check (int a, int b) {
	return a >= 0 and a < n and b >= 0 and b < m;
}

vector< pair< int, int > > neighbors (int a, int b) {
	return {{a - 1, b}, {a + 1, b}, {a, b - 1}, {a, b + 1}};
}

void dfs (int a, int b) {
	vis[a][b] = 1;
	mp[{a, b}] = scc;
	if (check (a - 1, b) and arr[a - 1][b] == '.' and !vis[a - 1][b]) dfs (a - 1, b);
	if (check (a + 1, b) and arr[a + 1][b] == '.' and !vis[a + 1][b]) dfs (a + 1, b);
	if (check (a, b - 1) and arr[a][b - 1] == '.' and !vis[a][b - 1]) dfs (a, b - 1);
	if (check (a, b + 1) and arr[a][b + 1] == '.' and !vis[a][b + 1]) dfs (a, b + 1);
}

signed main () {
	ios_base::sync_with_stdio (false), cin.tie (nullptr);
	cin >> n >> m;
	cin >> x >> y;
	x--;
	y--;
	cin >> g >> h;
	g--;
	h--;

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			cin >> arr[i][j];
			ans[i][j] = 1e9;
		}
	}

	dfs (x, y);


	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			if (!vis[i][j] and arr[i][j] == '.') {
				++scc;
				dfs (i, j);
			}
		}
	}


	// find the number of islands first and group them
	priority_queue< pair< int, pair< int, int > >, vector< pair< int, pair< int, int > > >, greater<> > q;
	q.push ({0, {x, y}});

	while (!q.empty ()) {
		auto [dis, p] = q.top ();
		q.pop ();

		int a = p.first, b = p.second;
		ans[a][b] = min (ans[a][b], dis);

		if (check (a - 1, b) and arr[a - 1][b] == '.' and ans[a - 1][b] > dis)
			q.push ({dis, {a - 1, b}});

		if (check (a + 1, b) and arr[a + 1][b] == '.' and ans[a + 1][b] > dis)
			q.push ({dis, {a + 1, b}});
		if (check (a, b - 1) and arr[a][b - 1] == '.' and ans[a][b - 1] > dis)
			q.push ({dis, {a, b - 1}});
		if (check (a, b + 1) and arr[a][b + 1] == '.' and ans[a][b + 1] > dis)
			q.push ({dis, {a, b + 1}});


		for (int i = -2; i <= 2; ++i) {
			for (int j = -2; j <= 2; ++j) {
				if (i == 0 and j == 0) continue;
				int c = a + i, d = b + j;
				if (check (c, d) and arr[c][d] == '.' and ans[c][d] > dis + 1) {
					q.push ({dis + 1, {c, d}});
				}
			}
		}
	}
	if (ans[g][h] == 1e9) ans[g][h] = -1;
	print (ans[g][h]);

	return 0;
}