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 Rust (1.42.0) (judge verdict: Wrong Answer)

use proconio::{fastout, input};
use std::io::*;
use std::str::FromStr;

use std::cmp::{max, min};
use std::collections::HashMap;
use std::collections::HashSet;

fn main() {
    input! {
        h: usize,
        w: usize,
        ch: usize,
        cw: usize,
        dh: usize,
        dw: usize,
        m: [String; h],
    }

    let mut map: Vec<Vec<char>> = vec![];
    let mut map_status: Vec<Vec<i64>> = vec![vec![0; w]; h];
    let mut hm: HashMap<i64, HashSet<i64>> = HashMap::new();

    for i in 0..h {
        map.push(m[i].chars().collect());
    }

    let mut color = 1;
    for i in 0..h {
        for j in 0..w {
            if map[i][j] == '#' || map_status[i][j] != 0 {
                continue;
            }
            coloring(&map, &mut map_status, i, j, color);
            color += 1;
        }
    }

    if map_status[ch - 1][cw - 1] == map_status[dh - 1][dw - 1] {
        println!("0");
        return;
    }

    for i in 0..h {
        for j in 0..w {
            if map[i][j] == '#' {
                continue;
            }
            // if i != 0 && j != 0 && i != h - 1 && j != w - 1 {
            //     let cc = map[i][j];
            //     if map[i - 1][j] == cc
            //         && map[i + 1][j] == cc
            //         && map[i][j + 1] == cc
            //         && map[i][j - 1] == cc
            //     {
            //         continue;
            //     }
            // }
            connect_check(&mut map_status, i as i64, j as i64, &mut hm);
        }
    }

    let ans = bfs(
        map_status[ch - 1][cw - 1],
        map_status[dh - 1][dw - 1],
        hm,
        color,
    );
    println!("{}", ans);

    // for i in 0..map_status.len() {
    //     println!("{:?}", map_status[i]);
    // }
}

use std::collections::VecDeque;

fn bfs(sc: i64, tc: i64, hm: HashMap<i64, HashSet<i64>>, color: i64) -> i64 {
    let mut q: VecDeque<(i64, i64)> = VecDeque::new();
    let mut done = vec![false; color as usize + 1];
    q.push_back((sc, 0));

    let mut ans = -1;
    while let Some(cn) = q.pop_front() {
        if done[cn.0 as usize] {
            continue;
        }
        match hm.get(&cn.0) {
            Some(s) => {
                for i in s {
                    if *i == tc {
                        ans = cn.1 + 1;
                        break;
                    }

                    if !done[*i as usize] {
                        q.push_back((*i, cn.1 + 1));
                    }
                }
            }
            None => continue,
        }
        done[cn.0 as usize] = true;
    }
    ans
}

fn connect_check(
    map_color: &mut Vec<Vec<i64>>,
    h: i64,
    w: i64,
    hm: &mut HashMap<i64, HashSet<i64>>,
) {
    let cc = map_color[h as usize][w as usize];

    for i in h - 2..=h + 2 {
        for j in w - 2..=w + 2 {
            if i < 0 || i >= map_color.len() as i64 || j < 0 || j >= map_color[0].len() as i64 {
                continue;
            }
            let tc = map_color[i as usize][j as usize];
            if tc == 0 || tc == cc {
                continue;
            }
            let mut current = hm.entry(cc).or_insert(HashSet::new());
            current.insert(tc);
        }
    }
}

fn coloring(map: &Vec<Vec<char>>, map_color: &mut Vec<Vec<i64>>, h: usize, w: usize, color: i64) {
    if map[h][w] == '#' || map_color[h][w] != 0 {
        return;
    }
    map_color[h][w] = color;
    if w + 1 < map[0].len() {
        coloring(map, map_color, h, w + 1, color);
    }
    if w >= 1 {
        coloring(map, map_color, h, w - 1, color);
    }
    if h + 1 < map.len() {
        coloring(map, map_color, h + 1, w, color);
    }
    if h >= 1 {
        coloring(map, map_color, h - 1, w, color);
    }
}