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 Java (OpenJDK 11.0.6) (judge verdict: Wrong Answer)


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;

public class Main {

	public static void main(String[] args) {
	
			InputStream inputStream = System.in;
			OutputStream outputStream = System.out;
			FastInput in = new FastInput(inputStream);
			PrintWriter out = new PrintWriter(outputStream);
			ABC176D solver = new ABC176D();
			solver.solve(1, in, out);
			out.close();
		}

		static class ABC176D {
			
			static int h,w;
			static int [][]map;
			static char [][]grid;
			static boolean [][]visit;
			static int ans;
			static int ch,cw,dh,dw;

			public void solve(int testNum, FastInput in, PrintWriter out) {

				h = in.nextInt();
				w = in.nextInt();
				
				 ch = in.nextInt()-1;
				 cw = in.nextInt()-1;
				 dh = in.nextInt()-1;
				 dw = in.nextInt()-1;
				
				grid = in.nextChar2DArray(h);
				map = new int [h][w];
				visit = new boolean[h][w];
				
				for(int i = 0 ; i < h ;i++) {
					for(int j = 0 ; j < w ;j++) {
						map[i][j] = 1000000000;
					}
			}
				
				int []mh = {-2,-1,0,1,2};
				int []mw = {-2,-1,0,1,2};
				
				int []qh = {1,0,-1,0};
				int []qw = {0,1,0,-1};
 				
				boolean start = false;
				
				for(int i = 0 ; i < h ;i++) {
					for(int j = 0 ; j < w ;j++) {
						if(!start && grid[i][j] != '#') {
							map[i][j] = 0;
							start = true;
						}
						else if(!start && grid[i][j] == '#'){
							continue;
						}
						for(int x = 0 ; x < 5 ;x++) {
							for(int y = 0 ; y < 5 ;y++) {
								int ph = i + mh[x];
								int pw = j + mw[y];
								
								if(ph < 0 || pw < 0) {
									continue;
								}
								if(ph >= h || pw >= w) {
									continue;
								}
								if(grid[ph][pw] == '#') {
									continue;
								}
								map[ph][pw] = Math.min(map[ph][pw],map[i][j] +1);
							}
						}
							
							for(int x = 0 ; x < 4 ;x++) {
									int ph = i + qh[x];
									int pw = j + qw[x];
									
									if(ph < 0 || pw < 0) {
										continue;
									}
									if(ph >= h || pw >= w) {
										continue;
									}
									if(grid[ph][pw] == '#') {
										continue;
									}
									map[ph][pw] = Math.min(map[ph][pw],map[i][j]);
						}
					}	
				}
				
				if(map[dh][dw] == 1000000000) {
					out.println(-1);
					return;
				}
				
				out.println(Math.abs(map[ch][cw] - map[dh][dw]));
				
				
				}
				
				
				
			}

		
		static class FastInput{
			private final BufferedReader in;
			private String[] buff;
			private int index = 0;
			
			public FastInput(InputStream is) {
				in = new BufferedReader(new InputStreamReader(is));
			}
			String nextLine() {
				try {
					return in.readLine();
				} catch (IOException e) {
					e.printStackTrace();
				}
				return null;
			}
			String next() {
				while (buff == null || index >= buff.length) {
					buff = nextLine().split(" ");
					index = 0;
				}
				return buff[index++];
			}
			int nextInt() {
				return Integer.parseInt(next());
			}
			
			long nextLong() {
				return Long.parseLong(next());
			}
			
			Double nextDouble() {
				return Double.parseDouble(next());
			}
			
			BigInteger nextBigInteger() {
				return new BigInteger(next());
			}
			
			BigDecimal nextBigDecimal() {
				return new BigDecimal(next());
			}
			
			public int[] nextIntArray(final int n) {
				final int[] res = new int [n];
				
				for(int i = 0 ; i < n ;i++) {
					res[i] = nextInt();
				}
				return res;
			}
			
			public long[] nextLongArray(final int n) {
				final long[] res= new long [n];
				
				for(int i = 0 ; i < n ;i++) {
					res[i] = nextLong();
				}
				return res;
			}
			
			public double[] nextDoubleArray(final int n ) {
				final double[] res = new double [n];
				
				for(int i = 0 ; i < n ;i++) {
					res[i] = nextDouble();
				}
				return res;
			}
			
			
			public int[][] nextInt2DArray(final int n , final int m){
				int [][]res = new int [n][m];
				
				for(int i = 0 ; i < n ;i++) {
					for(int j = 0 ; j < m ;j++) {
						res[i][j] = nextInt();
					}
				}
				return res;
			}
			public long[][] nextLong2DArray(final int n , final int m){
				long [][]res = new long [n][m];
				
				for(int i = 0 ; i < n ;i++) {
					for(int j = 0 ; j < m ;j++) {
						res[i][j] = nextLong();
					}
				}
				return res;
			}
			
			public char[][] nextChar2DArray(final int n){
				char [][]res = new char [n][];
				
				for(int i = 0 ; i < n ;i++) {
					String s = next();
					res[i] = s.toCharArray();
				}
				
				return res;
			}
		}

}