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
//GRT_2018
#include <bits/stdc++.h>
#define PB push_back
#define ST first
#define ND second
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

using namespace std;

using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;

vector<ll> memo = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,3,3,6,6,3,3,0,0,0,0,0,0,0,0,0,0,4,4,12,12,12,12,4,4,0,0,0,0,0,0,0,0,5,5,20,20,30,30,20,20,5,5,0,0,0,0,0,0,6,6,30,30,60,60,60,60,30,30,6,6,0,0,0,0,7,7,42,42,105,105,140,140,105,105,42,42,7,7,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,4,8,8,4,4,0,0,0,0,0,0,0,0,0,0,7,7,28,28,42,42,28,28,7,7,0,0,0,0,0,0,10,10,60,60,150,150,200,200,150,150,60,60,10,10,0,0,13,13,104,104,364,364,728,728,910,910,728,728,364,364,104,104,16,16,160,160,720,720,1920,1920,3360,3360,4032,4032,3360,3360,1920,1920,19,19,228,228,1254,1254,4180,4180,9405,9405,15048,15048,17556,17556,15048,15048,22,22,308,308,2002,2002,8008,8008,22022,22022,44044,44044,66066,66066,75504,75504,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,7,7,28,28,42,42,28,28,7,7,0,0,0,0,0,0,12,12,84,84,252,252,420,420,420,420,252,252,84,84,12,12,17,17,170,170,765,765,2040,2040,3570,3570,4284,4284,3570,3570,2040,2040,22,22,286,286,1716,1716,6292,6292,15730,15730,28314,28314,37752,37752,37752,37752,27,27,432,432,3240,3240,15120,15120,49140,49140,117936,117936,216216,216216,308880,308880,32,32,608,608,5472,5472,31008,31008,124032,124032,372096,372096,868224,868224,1612416,1612416,37,37,814,814,8547,8547,56980,56980,270655,270655,974358,974358,2760681,2760681,6310128,6310128,3,3,6,6,3,3,0,0,0,0,0,0,0,0,0,0,10,10,60,60,150,150,200,200,150,150,60,60,10,10,0,0,17,17,170,170,765,765,2040,2040,3570,3570,4284,4284,3570,3570,2040,2040,24,24,336,336,2184,2184,8736,8736,24024,24024,48048,48048,72072,72072,82368,82368,31,31,558,558,4743,4743,25296,25296,94860,94860,265608,265608,575484,575484,986544,986544,38,38,836,836,8778,8778,58520,58520,277970,277970,1000692,1000692,2835294,2835294,6480672,6480672,45,45,1170,1170,14625,14625,117000,117000,672750,672750,2960100,2960100,10360350,10360350,29601000,29601000,52,52,1560,1560,22620,22620,211120,211120,1425060,1425060,7410312,7410312,30876300,30876300,105861600,105861600,4,4,12,12,12,12,4,4,0,0,0,0,0,0,0,0,13,13,104,104,364,364,728,728,910,910,728,728,364,364,104,104,22,22,286,286,1716,1716,6292,6292,15730,15730,28314,28314,37752,37752,37752,37752,31,31,558,558,4743,4743,25296,25296,94860,94860,265608,265608,575484,575484,986544,986544,40,40,920,920,10120,10120,70840,70840,354200,354200,1345960,1345960,4037880,4037880,9806280,9806280,49,49,1372,1372,18522,18522,160524,160524,1003275,1003275,4815720,4815720,18460260,18460260,58017960,58017960,58,58,1914,1914,30624,30624,316448,316448,2373360,2373360,13765488,13765488,64238944,64238944,247778784,247778784,67,67,2546,2546,47101,47101,565212,565212,4945605,4945605,33630114,33630114,184965627,184965627,845557152,845557152,5,5,20,20,30,30,20,20,5,5,0,0,0,0,0,0,16,16,160,160,720,720,1920,1920,3360,3360,4032,4032,3360,3360,1920,1920,27,27,432,432,3240,3240,15120,15120,49140,49140,117936,117936,216216,216216,308880,308880,38,38,836,836,8778,8778,58520,58520,277970,277970,1000692,1000692,2835294,2835294,6480672,6480672,49,49,1372,1372,18522,18522,160524,160524,1003275,1003275,4815720,4815720,18460260,18460260,58017960,58017960,60,60,2040,2040,33660,33660,359040,359040,2782560,2782560,16695360,16695360,80694240,80694240,322776960,322776960,71,71,2840,2840,55380,55380,701480,701480,6488690,6488690,46718568,46718568,272524980,272524980,1323692760,1323692760,82,82,3772,3772,84870,84870,1244760,1244760,13381170,13381170,112401828,112401828,768079158,768079158,4389023760,4389023760,6,6,30,30,60,60,60,60,30,30,6,6,0,0,0,0,19,19,228,228,1254,1254,4180,4180,9405,9405,15048,15048,17556,17556,15048,15048,32,32,608,608,5472,5472,31008,31008,124032,124032,372096,372096,868224,868224,1612416,1612416,45,45,1170,1170,14625,14625,117000,117000,672750,672750,2960100,2960100,10360350,10360350,29601000,29601000,58,58,1914,1914,30624,30624,316448,316448,2373360,2373360,13765488,13765488,64238944,64238944,247778784,247778784,71,71,2840,2840,55380,55380,701480,701480,6488690,6488690,46718568,46718568,272524980,272524980,1323692760,1323692760,84,84,3948,3948,90804,90804,1362060,1362060,14982660,14982660,128850876,128850876,901956132,901956132,5282885916,5282885916,97,97,5238,5238,138807,138807,2405988,2405988,30676347,30676347,306763470,306763470,2505235005,2505235005,17178754320,17178754320,7,7,42,42,105,105,140,140,105,105,42,42,7,7,0,0,22,22,308,308,2002,2002,8008,8008,22022,22022,44044,44044,66066,66066,75504,75504,37,37,814,814,8547,8547,56980,56980,270655,270655,974358,974358,2760681,2760681,6310128,6310128,52,52,1560,1560,22620,22620,211120,211120,1425060,1425060,7410312,7410312,30876300,30876300,105861600,105861600,67,67,2546,2546,47101,47101,565212,565212,4945605,4945605,33630114,33630114,184965627,184965627,845557152,845557152,82,82,3772,3772,84870,84870,1244760,1244760,13381170,13381170,112401828,112401828,768079158,768079158,4389023760,4389023760,97,97,5238,5238,138807,138807,2405988,2405988,30676347,30676347,306763470,306763470,2505235005,2505235005,17178754320,17178754320,112,112,6944,6944,211792,211792,4235840,4235840,62478640,62478640,724752224,724752224,6885146128,6885146128,55081169024,55081169024};

