B. A King With No Name
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Killua is a smart anime character who enjoys playing chess. However, in his world, the chess pieces behave a bit differently. He currently has a special piece called a $$$Half-Queen$$$. Its movement follows these rules:

  1. It cannot move outside the board, which has $$$N$$$ rows and $$$N$$$ columns, with the upper-leftmost cell being $$$(0, 0)$$$.
  2. It cannot move through other pieces (no jumping over pieces).
  3. It can move horizontally and along the minor diagonal. Meaning if it is currently on cell $$$(x, y)$$$, it can move to either $$$(x + i, y - i)$$$ or $$$(x, y + i)$$$, for all integers $$$i \in \mathbb{Z}$$$, as long as the previous two conditions remain valid.
Figure 1. Example of the Half-Queen's (at cell x=4,y=2) possible movement.

You are given an $$$N×N$$$ chessboard. One cell contains Killua's half-queen, and another cell contains an enemy King. Other cells may be empty or may contain pieces (which cannot be captured or crossed).

Your task is to determine the minimum number of moves required for the Half-Queen to reach the enemy King without capturing or passing through any other piece. If it is impossible to reach the King, report that.

Input

The first line will contain an integer $$$N$$$ ($$$2\le N \le 1000$$$). The next $$$N$$$ lines each contain a string of length $$$N$$$, representing the board. The board contains:

  • "Q" — the Half-Queen
  • "K" — the enemy King
  • "." — an empty cell
  • "#" — a piece
Output

Print the minimum number of moves required for the Half-Queen to reach the enemy King. If it is impossible, print -1.

Examples
Input
4
.K..
....
..Q.
....
Output
3
Input
4
.K#.
#...
..Q.
....
Output
-1