시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
1 초 128 MB 6 2 1 25.000%

문제

After the untimely demise of its previous tenant, you have found yourself promoted to the position of the new “chief architect” of the Death Star by none other than Lord Vader himself. Your have been tasked with designing the wiring system for distributing power throughout each sector of the Death Star. Knowing that your life depends on your success, you have decided to be extremely meticulous and exhaustively enumerate all valid wiring configurations so that you may choose the best one.

You are given a map of the Death Star, in the form of an m × n grid, whose cells correspond to sectors. Exactly one of the sectors in the map is a power station; the remaining sectors are all either living quarters or storage units. You have the option of placing a connection between any two non-storage sectors that are either vertically or horizontally adjacent. A valid wiring configuration is one such that:

  • Every living quarter sector is either directly or indirectly connected to the power station sector, and
  • As few connections are used as possible (i.e., removing any connection would result in disconnecting some living quarter from the power station).

For a given map, how many valid wiring configurations exist?

입력

The input will contain multiple test cases. Each test case begins with a single line containing a pair of integers m and n (where 1 ≤ m ≤ 8 and 1 ≤ n ≤ 8). The next m lines each contain n characters, representing the map of the Death Star. The map will contain exactly one sector marked as a power station (P); the remainder of the sections will be either living quarters (.) or storage units (#). You are guaranteed that there exists at least one valid wiring configuration. The end-of-input is denoted by a single line containing “0 0” and should not be processed.

출력

For each input test case, print the number of valid wiring configurations mod 1,000,000,000.

예제 입력

2 2
..
.P
4 5
#####
#.P.#
#...#
#####
3 4
....
P#..
...#
6 6
######
##..##
#....#
#..P.#
##..##
######
0 0

예제 출력

4
15
31
768

힌트