Under construction

Still being built. Things may change

Junan's Rampage

Interview

Time limit2sMemory limit512 MB

Summary
Each jump clears all friends in the four straight rays from Junan's cell until the first friend in each direction; find the fewest jumps to clear the culprit's cell.
Level

Medium6 of 10

Topics
BFS, Graph, Simulation, Implementation
Solved
No attempts yet

Problem

Junan is furious. The chocolate bar he had hidden in his desk drawer to eat in secret has disappeared. Junan has started going wild. In fact, he has literally started jumping.

'Thud... thud...'

Junan wants to knock down (?) every friend around him with the shock wave of his jumps and find whoever stole the chocolate bar. He starts jumping somewhere in an N×MN \times M classroom. His wave spreads in the four directions (up, down, left, right) until it reaches a friend, knocks that friend down (?), and stops there. In other words, one jump knocks down one layer of friends. A cell whose friend has been knocked down becomes empty space. Consider the following example.

1 # 1 0 1 1 1
1 1 0 1 0 0 1
0 0 1 * 1 1 1
1 1 0 1 1 1 1
0 0 1 1 0 0 1

Junan, shown as *, is at (3,4)(3, 4), and the culprit holding the chocolate bar, shown as #, is at (1,2)(1, 2). 0 is empty space with no obstacle, and 1 is a cell where a friend is standing. The following grids show how the surviving (?) students change after each of Junan's jumps.

1 # 1 0 1 1 1
1 1 0 0 0 0 1
0 0 0 * 0 1 1
1 1 0 0 1 1 1
0 0 1 1 0 0 1
1 # 0 0 0 0 1
0 0 0 0 0 0 0
0 0 0 * 0 0 1
0 0 0 0 0 1 1
0 0 0 0 0 0 1
0 X 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 * 0 0 0
0 0 0 0 0 0 1
0 0 0 0 0 0 0

In this example Junan finds the thief after 3 jumps. The culprit is knocked down by the wave exactly like a friend, and the culprit is caught at the moment he falls.

Junan must be stopped quickly to keep the classroom at peace. Protect the classroom by telling Junan the minimum number of jumps.

Input

The first line contains the size of the classroom, NN and MM. (1≤N,M≤300)(1 \le N, M \le 300)

The second line contains Junan's position x1x_1, y1y_1 and the culprit's position x2x_2, y2y_2. (1≤x1,x2≤N(1 \le x_1, x_2 \le N, 1≤y1,y2≤M)1 \le y_1, y_2 \le M) Positions are given as (row, column), counted from 1.

Each of the next NN lines describes one row of the classroom as MM characters with no spaces. 0 is empty space, 1 is a friend, * is Junan, and # is the culprit.

Output

Print the minimum number of jumps Junan needs to catch the culprit.

Hint

Like the ripples from a stone dropped into a lake, the wave spreads in the four directions (up, down, left, right) and keeps spreading until it meets an obstacle (a friend).

# 0 0 0 0
1 1 1 1 1
0 0 0 0 *

After the first jump, the classroom above becomes the following.

# 0 0 0 0
0 0 0 0 0
0 0 0 0 *

One more jump then lets the wave reach #, and the culprit is caught.

Examples3

  1. Example 1

    Input
    5 7
    3 4 1 2
    1#10111
    1101001
    001*111
    1101111
    0011001
    
    Expected output
    3
    
  2. Example 2

    Input
    3 5
    3 5 1 1
    #0000
    11111
    0000*
    
    Expected output
    2
    
  3. Example 3

    Input
    3 3
    2 2 1 1
    #00
    0*0
    000
    
    Expected output
    1