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

using namespace std;

#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ld long double
ll mod=1000000007;
int inf=1000000007;
ll infl=1000000000000000007;

char g[17][17];

ll npok(ll n,ll k)
{
    if(k==0) return 1;
    if(k>n||n<0||k<0) return 0;
    ll ans=1;
    for(ll i=n-k+1;i<=n;i++) ans*=i;
    for(ll i=1;i<=k;i++) ans/=i;
    return ans;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,m;
    cin>>n>>m;
    int c0=0,c1=0;
    bool par=0,par1=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>g[i][j];
            if(g[i][j]=='O') par^=(i+j)%2;
            if((i+j)%2==0) c0++;
            else c1++;
        }
    }
    ll deg=0;
    int c=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>g[i][j];
            if(g[i][j]=='O')
            {
                par1^=(i+j)%2;
                c++;
            }
            if(i>1&&((g[i][j]=='O')+(g[i-1][j]=='O'))==1) deg++;
            if(j>1&&((g[i][j]=='O')+(g[i][j-1]=='O'))==1) deg++;
        }
    }
    if(par!=par1)
    {
        cout<<0<<endl;
        return 0;
    }
    ll ways=npok(n*m-2,c-1);
    ll sum=(n*(m-1)+m*(n-1))*ways;
    cout<<fixed<<setprecision(15)<<(ld)deg/(ld)sum<<endl;

    return 0;
}