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 Ruby (2.7.1) (judge verdict: Time Limit Exceeded)

h, w = gets.split.map(&:to_i)
ch, cw = gets.split.map(&:to_i)
dh, dw = gets.split.map(&:to_i)
grid = []
h.times do
  grid << gets.chomp.split("")
end
memo = Array.new(h) { Array.new(w) }
current_group = 0
h.times do |row|
  current_grid = grid[row]
  current_memo = memo[row]
  w.times do |col|
    next if current_memo[col]
    next if current_grid[col] == "#"
    queue = [[row, col]]
    until queue.empty?
      current_row, current_col = queue.shift
      memo[current_row][current_col] = current_group
      if current_row <= h - 2 && grid[current_row + 1][current_col] == "." && memo[current_row + 1][current_col].nil?
        queue << [current_row + 1, current_col]
      end
      if current_row > 0 && grid[current_row - 1][current_col] == "." && memo[current_row - 1][current_col].nil?
        queue << [current_row - 1, current_col]
      end
      if current_col <= w - 2 && grid[current_row][current_col + 1] == "." && memo[current_row][current_col + 1].nil?
        queue << [current_row, current_col + 1]
      end
      if current_col > 0 && grid[current_row][current_col - 1] == "." && memo[current_row][current_col - 1].nil?
        queue << [current_row, current_col - 1]
      end
    end
    current_group += 1
  end
end

edges = Array.new(current_group) { Hash.new }
# up = 0
# down = [2, w - 1].min
# current_rows = []
# (up..down)
memo.each_with_index do |row, r_index|
  current_rows = []
  current_rows << memo[r_index - 2] if r_index > 1
  current_rows << memo[r_index - 1] if r_index > 0
  current_rows << memo[r_index] if r_index
  current_rows << memo[r_index + 1] if r_index <= h - 2
  current_rows << memo[r_index + 2] if r_index <= h - 3
  row.each_with_index do |col, c_index|
    next if col.nil?
    # current_rows = []
    # current_rows << memo[r_index - 2] if r_index > 1
    # current_rows << memo[r_index - 1] if r_index > 0
    # current_rows << memo[r_index] if r_index
    # current_rows << memo[r_index + 1] if r_index <= h - 2
    # current_rows << memo[r_index + 2] if r_index <= h - 3
    current_rows.each do |current_row|
      left = c_index - 2
      left = 0 if left < 0
      right = c_index + 2
      right = w - 1 if right > w - 1
      (left..right).each do |i|
        next unless cur = current_row[i]
        next if cur == col
        edges[col][cur] = true
      end
    end
  end
end

start = memo[ch - 1][cw - 1]
goal = memo[dh - 1][dw - 1]
if start == goal
  puts 0
  exit
end
# current, from, cost
queue = [[start, start, 0]]

ans = -1
visited = Array.new(current_group, false)
until queue.empty?
  cur, from, cost = queue.shift
  visited[cur] = true
  edges[cur].keys.each do |key|
    if key == goal
      ans = cost + 1
      break
    end
    next if key == from || visited[key]
    queue << [key, cur, cost + 1]
  end
  break if ans != -1
end
puts ans