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
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;

typedef pair<int,int> par;
string s;
long long n, m, k, tab[10][10], tac[10][10], total_edg, newt[100][100];
par moves[4] = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}};

long long total_edges()
{
	newt[0][0] = 1;
	for(int i=1; i<=62; i++)
	{
		newt[i][0] = 1;
		for(int j=1; j<=i; j++) newt[i][j] = newt[i-1][j] + newt[i-1][j-1];
	}
	long long kraw = n * (m - 1) + (n - 1) * m;
	kraw = kraw * newt[n * m - 2][k - 1];
	return kraw;
}

int main()
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	long long a, b, dx = 0, dy = 0, ddx = 0, ddy = 0, edg = 0, total_edg = 0;
	par p;
	cin>>n>>m;
	k = 0;
	for(int i=1; i<=n; i++)
	{
		cin>>s;
		for(int j=0; j<m; j++)
		{
			if(s[j] == 'O')
			{
				tab[i][j+1] = 1;
				k++;
				dx += (i % 2);
				dy += ((j+1) % 2);
			}
		}
	}
	for(int i=1; i<=n; i++)
	{
		cin>>s;
		for(int j=0; j<m; j++)
		{
			if(s[j] == 'O')
			{
				tac[i][j+1] = 1;
				ddx += (i % 2);
				ddy += ((j+1) % 2);
			}
		}
	}
	if((dx + dy) % 2 != (ddx + ddy) % 2)
	{
		cout<<"0";
		return 0;
	}
	
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<=m; j++)
		{
			if(tac[i][j] == 1)
			{
				for(int l=0; l<4; l++)
				{
					p = moves[l];
					a = i + p.fi;
					b = j + p.se;
					if(a >= 1 && a <= n && b >= 1 && b <= m && tac[a][b] == 0) edg++;
				}
			}
		}
	}
	
	total_edg = total_edges();
	double dedg = edg, dtotal = total_edg;
	dedg /= dtotal;
	cout<<setprecision(17);
	cout<<fixed;
	cout<<dedg;
	return 0;
}