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

int n,m;

char start[10][10], aim[10][10];

int parity;
int parity2;

long long licznik, mianownik;

int ile[5][2];

long long p, np, vp, vnp;

int value(int i,int j)
{
    int res = 0;
    if(i!=1) res++;
    if(j!=1) res++;
    if(i!=n) res++;
    if(j!=m) res++;
    return res;
}

int value2(int i,int j)
{
    int res = 0;
    if(i!=1&&aim[i-1][j]!='O') res++;
    if(j!=1&&aim[i][j-1]!='O') res++;
    if(i!=n&&aim[i+1][j]!='O') res++;
    if(j!=m&&aim[i][j+1]!='O') res++;
    return res;
}

int pionki;

long long beczka (int N, int K)
{
    if(N<K)return 0;
    long long res = 1;
    for(int i=1;i<=K;i++)
    {
        res*=N;
        res/=i;
        N--;
    }
    return res;
}

int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>start[i][j];
            if(start[i][j]=='O')
            {
                parity+=i;
                parity+=j;
                pionki++;
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>aim[i][j];
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(aim[i][j]=='O')
            {
                parity2+=i;
                parity2+=j;
                licznik += value2(i,j);
            }
            ile[value(i,j)][(i+j)%2]++;
        }
    }
    if((parity%2) != (parity2%2))
    {
        cout<<"0";
        return 0;
    }

    for(int i=1;i<=4;i++)
    {
        p+=ile[i][0];
        np+=ile[i][1];
        vp+=i*ile[i][0];
        vnp+=i*ile[i][1];
    }

    //cout<<p<<" "<<np<<" "<<vp<<" "<<vnp<<endl;

    for(int i=0;i<=pionki;i++)
    {
        if((i%2)==(parity%2))
        {
            long long A = (beczka(np,i)-beczka(np-1,i))*vnp *beczka(p,pionki-i);
            //if(i==0)A=1;
            long long B = (beczka(p,pionki-i)-beczka(p-1,pionki-i))*vp*beczka(np,i);
            //if(i==pionki)B=1;
            //cout<<A<<" "<<B<<" ";
            A=A+B;
            long long C=0;
            if(i>0&&pionki-i>0)
            {
                C = 2 * (n*(m-1)+m*(n-1)) * beczka (np-1,i-1) * beczka (p-1,pionki-i-1);
            }
            //cout<<C<<endl;
            mianownik +=A;
            mianownik -=C;
        }
    }

    //cout<<licznik<<" "<<mianownik;

    long double d = licznik;
    d/=mianownik;

    cout<<setprecision(13)<<fixed<<d;




}