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

using namespace std;

#define ALL(x) (x).begin(), (x).end()

template<class C> void mini(C &a5, C b5) { a5 = min(a5, b5); }
template<class C> void maxi(C &a5, C b5) { a5 = max(a5, b5); }

#ifdef LOCAL
const bool debug = true;
#else
const bool debug = false;
#define cerr if (true) {} else cout
#endif
// clang-format on

#define int long long
#define double long double

const int INF = 1e18;
const int mod = 1e9 + 7;
const int NAX = 10;

int n, m;
int input_parity;
int output_parity, output_pawns;
bool pos_start[NAX][NAX], pos_end[NAX][NAX];
int newton[NAX * NAX][NAX * NAX];
vector<pair<int, int> > directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
//vector<pair<int, int> > directions = {{0, 1}};

void read_input() {
	cin >> n >> m;
	char c;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> c;
			pos_start[i][j] = (c == 'O');
			if (pos_start[i][j]) {
				input_parity += i + j;
			}
		}
	}
	input_parity %= 2;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> c;
			pos_end[i][j] = (c == 'O');
			if (pos_end[i][j]) {
				output_parity += i + j;
				output_pawns++;
			}
		}
	}
	output_parity %= 2;
}

inline bool add_step(int &x, int &y, pair<int, int> step) {
	x += step.first;
	y += step.second;
	if (x < 0 || n <= x) return false;
	if (y < 0 || m <= y) return false;
	return true;
}

int xd(int n_pawns, int parity, int taken_even, int taken_odd) {
	if (n_pawns < 0) return 0;
	
	int n_even = (n * m + 1) / 2 - taken_even;
	int n_odd = n * m / 2 - taken_odd;
	
	int res = 0;
	for (int i = parity%2; i <= n_pawns; i+=2) {
		if (n_pawns-i < 0) continue;
		res += newton[n_odd][i] * newton[n_even][n_pawns-i] ;
	}
	return res;
}

int calc_patterns(int n_pawns, int parity, pair<int, int> step1) {
	int res = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			int x = i, y = j;
			if (!add_step(x, y, step1)) continue;
			res += xd(n_pawns-1, parity+i+j, 1, 1);
			//cerr << "i,j " << i << "," << j << " res:" << res << "\n";
			//cerr << "taken even,odd: " << taken_even << " " << taken_odd << "\n";
		}
	}
	return res;
}

int calc_output_edges(pair<int, int> step1) {
	int res = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			int x = i, y = j;
			if (!pos_end[x][y]) continue; // zaczynamy z pionka
			if (!add_step(x, y, step1)) continue;
			if (pos_end[x][y]) continue; // po ruchu nie ma pionka
			res++;
		}
	}
	return res;
}

void init_newton() {
	for (int i = 0; i < NAX * NAX; i++) {
		for (int j = 0; j <= i; j++) {
			if (j == 0 || j == i) {
				newton[i][j] = 1;
				continue;
			}
			newton[i][j] = newton[i-1][j-1] + newton[i-1][j];
		}	
	}
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    init_newton();
    read_input();
    
    if (input_parity != output_parity) {
		cout << "0\n";
		return 0;
	}
    
    int sum_k = 0;
    int output_edges = 0;
    for (auto step1 : directions) {
			//cerr << "output pawns: " << output_pawns << endl;
			sum_k += calc_patterns(output_pawns, output_parity, step1);
			output_edges += calc_output_edges(step1);
			cerr << step1.first << "," << step1.second << "\t sum_k " << sum_k << "\t output_edges " << output_edges << "\n";
	}
	cerr << sum_k << " " << output_edges << "\n";
	cout << fixed << setprecision(15) << (double)output_edges / sum_k << "\n";
    return 0;
}/*
1 3
OO.

OO.

1 5
O....
....O

1 3
O..
..O

2 2
O.
.O
OO
..
*/