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

#ifdef DEBUG
#include "debug.h"
#else
#define debug(...) ;
#endif

#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, n, m) for (int i = (n); i < (m); ++i)

#define repr(i, n) for (int i = (n) - 1; i >= 0; --i)
#define reppr(i, n, m) for (int i = (m) - 1; i >= (n); --i)

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;

template <typename T, typename V> void mini(T& a, V b) {if (b < a) a = b; }
template <typename T, typename V> void maxi(T& a, V b) {if (b > a) a = b; }

ll nck(int n, int k) {
    if (!k)
        return 1;

    return nck(n-1,k-1)*n/k;
}

void solve() {
    int n, m, k = 0;
    cin >> n >> m;

    vector<string> s(n), b(n);

    for (string &str : s) cin >> str;
    for (string &str : b) cin >> str;

    ll ss = 0, sb = 0;

    rep(i, n) rep(j, m) {
        if (s[i][j] == 'O') {ss += i + j; ++k; }
        if (b[i][j] == 'O') sb += i + j;
    }

    if (1&(ss^sb)) {
        cout << "0\n";
        return;
    }

    ll total = (4*n*m-2*n-2*m) * nck(n*m-2, k-1), here = 0;

    rep(i, n) rep(j, m-1) here += b[i][j] != b[i][j+1];
    rep(i, n-1) rep(j, m) here += b[i][j] != b[i+1][j];

    debug(here, total);

    long double ans = 2*here;
    ans /= total;

    debug(ans);

    printf("%.16Lf\n", ans);
}

int main() {
#ifndef DEBUG
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#endif
    int z = 1;
    // scanf("%d", &z);
    while (z--)
        solve();
    return 0;
}