Power Grid#6051

Power Grid

3 seconds
256 megabytes
Very hard

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. You 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 \times 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:

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

Input

The input will contain multiple test cases. Each test case begins with a single line containing a pair of integers and (where 1 \le m \le 8 and 1 \le n \le 8). The next lines each contain 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.

Output

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

Examples

Example 1

Loading
Loading