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 JAVA):

Solution:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	void run() {
		Scanner sc = new Scanner(System.in);
		while (true) {
			int H = sc.nextInt();
			int W = sc.nextInt();
			if (H == 0 && W == 0)
				break;
			char[][] initial_map = new char[H][W];
			char[][] desired_map = new char[H][W];
			for (int i = 0; i < H; ++i) {
				initial_map[i] = sc.next().toCharArray();
			}
			for (int i = 0; i < H; ++i) {
				desired_map[i] = sc.next().toCharArray();
			}

			solve(H, W, initial_map, desired_map);
		}
	}

	void solve(int H, int W, char[][] initial_map, char[][] desired_map) {
		int cnt = 1;
		for (int i = 1; i <= H + W; ++i) {
			cnt *= i;
		}
		for (int i = 1; i <= H; ++i) {
			cnt /= i;
		}
		for (int i = 1; i <= W; ++i) {
			cnt /= i;
		}
		// the number of the states is cnt

		ArrayList<ArrayList<Integer>> lis = dfs(H, W, 0, H);
		lis = reverse(lis);

		int idx = 0;
		HashMap<List<Integer>, Integer> numbering = new HashMap<>();

		for (ArrayList<Integer> a : lis) {
			numbering.put(a, idx++);
		}

		// numbering is done.

		char[] col = { 'B', 'W' };
		double[][] mx = new double[cnt][cnt];
		double[][] v = new double[cnt][1];
		for (int i = 0; i < cnt; ++i) {
			v[i][0] = -1d / 4 * (W + 1) * (H + 1);
		}
		for (int i = 0; i < cnt; ++i) {
			mx[i][i] = -1;
		}
		Arrays.fill(mx[cnt - 1], 0);
		v[cnt - 1][0] = 0;
		for (Entry<List<Integer>, Integer> entry : numbering.entrySet()) {
			List<Integer> arr = entry.getKey();
			int curIdx = entry.getValue();
			if (curIdx == idx - 1)
				continue;
			boolean[][] bMap = to_map(arr, H, W);
			for (char c : col) {
				for (int i = 0; i < H; ++i) {
					for (int j = 0; j < W; ++j) {
						ArrayList<Integer> tmp = paint(bMap, H, W, i, j, c, desired_map);
						int nidx = numbering.get(tmp);
						mx[curIdx][nidx] += 1d / (2 * H * W);
					}
				}
			}
		}

		int id = numbering.get(to_arr(initial_map, H, W, desired_map));
		double ans = MtPrd(Rev(mx), v)[id][0];
		System.out.printf("%.20f\n", ans);
	}

	ArrayList<ArrayList<Integer>> dfs(int H, int W, int curIdx, int curHeight) {
		ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
		if (curIdx == W) {
			ret.add(new ArrayList<>());
			return ret;
		}
		for (int i = curHeight; i >= 0; --i) {
			ArrayList<ArrayList<Integer>> tmp = dfs(H, W, curIdx + 1, i);
			for (ArrayList<Integer> lis : tmp) {
				lis.add(i);
				ret.add(lis);
			}
		}
		return ret;
	}

	// the color is incorrect:false
	// the color is correct:true
	boolean[][] to_map(List<Integer> lis, int H, int W) {
		boolean[][] map = new boolean[H][W];

		for (int i = 0; i < H; ++i) {
			for (int j = 0; j < W; ++j) {
				map[i][j] = true;
			}
		}

		for (int i = 0; i < W; ++i) {
			for (int j = lis.get(i) - 1; j >= 0; --j) {
				map[j][i] = false;
			}
		}
		return map;

	}

	ArrayList<Integer> to_arr(char[][] src, int H, int W, char[][] dst) {
		ArrayList<Integer> lis = new ArrayList<>();
		for (int i = 0; i < W; ++i) {
			lis.add(0);
		}

		for (int k = H + W - 2; k >= 0; --k) {
			for (int i = 0; i < H; ++i) {
				int j = k - i;
				if (j < 0 || j >= W)
					continue;
				if (dst[i][j] == src[i][j])
					continue;
				lis = paint(to_map(lis, H, W), H, W, i, j, src[i][j], dst);
			}
		}
		return lis;
	}

	ArrayList<Integer> paint(boolean[][] bMap, int H, int W, int h, int w, char c, char[][] dst) {
		boolean[][] map = new boolean[H][W];
		for (int i = 0; i < H; ++i) {
			for (int j = 0; j < W; ++j) {
				map[i][j] = bMap[i][j];
			}
		}
		for (int i = 0; i < H; ++i) {
			for (int j = 0; j < W; ++j) {
				if (i <= h && j <= w) {
					map[i][j] = (dst[i][j] == c);
				}
			}
		}

		for (int k = H + W - 2; k >= 0; --k) {
			for (int i = 0; i < H; ++i) {
				int j = k - i;
				if (j < 0 || j >= W)
					continue;
				if (!map[i][j]) {
					for (int ii = 0; ii <= i; ++ii) {
						for (int jj = 0; jj <= j; ++jj) {
							map[ii][jj] = false;
						}
					}
				}
			}
		}

		ArrayList<Integer> ret = new ArrayList<>();
		for (int i = 0; i < W; ++i) {
			int cnt = 0;
			for (int j = 0; j < H; ++j) {
				if (!map[j][i])
					++cnt;
			}
			ret.add(cnt);
		}
		return ret;
	}

	ArrayList<ArrayList<Integer>> reverse(ArrayList<ArrayList<Integer>> lis) {
		ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
		for (ArrayList<Integer> a : lis) {
			ArrayList<Integer> tmp = new ArrayList<>();
			for (int i = a.size() - 1; i >= 0; --i) {
				tmp.add(a.get(i));
			}
			ret.add(tmp);
		}
		return ret;

	}

	public static double[][] Rev(double[][] OM) {
		int n = OM.length, m = OM[0].length;
		if (n != m)
			return null;

		double[][] M = new double[n][2 * n];
		m = 2 * n;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				M[i][j] = OM[i][j];
			}
			M[i][n + i] = 1;
		}

		double[][] res = operateElementarily(M);
		if (res == null)
			return null;

		// resotration
		double[][] ret = new double[n][n];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				ret[i][j] = res[i][j + n];
			}
		}

		return ret;
	}

	public static double[][] operateElementarily(double[][] M) {
		int n = M.length, m = M[0].length;
		int rank = n - 1;

		// Forward Elimination
		for (int i = 0; i < n; i++) {
			// select pivot
			double max = 1E-9;
			int maxj = -1;
			for (int j = i; j < n; j++) {
				double v = Math.abs(M[j][i]);
				if (v > max) {
					max = v;
					maxj = j;
				}
			}
			if (maxj == -1) {
				rank = i - 1;
				break;
			}
			if (maxj != i) {
				double[] dum = M[i];
				M[i] = M[maxj];
				M[maxj] = dum;
			}

			double D = M[i][i];
			M[i][i] = 1;
			for (int j = i + 1; j < m; j++) {
				M[i][j] /= D;
			}

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

		// Back Substitution
		for (int i = rank; i >= 0; i--) {
			for (int j = rank; j >= i + 1; j--) {
				double B = -M[i][j];
				M[i][j] = 0;
				for (int k = rank + 1; k < m; k++) {
					M[i][k] += B * M[j][k];
				}
			}
		}

		return M;
	}

	double[][] MtPrd(double[][] A, double[][] B) {
		double[][] C = new double[A.length][B[0].length];
		for (int i = 0; i < A.length; i++) {
			for (int j = 0; j < B[0].length; j++) {
				for (int k = 0; k < A[0].length; k++) {
					C[i][j] += A[i][k] * B[k][j];
				}
			}
		}
		return C;
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}