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
#include <bits/stdc++.h>

using namespace std;

int n, m, sum, tar, k;

uint64_t read() {
	uint64_t s = 0;
	for (int i = 0; i < n; ++i) {
		string str;
		cin >> str;
		for (int j = 0; j < m; ++j) {
			int k = i * m + j;
			if (str[j] == 'O')
				s |= 1ull << k;
		}
	}
	return s;
}

int parity(uint64_t s) {
	int ans = 0;
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j) {
			int k = i * m + j;
			if (i + j & 1) continue;
			if (s >> k & 1)
				ans ^= 1;
		}
	return ans;
}

int cnt(uint64_t s) {
	int ans = 0;
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j) {
			if (i + 1 < n) {
				int k = i * m + j, t = (i + 1) * m + j;
				if ((s >> k & 1) != (s >> t & 1))
					++ans;
			}
			if (j + 1 < m) {	
				int k = i * m + j, t = i * m + j + 1;
				if ((s >> k & 1) != (s >> t & 1))
					++ans;
			 }
		}
	return ans; 
}

void dfs(uint64_t cur, int rem, int bit) {
	if (bit + rem > n * m) {
		return;
	}
	if (bit == n * m && parity(cur) == tar) {
		//assert(rem == 0);
		sum += cnt(cur);
		return;
	}
	dfs(cur, rem, bit + 1);
	if (rem > 0) dfs(cur | (1ull << bit), rem - 1, bit + 1);
}

int64_t C[105][105];

void init(int n) {
	for (int i = 0; i <= n; ++i) {
		C[i][0] = 1;
		for (int j = 1; j <= i; ++j) {
			C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
		}
	}
}

int64_t cntsum() {
	int ls = 0, rs = 0, cnt = 0;
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j) {
			if (i + j & 1) {
				++ls;
			}
			else {
				++rs;
			}
			if (i + 1 < n) ++cnt;
			if (j + 1 < m) ++cnt;
		}
	int64_t ans = 0;
	for (int o : {0, 1}) {
		for (int i = 0; i <= k - 1 && i < ls; ++i) {
			if ((i + o) % 2 == tar) {
				int j = k - 1 - i;
				int64_t ways = C[ls - 1][i] * C[rs - 1][j];
				ans += ways;
			}
		}
	}
	return ans * cnt;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cout << fixed << setprecision(15);
	cin >> n >> m;
	init(n * m);
	uint64_t s = read(), t = read();
	if (__builtin_popcountll(s) != __builtin_popcountll(t) || parity(s) != parity(t)) {
		cout << 0 << '\n';
		return 0;
	}
	tar = parity(s);
	k = __builtin_popcountll(s);
	int64_t sum = cntsum();
	cout << 1.0L * cnt(t) / sum << '\n';
}