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
// Marcin Knapik

#pragma GCC optimize ("O3")
#include<bits/stdc++.h>
using namespace std;

#define FOR(i, n) for (int i = 0; i < n; i++)
#define f first
#define s second
#define pb push_back
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;

template <class T>ostream &operator<<(ostream &os, vector<T> &vec){for (T &el : vec){os << el << ' ';}return os;}
template <class T>istream &operator>>(istream &is, vector<T> &vec) {for (T &el : vec){is >> el;}return is;}

template <class T, class G> ostream &operator<<(ostream &os, pair<T, G> para) { os << para.f << ' ' << para.s; return os;}

using ll = long long;
using vi = vector<int>;
using ii = pair<int, int>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;
using vii = vector<ii>;
using mp = map<vector<string>, long double>;

int n, m;

using ld = long double;

ld sil(int x){
    ld ret = 1.0;
    for(int i = 2; i <= x; i++){
        ret = ret * i;
    }
    return ret;
}

ld choose(ll a, ll b){
    return sil(a) / sil(b) / sil(a - b);
}

bool parzystosc(vector<string> mapa){
    bool ret = false;

    FOR(i, n){
        FOR(j, m){
            if(mapa[i][j] == 'O'){
                ret ^= (i + j) % 2;
            }
        }
    }
    return ret;
}

int liczba_ruchow(vector<string> mapa){
    int ret = 0;
    FOR(i, n){
        FOR(j, m - 1){
            ret += ((mapa[i][j] == '.') ^ (mapa[i][j + 1] == '.'));
        }
    }
    FOR(i, n - 1){
        FOR(j, m){
            ret += ((mapa[i][j] == '.') ^ (mapa[i + 1][j] == '.'));
        }
    }
    return ret;
}

void solve () {
    cin >> n >> m;

    vector<string> start(n), koniec(n);
    FOR(i, n){
        cin >> start[i];
    }

    FOR(i, n){
        cin >> koniec[i];
    }

    cout << setprecision(15) << fixed;
    if(parzystosc(start) != parzystosc(koniec)){
        cout << 0.0 << "\n";
        return;
    }

    int ruchy = liczba_ruchow(koniec);
    
    int pionki = 0;
    for(auto & u : start){
        for(auto & v : u){
            pionki += v == 'O';
        }
    }

    long double ppb = 1.0 / (((n * (m - 1)) + (m * (n - 1))) * choose(n * m - 2, pionki - 1));
    cout << ppb * ruchy << '\n';
}

int main () {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int tests = 1;
    // cin >> tests;

    for (int test = 1; test <= tests; test++) {
        solve();
    }

    return 0;
}