const int nax = 10;
int n, m;
int grid[nax][nax];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m;
	int par1 = 0;
	for (int i = 0; i < n; ++i) {
		string s;
		cin >> s;
		for (int j = 0; j < m; ++j) {
			if (s[j] == 'O') {
				par1 ^= i;
				par1 ^= j;
			}
		}
	}
	int par2 = 0;
	for (int i = 0; i < n; ++i) {
		string s;
		cin >> s;
		for (int j = 0; j < m; ++j) {
			if (s[j] == 'O') {
				grid[i][j] = 1;
				par2 ^= i;
				par2 ^= j;
			}
		}
	}
	if ((par2 & 1) ^ (par1 & 1)) {
		cout << "0";
		return 0;
	}
	int cnt = 0;
	int ile = 0;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			if (grid[i][j] == 1) {
				cnt++;
				if (i > 0 && !grid[i - 1][j]) ile++;
				if (i < n - 1 && !grid[i + 1][j]) ile++;
				if (j > 0 && !grid[i][j - 1]) ile++;
				if (j < m - 1 && !grid[i][j + 1]) ile++;
			}
		}
	}
	int pos = 0;
	ll d = 1;
	for (int i = 1; i <= 8; ++i) {
		for (int j = 1; j <= 8; ++j) {
			for (int k = 1; k <= 8; ++k) {
				if (k == cnt && i == n && j == m) {
					if (par1 & 1) {
						d = memo[pos + 1];
					} else {
						d = memo[pos];
					}
				}
				pos += 2;
			}
		}
	}
	cout << setprecision(15);
	cout << fixed;
	cout << (long double)ile / d;
}