Obstacle Course

No attempts yetTime limit1sMemory limit128 MB

Problem

Consider an $N \times N$ ($1 \le N \le 100$) square field made of $1 \times 1$ tiles. Some tiles cannot be crossed by cows and are marked with x. Below is one such $5 \times 5$ field:

. . B x .
. x x A .
. . . x .
. x . . .
. . x . .

Bessie the cow starts on the tile marked A and wants to reach the tile marked B to lick the salt block there. Cows dislike turning and may only move parallel to the edges of the field, that is, up, down, left, or right by one tile at a time. Bessie may face any direction when she starts and when she finishes. Determine the minimum number of $90$-degree turns on any path from A to B. It is guaranteed that B is reachable from A.

Input

  • Line 1: a single integer $N$.
  • Lines $2$ to $N+1$: line $i+1$ describes row $i$ of the field as $N$ characters, each one of ., x, A, or B, with no spaces.

Output

  • Line 1: a single integer, the minimum number of $90$-degree turns on a path from A to B.

Hint

In the first example the cow needs at least $2$ turns. For instance, it can face south and move south one tile, turn to face west and move west two tiles, then turn to face south and move south one tile onto B.