Under construction

Still being built. Things may change

Hungry Gargamel

Time limit1sMemory limit2048 MB

Summary
Find the expected number of steps from the top-left to the bottom-right cell in a grid where stepping on a white cell teleports to a random white cell.
Level

Medium6 of 10

Topics
BFS, Math
Solved
No attempts yet

Problem

The wizard Gargamel is hungry and hopes to find food inside a grid with NN rows and MM columns, where each cell is black or white.

Gargamel starts in the top left corner and wants to reach the bottom right corner, where the cevapi are. The top left and bottom right cells are always black.

At each step, Gargamel must move to an adjacent cell: up, down, left, or right. If the cell he moves to is white, the following happens. One of all the white cells in the grid is chosen uniformly at random, and Gargamel teleports to it. This may be the cell he is currently on.

Then the process continues with the next step. So in each step, Gargamel first moves to an adjacent cell, and then at most one teleportation happens (if that cell is white).

Gargamel's search ends as soon as he reaches the bottom right corner.

Assuming Gargamel moves optimally, find the expected number of steps he takes before reaching the bottom right corner.

Input

The first line contains a natural number TT (1≤T≤10001 ≤ T ≤ 1000), the number of scenarios.

Each scenario starts with a line containing natural numbers NN and MM (1≤N,M≤10001 ≤ N, M ≤ 1000). The next NN lines each contain MM characters, either 'C' for a black cell or 'B' for a white cell, describing one row of the grid.

The total number of cells across all scenarios does not exceed 10610^6.

Output

For each scenario, print the expected value as a fraction in lowest terms.

Hint

In the first scenario all cells are black, so the best move is to head straight to the bottom right cell. The second scenario has two white cells, but wherever Gargamel lands after teleporting, he can reach the bottom right cell in the next step.

Examples3

  1. Example 1

    Input
    3
    2 2
    CC
    CC
    2 2
    CB
    BC
    1 12
    CBCCCCCCCCBC
    
    Expected output
    2/1
    2/1
    4/1
    
  2. Example 2

    Input
    1
    6 2
    CC
    CC
    CC
    CC
    CC
    CC
    
    Expected output
    6/1
    
  3. Example 3

    Input
    1
    6 3
    CBC
    CBC
    CBC
    CBC
    CBC
    CBC
    
    Expected output
    4/1