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>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
#define double long double

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define EPS 1e-8

typedef vector<double> vec;
typedef vector<vec> mat;

vec gauss_jordan(const mat& A,const vec& b){
  int n=A.size();
  mat B(n,vec(n+1));
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      B[i][j]=A[i][j];
    }
    B[i][n]=b[i];
  }

  for (int k = 0; k < n-1; k++) {
    for (int i = k + 1; i < n; i++) {
      double d = B[i][k] / B[k][k];
      for (int j = k + 1; j <= n; j++){
        B[i][j] -= B[k][j] * d;
      }
    }
  }

  for (int i = n - 1; i >= 0; i--) {
    double d = B[i][n];
    for (int j = i + 1; j < n; j++){
      d -= B[i][j] * B[j][n];
    }
    B[i][n] = d / B[i][i];
  }

  vec x(n);
  for(int i=0;i<n;i++)x[i]=B[i][n];
  return x;
}

int H,W,N;
vector<vector<int> > ps;
vector<int> crt;
vector<string> sb,tb;
double p;

vector<int> calc(const vector<string>& a){
  vector<int> res;
  int mn=H;
  for(int j=W-1;j>=0;j--){
    int tmp=0;
    for(int i=H-1;i>=0;i--){
      if(tb[i][j]!=a[i][j]){
        break;
      }
      tmp++;
    }
    minch(mn,tmp);
    res.push_back(mn);
  }
  reverse(all(res));
  return res;
}

void predfs(int i,int j){
  if(i==W){
    ps.push_back(crt);
    return ;
  }
  repl(k,j,H+1){
    crt.push_back(k);
    predfs(i+1,k);
    crt.pop_back();
  }
}
void dfs(int i0,int j0,mat& A,vec &b){
  if(i0==W){
    vector<string> crtb;
    crtb.resize(H);
    rep(i,H)crtb[i]=tb[i];
    rep(j,W){
      rep(i,H-crt[j])crtb[i][j]='.';
    }
    int crtidx=lower_bound(all(ps),crt)-ps.begin();
    rep(i,H)rep(j,W)rep(c,2){
      vector<string> tmp(H);
      rep(ni,H)tmp[ni]=crtb[ni];
      rep(ni,i+1)rep(nj,j+1){
        if(c==0) tmp[ni][nj]='W';
        else tmp[ni][nj]='B';
      }
      int nxtidx=lower_bound(all(ps),calc(tmp))-ps.begin();
      A[crtidx][nxtidx]-=p; b[crtidx]+=p*(double)(i+1)*(double)(j+1);
    }
    return ;
  }
  repl(k,j0,H+1){
    crt.push_back(k);
    dfs(i0+1,k,A,b);
    crt.pop_back();
  }
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  while(1){
    cin>>H>>W;
    if(H==0)break;

    ps.clear(); p=1.0/(double)(H*W*2.0);
    sb.resize(H); tb.resize(H);

    rep(i,H)cin>>sb[i];
    rep(i,H)cin>>tb[i];

    predfs(0,0);
    N=ps.size();
    mat A(N,vec(N,0)); vec b(N,0.0);
    dfs(0,0,A,b);

    int startidx=lower_bound(all(ps),calc(sb))-ps.begin();
    int endidx=lower_bound(all(ps),calc(tb))-ps.begin();
    rep(i,N)A[i][i]+=1.0;
    rep(j,N)A[endidx][j]=0.0;
    A[endidx][endidx]=1.0; b[endidx]=0.0;
    vec res=gauss_jordan(A,b);
    printf("%.10Lf\n", res[startidx]);
  }

  return 0;
}