1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*************************************************************************************************************
										⣿⣿⣿⣿⣿⣿⣿⣿⡿⠿⠛⠛⠛⠋⠉⠈⠉⠉⠉⠉⠛⠻⢿⣿⣿⣿⣿⣿⣿⣿
										⣿⣿⣿⣿⣿⡿⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⢿⣿⣿⣿⣿
										⣿⣿⣿⣿⡏⣀⠀⠀⠀⠀⠀⠀⠀⣀⣤⣤⣤⣄⡀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣿⣿
										⣿⣿⣿⢏⣴⣿⣷⠀⠀⠀⠀⠀⢾⣿⣿⣿⣿⣿⣿⡆⠀⠀⠀⠀⠀⠀⠀⠈⣿⣿
										⣿⣿⣟⣾⣿⡟⠁⠀⠀⠀⠀⠀⢀⣾⣿⣿⣿⣿⣿⣷⢢⠀⠀⠀⠀⠀⠀⠀⢸⣿
										⣿⣿⣿⣿⣟⠀⡴⠄⠀⠀⠀⠀⠀⠀⠙⠻⣿⣿⣿⣿⣷⣄⠀⠀⠀⠀⠀⠀⠀⣿
										⣿⣿⣿⠟⠻⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠶⢴⣿⣿⣿⣿⣿⣧⠀⠀⠀⠀⠀⠀⣿
										⣿⣁⡀⠀⠀⢰⢠⣦⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣿⡄⠀⣴⣶⣿⡄⣿
										⣿⡋⠀⠀⠀⠎⢸⣿⡆⠀⠀⠀⠀⠀⠀⣴⣿⣿⣿⣿⣿⣿⣿⠗⢘⣿⣟⠛⠿⣼
										⣿⣿⠋⢀⡌⢰⣿⡿⢿⡀⠀⠀⠀⠀⠀⠙⠿⣿⣿⣿⣿⣿⡇⠀⢸⣿⣿⣧⢀⣼
										⣿⣿⣷⢻⠄⠘⠛⠋⠛⠃⠀⠀⠀⠀⠀⢿⣧⠈⠉⠙⠛⠋⠀⠀⠀⣿⣿⣿⣿⣿
										⣿⣿⣧⠀⠈⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠟⠀⠀⠀⠀⢀⢃⠀⠀⢸⣿⣿⣿⣿
										⣿⣿⡿⠀⠴⢗⣠⣤⣴⡶⠶⠖⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⡸⠀⣿⣿⣿⣿
										⣿⣿⣿⡀⢠⣾⣿⠏⠀⠠⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠛⠉⠀⣿⣿⣿⣿
										⣿⣿⣿⣧⠈⢹⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⣿⣿⣿⣿
										⣿⣿⣿⣿⡄⠈⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣴⣾⣿⣿⣿⣿⣿
										⣿⣿⣿⣿⣧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿
										⣿⣿⣿⣿⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
										⣿⣿⣿⣿⣿⣦⣄⣀⣀⣀⣀⠀⠀⠀⠀⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
										⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡄⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
										⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠀⠀⠀⠙⣿⣿⡟⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿
										⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⠀⠁⠀⠀⠹⣿⠃⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿
										⣿⣿⣿⣿⣿⣿⣿⣿⡿⠛⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⢐⣿⣿⣿⣿⣿⣿⣿⣿⣿
										⣿⣿⣿⣿⠿⠛⠉⠉⠁⠀⢻⣿⡇⠀⠀⠀⠀⠀⠀⢀⠈⣿⣿⡿⠉⠛⠛⠛⠉⠉
										⣿⡿⠋⠁⠀⠀⢀⣀⣠⡴⣸⣿⣇⡄⠀⠀⠀⠀⢀⡿⠄⠙⠛⠀⣀⣠⣤⣤⠄
*************************************************************************************************************/
#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <tuple>
#include <random>
#include <vector>
#include <bitset>
#include <chrono>
#include <iomanip>
#include <cassert>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
using namespace std;
#define UwU cin.tie(0);ios_base::sync_with_stdio(0);
#define test int test_num;cin>>test_num;while(test_num--)
#define prec(k) cout<<fixed<<setprecision(k);
#define cl(n,k) ((n)+(k)-1)/(k)
#define f first
#define s second
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
ll gcd(ll a, ll b){return (b==0?a:gcd(b,a%b));}
ll lcm(ll a, ll b){return a/gcd(a,b)*b;}
const int M = 1e9 + 7;
const int O = 998244353;
const ld EPS = 1e-10;
const ld PI = 3.14159265358979323846;
const ll INF = (ll)1e18 + 5;

const int N = 2e5 + 5;
const int K = 10;
const int dy[4] = {0, 1, 0, -1};
const int dx[4] = {1, 0, -1, 0};

