Problem: Problem Statement

You have a rectangular board with square cells arranged in $H$ rows and $W$ columns. The rows are numbered $1$ through $H$ from top to bottom, and the columns are numbered $1$ through $W$ from left to right. The cell at the row $i$ and the column $j$ is denoted by $(i, j)$. Each cell on the board is colored in either Black or White.

You will paint the board as follows:

1. Choose a cell $(i, j)$ and a color $c$, each uniformly at random, where $1 \le i \le H$, $1 \le j \le W$, and $c \in \\{{\rm Black}, {\rm White}\\}$.

2. Paint the cells $(i', j')$ with the color $c$ for any $1 \le i' \le i$ and $1 \le j' \le j$.




Here's an example of the painting operation. You have a $3 \times 4$ board with the coloring depicted in the left side of the figure below. If your random choice is the cell $(2, 3)$ and the color Black, the board will become as shown in the right side of the figure. $6$ cells will be painted with Black as the result of this operation. Note that we count the cells "painted" even if the color is not actually changed by the operation, like the cell $(1, 2)$ in this example.

<image>
Fig: An example of the painting operation


Given the initial coloring of the board and the desired coloring, you are supposed to perform the painting operations repeatedly until the board turns into the desired coloring. Write a program to calculate the expected total number of painted cells in the sequence of operations.

Input

The input consists of several datasets. The number of datasets is at most $100$.

The first line of each dataset contains two integers $H$ and $W$ ($1 \le H, W \le 5$), the numbers of rows and columns of the board respectively. Then given are two coloring configurations of the board, where the former is the initial coloring and the latter is the desired coloring. A coloring configuration is described in $H$ lines, each of which consists of $W$ characters. Each character is either B or W, denoting a Black cell or a White cell, respectively. There is one blank line between two configurations, and also after each dataset. You can assume that the resulting expected value for each dataset will not exceed $10^9$.

The input is terminated by a line with two zeros, and your program should not process this as a dataset.

Output

For each dataset, your program should output the expected value in a line. The absolute error or the relative error in your answer must be less than $10^{-6}$.

Sample Input


1 2
BB

WW

2 1
B
W

B
W

2 2
BW
BW

WW
WW

3 4
BBBB
BBBB
BBBB

WWWW
WWWW
WWWW

5 5
BBBBB
BBBBB
BBBBB
BBBBB
BBBBB

BBBBB
BBBWB
BBBBB
BWBBB
BBBBB

0 0

Output for the Sample Input


6.0000000000
0.0000000000
12.8571428571
120.0000000000
23795493.8449918639





Example

Input

1 2
BB

WW

2 1
B
W

B
W

2 2
BW
BW

WW
WW

3 4
BBBB
BBBB
BBBB

WWWW
WWWW
WWWW

5 5
BBBBB
BBBBB
BBBBB
BBBBB
BBBBB

BBBBB
BBBWB
BBBBB
BWBBB
BBBBB

0 0


Output

6.0000000000
0.0000000000
12.8571428571
120.0000000000
23795493.8449918639

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<(int)(n); ++i)

using namespace std;
int H, W;
void input(int grid[5][5]){
    REP(y, H) {
        string s;
        cin >> s;
        REP(x, W) {
            grid[y][x] = (s[x] == 'B');
        }
    }
}
const double EPS = 1e-8;
typedef vector<double> Vec;
typedef vector<Vec> Mat;

Vec gauss_jordan(const Mat& A, const Vec& b){
    int W = A[0].size();
    int H = A.size();

    Mat B(H, Vec(W + 1));
    for(int y = 0; y < H; y++)
        for(int x = 0; x < W; x++)
            B[y][x] = A[y][x];
    for(int y = 0; y < H; y++)
        B[y][W] = b[y];

    bool unique = true; // 解が一意かどうか
    int cy = 0; // 現在注目している式

    // 現在注目している変数
    for(int x = 0; x < W; x++){
        int pivot = cy;
        // 注目している変数の係数の絶対値が一番大きい式を選ぶ
        for(int y = cy; y < H; y++){
            if(abs(B[y][x]) > abs(B[pivot][x])) pivot = y;
        }

        // 解が一意でないか，解が存在しない
        if(pivot >= H || abs(B[pivot][x]) < EPS) {
            unique = false;
            continue;
        }

        swap(B[cy], B[pivot]);

        // 注目している変数の係数を1にする
        for(int x2 = x + 1; x2 <= W; x2++) {
            B[cy][x2] /= B[cy][x];
        }

        // y番目の式からx2番目の変数を消去
        for(int y = 0; y < H; y++) if(y != cy)
            for(int x2 = x + 1; x2 <= W; x2++)
                B[y][x2] -= B[y][x] * B[cy][x2];

        // 次の式に注目する
        cy++;
    }


    // 解が存在するかどうか
    for(int y = cy; y < H; y++)
        if(abs(B[y][W]) > EPS)
            return Vec();

    // 解が複数存在するかどうか
    if(!unique) return Vec();

    // 一意な解を返す
    Vec V(W);
    int cur_x = 0;
    for(int y = 0; y < H; y++){
        if(abs(B[y][cur_x]) > EPS){
            V[cur_x++] = B[y][W];
        }
    }
    return V;
}

vector<int> calc_v(int grid[5][5], int goal[5][5]){
    vector<int> v;
    REP(y, H) {
        int cx = W;
        while(cx - 1 >= 0 && grid[y][cx - 1] == goal[y][cx - 1]){
            cx--;
        }
        v.push_back(W - cx);
    }
    for(int y = H - 2; y >= 0; y--){
        v[y] = min(v[y], v[y + 1]);
    }
    return v;
}

void calc_grid(vector<int> v, int grid[5][5], int goal[5][5]){
    REP(y, H) REP(x, W) grid[y][x] = 2;
    REP(y, H) {
        REP(x, v[y]) {
            grid[y][W - 1 - x] = goal[y][W - 1 - x];
        }
    }
}

int main(){
    while(cin >> H >> W && (H > 0)){
        int start[5][5] = {};
        int goal[5][5] = {};
        input(start);
        input(goal);

        vector<int> goal_v = calc_v(goal, goal);
        vector<int> start_v = calc_v(start, goal);
        if(start_v == goal_v){
            cout << 0 << endl;
            continue;
        }

        vector<vector<int>> list;
        map<vector<int>, int> index;
        REP(S, 1 << (H + W)){
            if(__builtin_popcount(S) != H) continue;
            int cy = 0, cx = 0;
            vector<int> v;
            REP(i, H + W){
                if(S >> i & 1){
                    v.push_back(cx);
                    cy++;
                }else{
                    cx++;
                }
            }
            if(v == goal_v) continue;

            int idx = index.size();
            index[v] = idx;
            list.push_back(v);
            assert(list[idx] == v);
        }

        int N = list.size();
        Mat M(N, Vec(N));
        Vec B(N);
        REP(i, N) {
            M[i][i] = -1.0;
            double T = 1.0 * H * W * 2;
            REP(ry, H) REP(rx, W) REP(c, 2) {
                int grid[5][5];
                calc_grid(list[i], grid, goal);
                REP(y, ry + 1) REP(x, rx + 1) {
                    grid[y][x] = c;
                }
                vector<int> nv = calc_v(grid, goal);

                int cost = (ry + 1) * (rx + 1);
                B[i] -= cost / T;


                if(nv == goal_v){
                    continue;
                }

                int j = index[nv];
                M[i][j] += 1.0 / T;
            }
        }

        Vec C = gauss_jordan(M, B);
        printf("%.12f\n", C[ index[ start_v ] ]);
    }
    return 0;
}