Whenever William steps on a floor made with tiles, he plays the "Hot Lava" game. In this game the floor is lava, that is, some of the tiles are to be absolutely avoided.
This particular floor is a grid made of tiles. Some of those tiles are red, the remaining ones are white. William wants to get from tile to tile using only white tiles, as quickly as possible. He can move diagonally from one tile to an adjacent one (by vertex). Moreover, he can move vertically or horizontally from one tile to one that is adjacent by edge, also to the next tile in the same direction, by doing a long jump.
Task
Help William compute the quickest route, as the number of jumps (short or long jumps) needed.
Input data
The first line contains two integers and , the height and the width of the grid, respectively.
Other lines follow, each containing a string of characters (either .
or #
).
Output data
You need to write a single line with an integer: the minimum number of jumps needed.
Constraints and clarifications
- The and positions in the grid will always contain a
.
and never a#
. - There will always be some route from to .
# | Points | Constraints |
---|---|---|
1 | 5 | Examples. |
2 | 10 | . That is: the grid is a line. |
3 | 20 | There are no # in the grid. |
4 | 25 | |
5 | 40 | No additional constraints. |
Example 1
stdin
3 4
.###
....
###.
stdout
3
Explanation
William could follow the path denoted by o
's:
o###
.oo.
###o
Another possible solution:
o###
.o.o
###o
Example 2
stdin
5 5
.##..
.##..
#.#..
#.##.
.##..
stdout
5
Explanation
There is a unique best solution:
o##..
o##..
#o#o.
#.##o
.##.o