int n, m;
char w[K][K];
char w2[K][K];
int chk;
ll dp[K][K][K] = {{{0,0,0,0,0,0,0,0},{2,0,0,0,0,0,0,0},{4,4,0,0,0,0,0,0},{6,12,6,0,0,0,0,0},{8,24,24,8,0,0,0,0},{10,40,60,40,10,0,0,0},{12,60,120,120,60,12,0,0},{14,84,210,280,210,84,14,0}},{{2,0,0,0,0,0,0,0},{8,16,8,0,0,0,0,0},{14,56,84,56,14,0,0,0},{20,120,300,400,300,120,20,0},{26,208,728,1456,1820,1456,728,208},{32,320,1440,3840,6720,8064,6720,3840},{38,456,2508,8360,18810,30096,35112,30096},{44,616,4004,16016,44044,88088,132132,151008}},{{4,4,0,0,0,0,0,0},{14,56,84,56,14,0,0,0},{24,168,504,840,840,504,168,24},{34,340,1530,4080,7140,8568,7140,4080},{44,572,3432,12584,31460,56628,75504,75504},{54,864,6480,30240,98280,235872,432432,617760},{64,1216,10944,62016,248064,744192,1736448,3224832},{74,1628,17094,113960,541310,1948716,5521362,12620256}},{{6,12,6,0,0,0,0,0},{20,120,300,400,300,120,20,0},{34,340,1530,4080,7140,8568,7140,4080},{48,672,4368,17472,48048,96096,144144,164736},{62,1116,9486,50592,189720,531216,1150968,1973088},{76,1672,17556,117040,555940,2001384,5670588,12961344},{90,2340,29250,234000,1345500,5920200,20720700,59202000},{104,3120,45240,422240,2850120,14820624,61752600,211723200}},{{8,24,24,8,0,0,0,0},{26,208,728,1456,1820,1456,728,208},{44,572,3432,12584,31460,56628,75504,75504},{62,1116,9486,50592,189720,531216,1150968,1973088},{80,1840,20240,141680,708400,2691920,8075760,19612560},{98,2744,37044,321048,2006550,9631440,36920520,116035920},{116,3828,61248,632896,4746720,27530976,128477888,495557568},{134,5092,94202,1130424,9891210,67260228,369931254,1691114304}},{{10,40,60,40,10,0,0,0},{32,320,1440,3840,6720,8064,6720,3840},{54,864,6480,30240,98280,235872,432432,617760},{76,1672,17556,117040,555940,2001384,5670588,12961344},{98,2744,37044,321048,2006550,9631440,36920520,116035920},{120,4080,67320,718080,5565120,33390720,161388480,645553920},{142,5680,110760,1402960,12977380,93437136,545049960,2647385520},{164,7544,169740,2489520,26762340,224803656,1536158316,8778047520}},{{12,60,120,120,60,12,0,0},{38,456,2508,8360,18810,30096,35112,30096},{64,1216,10944,62016,248064,744192,1736448,3224832},{90,2340,29250,234000,1345500,5920200,20720700,59202000},{116,3828,61248,632896,4746720,27530976,128477888,495557568},{142,5680,110760,1402960,12977380,93437136,545049960,2647385520},{168,7896,181608,2724120,29965320,257701752,1803912264,10565771832},{194,10476,277614,4811976,61352694,613526940,5010470010,34357508640}},{{14,84,210,280,210,84,14,0},{44,616,4004,16016,44044,88088,132132,151008},{74,1628,17094,113960,541310,1948716,5521362,12620256},{104,3120,45240,422240,2850120,14820624,61752600,211723200},{134,5092,94202,1130424,9891210,67260228,369931254,1691114304},{164,7544,169740,2489520,26762340,224803656,1536158316,8778047520},{194,10476,277614,4811976,61352694,613526940,5010470010,34357508640},{224,13888,423584,8471680,124957280,1449504448,13770292256,110162338048}}};

bool cango(int y, int x){
	if(y < 0 || y >= n) return false;
	if(x < 0 || x >= m) return false;
	return w2[y][x] != 'O';
}

int main(){
	UwU

	prec(16);

	cin >> n >> m;

	int pawns1 = 0;

	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin >> w[i][j];
			if(w[i][j] == 'O'){
				chk ^= i;
				chk ^= j;
				pawns1++;
			}
		}
	}

	int pawns = 0;

	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin >> w2[i][j];
			if(w2[i][j] == 'O'){
				chk ^= i;
				chk ^= j;
				pawns++;
			}
		}
	}

	if((chk&1) || (pawns1 != pawns)){
		cout << "0\n";
		return 0;
	}

	ll cnt = 0;

	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(w2[i][j] == 'O'){
				for(int k=0;k<4;k++){
					if(cango(i+dy[k],j+dx[k])){
						cnt++;
					}
				}
			}
		}
	}

	//cout << dp[n-1][m-1][pawns-1] << "\n";

	cout << (ld)cnt/(dp[n-1][m-1][pawns-1]/2);

	return 0;
}