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 Go (1.14.1) (judge verdict: Wrong Answer)

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	sc := NewScanner()
	H, W := sc.NextInt(), sc.NextInt()
	Ch, Cw := sc.NextInt()-1, sc.NextInt()-1
	Dh, Dw := sc.NextInt()-1, sc.NextInt()-1
	S := make([]string, H)
	groups := make([][]int, H)
	checked := make([][]bool, H)

	for i := 0; i < H; i++ {
		S[i] = sc.Next()
		groups[i] = make([]int, W)
		checked[i] = make([]bool, W)
	}

	check := func(y, x int) bool {
		return 0 <= y && y < H && 0 <= x && x < W
	}

	group := 1
	var dfs func(x, y int)
	dfs = func(y, x int) {
		groups[y][x] = group

		for i := x - 1; i <= x+1; i++ {
			for j := y - 1; j <= y+1; j++ {
				if !check(j, i) {
					continue
				}

				if abs(x-i)+abs(y-j) != 1 {
					continue
				}
				if S[j][i] == '#' {
					continue
				}

				if groups[j][i] != 0 {
					continue
				}
				dfs(j, i)
			}
		}
	}

	groups[0][0] = 1
	for i := 0; i < W; i++ {
		for j := 0; j < H; j++ {
			if S[j][i] == '#' {
				continue
			}
			if groups[j][i] != 0 {
				continue
			}
			dfs(j, i)
			group++
		}
	}

	edge := make([]map[int]bool, group+1)
	for i := 0; i < group+1; i++ {
		edge[i] = map[int]bool{}
	}

	var dfs2 func(y, x int)
	dfs2 = func(y, x int) {
		for i := x - 2; i <= x+2; i++ {
			for j := y - 2; j <= y+2; j++ {
				if !check(j, i) {
					continue
				}

				if groups[j][i] == 0 {
					continue
				}
				if x-i == 0 && y-j == 0 {
					continue
				}
				if checked[j][i] {
					continue
				}
				checked[j][i] = true
				if groups[j][i] != groups[y][x] {
					edge[groups[y][x]][groups[j][i]] = true
					edge[groups[j][i]][groups[y][x]] = true
				}
			}
		}
	}

	for i := 0; i < H; i++ {
		for j := 0; j < W; j++ {
			if S[i][j] == '#' {
				continue
			}
			dfs2(i, j)
		}
	}
	start := groups[Ch][Cw]
	if start == groups[Dh][Dw] {
		fmt.Println(0)
		return
	}

	queue := make([]int, 1)
	queue[0] = start
	checkedP := make([]bool, group)
	checkedP[start] = true
	turn := 1
	for 0 < len(queue) {
		nextQueue := make([]int, 0)

		for left := 0; left < len(queue); left++ {

			for nxt, _ := range edge[queue[left]] {
				if checkedP[nxt] {
					continue
				}
				if nxt == groups[Dh][Dw] {
					fmt.Println(turn)
					return
				}
				checkedP[nxt] = true
				nextQueue = append(nextQueue, nxt)
			}
		}
		queue = nextQueue
		turn++
	}
	fmt.Println(-1)
}
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextFloat() float64 {
	v, _ := strconv.ParseFloat(s.Next(), 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}