This page is still under construction.

Parts of this page are still being built. What you see may change.

Surrounded

Time limit1sMemory limit1024 MB

Summary
Mirko moves his piece on a grid to wall in Slavko's hidden piece before it reaches the border, and the task asks for the fewest wall cells needed.
Level

Hard8 of 10

Topics
BFS, Graph, Implementation
Solved
No attempts yet

Problem

Mirko and Slavko play a new game on an N×MN \times M board. The board has two pieces: Mirko's piece and Slavko's hidden piece. Mirko's piece starts in the upper left corner. Mirko does not know where Slavko's piece starts, but he knows every possible starting position.

Mirko moves first. On his turn, he can make up to 10 steps. One step moves a piece to an adjacent cell. Two cells are adjacent if they share a side. After Mirko, Slavko moves his piece one step or leaves it where it is. Slavko's piece is hidden, so Mirko cannot see where it is or which moves Slavko makes during the game. The two pieces may occupy the same cell. Mirko may step onto a cell he has already visited only in the last step of the whole game. Slavko may visit any cell as many times as he wants. The players take turns until one of them wins.

Slavko wins by moving his piece to the first or last row, or to the first or last column, of the board. Mirko wins by using his piece to enclose the area around Slavko's piece. When Mirko steps onto a cell he already visited in the last step of the game, a wall forms on every cell he occupied between the first and second visits to that cell, including that cell itself. Slavko's piece is enclosed if it lies strictly inside Mirko's wall.

Input

The first line contains two natural numbers NN and MM (1≤N≤2001 \le N \le 200, 1≤M≤2001 \le M \le 200), the number of rows and columns of the board.

Each of the next NN lines contains MM characters that describe the board. A dot ('.') is an empty cell. A lowercase 'x' marks a possible starting position of Slavko's piece.

Output

Print the smallest number of cells on which walls must be built so that Mirko is sure to enclose Slavko's piece, regardless of Slavko's starting position and moves. If Mirko cannot guarantee this, print -1.

Hint

Explanation of the first sample: On his first turn, Mirko visits the cells in the order shown in the figure:

01...
.234.
.9x5.
.876.
.....

At the 10th step of his first turn, Mirko steps back onto the cell marked 2. When he steps onto the cell marked 2 again, walls form on 8 cells (the cells marked 2 to 9).

Explanation of the second sample: Mirko cannot enclose Slavko's piece. If Slavko places his piece on the starting position in the second-to-last row, then after Mirko's 10 steps, Slavko moves his piece to the last row and wins.

Explanation of the third sample: One way for Mirko to enclose Slavko's piece with 30 walls is to move in the order shown:

 0  1  2  3  4  .  .  .
29  .  .  .  5  6  7  .
28  .  .  .  .  .  8  9
27  .  .  x  .  .  . 10
26  .  .  .  x  .  . 11
25  .  .  x  .  .  . 12
24  .  .  .  .  .  . 13
23 22  .  .  .  .  . 14
 . 21 20 19 18 17 16 15

Mirko reaches cell 0 on his 30th step. Walking those 30 cells takes Mirko 3 turns, so Slavko gets 2 moves before the wall is finished. Slavko is certainly inside the enclosed area within those 2 moves.

Examples3

  1. Example 1

    Input
    5 5
    .....
    .....
    ..x..
    .....
    .....
    
    Expected output
    8
    
  2. Example 2

    Input
    8 8
    ........
    ........
    ...x....
    ........
    ..x.....
    ........
    ......x.
    ........
    
    Expected output
    -1
    
  3. Example 3

    Input
    9 8
    ........
    ........
    ........
    ...x....
    ....x...
    ...x....
    ........
    ........
    ........
    
    Expected output
    30