시간 제한메모리 제한제출정답맞힌 사람정답 비율
5 초 512 MB239939.130%

## 문제

Sokoban is a famous Japanese puzzle game. Sokoban is Japanese for "warehouse keeper". In this game, your goal is to push boxes to their designated locations in the warehouse. To push a box, the area behind the box and in front of the box must be empty. This is because you stand behind the box when pushing and you can push only one box at a time. You cannot push a box out of the board and you cannot stand outside the board when pushing a box.

For example, in this picture:

Box 1 can be pushed in any of the four directions because the four spaces adjacent to it are empty. Box 2 can only be pushed east or west; it cannot be pushed north or south because the space to its south is not empty. Box 3 cannot be pushed in any direction. Box 4 can only be pushed east or west because there is a wall south of it.

Sokoban was proved to be a P-Space complete problem, but we deal with an easier variation here. In our variation of Sokoban, boxes have strong magnets inside and they have to stick together almost all the time. Under "stable" conditions, all boxes should be connected, edge to edge. This means that from any box we can get to any other box by going through boxes that share an edge. If you push a box and boxes are no longer connected, you are in "dangerous mode". In dangerous mode, the next push must make the boxes connected again.

For example, in this picture:

The situation is stable, since all 4 boxes are connected, edge to edge. Let's assume that you decided to push the northmost box west:

Now, we are in dangerous mode since the northmost box is not connected to any other boxes. The next push must return us to a stable position. For example, we can push that northmost box south:

Making the boxes stable again.

A Sokoban puzzle consists of a board, initial configuration of the boxes and the final configuration (where we want the boxes to be at the end). Given an EZ-Sokoban puzzle, find a solution that makes the minimum number of box moves, or decide that it can't be solved. The final and initial configurations will not be in "dangerous" mode.

To simplify things, we will assume that you, the warehouse keeper, can jump at any time to any empty spot on the board.

## 입력

The first line in the input file contains the number of cases, T

Each case consists of several lines. The first line contains R and C, the number of rows and columns of the board, separated by one space. This is followed by R lines. Each line contains C characters describing the board:

• '.' is an empty spot
• '#' is a wall
• 'x' is a goal (where a box should be at the end)
• 'o' is a box
• 'w' is a both a box and a goal

The number of boxes will be equal to the number of goals.

Limits

• 1 ≤ T ≤ 50
• 1 ≤ R,C ≤ 12
• 1 ≤ the number of boxes ≤ 5

## 출력

For each test case, output

Case #X: K

where X is the test case number, starting from 1, and K is the minimum number of box moves that are needed to solve the puzzle or -1 if it cannot be solved.

## 예제 입력 1

6
5 4
....
#..#
#xx#
#oo#
#..#
7 7
.######
.x....#
.x....#
..#oo.#
..#...#
.######
.######
7 7
#######
#x....#
#xx.o.#
#..oo.#
#.#...#
#######
#######
4 10
##########
#.x...o..#
#.x...o..#
##########
7 7
#######
#x....#
#x..o.#
#x#oo.#
#.#...#
#######
#######
3 4
.#x.
.oww
....


## 예제 출력 1

Case #1: 2
Case #2: 8
Case #3: 12
Case #4: 8
Case #5: -1
Case #6: 2


## 채점 및 기타 정보

• 예제는 채점하지 않는다.