eolymp
bolt
Try our new interface for solving problems
Problems

Turning Turtles

Turning Turtles

Military built a rectangular training ground of \textbf{w}×\textbf{h} cells to train battle turtles. Some of the cells are passable for turtles, and some of them are not. Turtles can move only parallel to the sides of the training ground. The ground is constructed in such a way that there is exactly one way to get from one passable cell to another passable cell without visiting any cell twice. It is known that turtles can run very fast along a straight line, but it is difficult for them to turn \textbf{90} degrees. So the complexity of the route is caluclated as the number of turns the turtle will make on its way from the initial to the final cell of the route. Your task is to write a program which will calculate the complexity of the route knowing its initial and final cell. \InputFile The first line contains two space-separated integers \textbf{h} and \textbf{w}, the lengths of the ground sides (\textbf{1} ≤ \textbf{w·h} ≤ \textbf{100000}). Then follows the map of the polygon --- \textbf{h} lines with \textbf{w} symbols in each. Symbol "\textbf{#}" stays for a passable cell and "\textbf{.}" stays for a non-passable cell. Line number \textbf{(h+2) contains an integer} \textbf{q}, the number of routes you have to calculate the complexity for (\textbf{1} ≤ \textbf{q} ≤ \textbf{50000}). Each of the next \textbf{q} lines contains four space-separated integers: the number of row and the number of column of the initial cell of the route, the number of row and the number of column of the final cell of the route, respectively. It is guaranteed that the initial and the final cells of the route are passable. Rows are numerated \textbf{1} to \textbf{h} from top to bottom, columns are numerated \textbf{1} to \textbf{w} from left to right. \OutputFile For each route output its complexity in a separate line.
Time limit 2 seconds
Memory limit 64 MiB
Input example #1
5 4
.#..
###.
..##
.##.
....
4
1 2 2 1
2 3 4 3
4 2 3 4
1 2 4 2
Output example #1
1
0
2
3
Author Alex Samsonov
Source Ural SU Contest. Petrozavodsk Winter Session, February 2009