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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// fajne zadanko, ale glupio ze da sie zgadnac wynik xD
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define e1 first
#define e2 second
#define pb push_back
#define OUT(x) {cout << x << "\n"; exit(0); }
#define TCOUT(x) {cout << x << "\n"; return; }
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define rep(i, l, r) for(int i = (l); i < (r); ++i)
#define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); }
#define sz(x) int(x.size())
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
mt19937_64 rng(time(0));
int random(int l, int r) {
	return uniform_int_distribution<int>(l, r)(rng);
}
#ifdef DEBUG
template<class T> int size(T &&x) {
	return int(x.size());
}
template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) {
	return out << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
	out << '{';
	for(auto it = x.begin(); it != x.end(); ++it)
		out << *it << (it == prev(x.end()) ? "" : ", ");
	return out << '}';
}
void dump() {}
template<class T, class... Args> void dump(T &&x, Args... args) {
	cerr << x << ";  ";
	dump(args...);
}
#endif
#ifdef DEBUG
  struct Nl{~Nl(){cerr << '\n';}};
# define debug(x...) cerr << (strcmp(#x, "") ? #x ":  " : ""), dump(x), Nl(), cerr << ""
#else
# define debug(...) 0 && cerr
#endif
const int maxn = 1000100;
int n, m;

inline bool in_range(int a, int b) {
	return a >= 0 && b >= 0 && a < n && b < m;
}
inline bool in_range(pii tmp) {
	return in_range(tmp.e1, tmp.e2);
}

vector <pii> neighbors = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};

pii operator+(pii a, pii b) {
	return {a.first + b.first, a.second + b.second};
}

bool degree(const vector<string> &vec) {
	int sum = 0;
	rep(i, 0, n) {
		rep(j, 0, m) {
			if (vec[i][j] == 'O') {
				sum += i;
				sum += j;
			}
		}
	}
	return sum % 2;
}
int find_pawns(const vector<string> &vec) {
	int sum = 0;
	rep(i, 0, n) {
		rep(j, 0, m) {
			if (vec[i][j] == 'O')sum++;
		}
	}
	return sum;
}

int possible_transitions(const vector<string> &vec) {
	int sum = 0;
	rep(i, 0, n) {
		rep(j, 0, m) {
			if (vec[i][j] == 'O') {
				pii tutaj = {i, j};
				for (auto delta : neighbors) {
					pii tam = tutaj + delta;
					if (in_range(tam) && vec[tam.e1][tam.e2] != 'O') ++sum;
				}
			}
		}
	}
	return sum;
}

const int MAX = 8;
const int BITMASK = (1 << 8);

ll ile[MAX][BITMASK][MAX + 1][2];
ll sum[MAX][BITMASK][MAX + 1][2];

inline bool jest(int bit, int mask) {
	return mask & (1 << bit);
}

int transitions_inside(int mask) {
	int ts = 0;
	rep(bit, 0, m) {
		if (jest(bit, mask)) {
			if (bit > 0 && !jest(bit - 1, mask)) ++ts;
			if (bit < m - 1 && !jest(bit + 1, mask)) ++ts;
		}
	}
	return ts;
}
int transitions_from(int maskA, int maskB) {
	int ts = 0;
	rep(bit, 0, m) {
		if (jest(bit, maskA) ^ jest(bit, maskB)) ++ts;
	}
	return ts;
}
inline int mask_parity(int row, int mask) {
	int parity = 0;
	rep(bit, 0, m) {
		if (jest(bit, mask)) {
			parity += row + bit;
		}
	}
	return parity % 2;
}

inline int bits(int mask) {
	return __builtin_popcount(mask);
}

ll sum_all_transitions(int pawns, bool degree) {
	debug(pawns, degree);
	// base transitions
	rep(mask, 0, (1 << m)) {
		int tr = transitions_inside(mask);
		int parity = mask_parity(0, mask);
		sum[0][mask][bits(mask)][parity] += tr;
		ile[0][mask][bits(mask)][parity] += 1;
	}
	
	// transitions from the previous mask
	rep(row, 1, n) {
		rep(mask, 0, (1 << m)) {
			int tr = transitions_inside(mask);
			int parity = mask_parity(row, mask);
			FOR(pawns_used, 0, pawns) {
				rep(prevmask, 0, (1 << m)) rep(prevparity, 0, 2) {
					int new_pawns = bits(mask) + pawns_used;
					ll ile_tego_stanu = ile[row - 1][prevmask][pawns_used][prevparity];
					if (new_pawns <= pawns) {
						ile[row][mask][new_pawns][prevparity ^ parity] += ile_tego_stanu;
						sum[row][mask][new_pawns][prevparity ^ parity] += (ile_tego_stanu * (tr + transitions_from(prevmask, mask))) + sum[row - 1][prevmask][pawns_used][prevparity];
					}
				}
			}
		}
	}
	
	ll result = 0;
	rep(mask, 0, (1 << m)) result += sum[n - 1][mask][pawns][degree];
	
	ll ile_stanow = 0;
	rep(mask, 0, (1 << m)) ile_stanow += ile[n - 1][mask][pawns][degree];
	debug(ile_stanow);
	return result;
}

int main() {
	boost;
	cin >> n >> m;
	vector <string> startowe(n);
	rep(i, 0, n) cin >> startowe[i];
	vector<string> koncowe(n);
	rep(i, 0, n) cin >> koncowe[i];
	
	bool deg = degree(startowe);
	bool degfinish = degree(koncowe);
	if (deg != degfinish) OUT(0);
	
	int trans = possible_transitions(koncowe);
	ll sumdegrees = sum_all_transitions(find_pawns(startowe), deg);
	debug(trans, sumdegrees);
	
	cout << fixed << setprecision(15);
	long double result = (long double)trans / (long double)sumdegrees;
	cout << result << endl;
}