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

using namespace std;

typedef unsigned long long lld;

int n, m, k;
const int sft[] { 0, 0, 1, -1 };

bool parity(vector<string> &v) {
    bool res = false;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
            if (v[i][j] == 'O') res ^= (i + j) & 1;
    return res;
}

bool empty(vector<string> &board, int x, int y) {
    return x >= 0 && y >= 0 && x < n && y < m && board[x][y] == '.';
}

int calc_deg(vector<string> &board) {
    int deg = 0;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
            if (board[i][j] == 'O')
                for (int dx = 0; dx < 4; ++dx)
                    if (empty(board, i+sft[dx], j+sft[3-dx]))
                        ++deg;
    return deg;
}

lld memo[63][8];

void init_memo() {
    for (int i = 0; i < 63; ++i) {
        memo[i][0] = 1;
        if (i < 8) memo[i][i] = 1;
        for (int j = 1; j < min(8, i); ++j)
            memo[i][j] = memo[i-1][j] + memo[i-1][j-1];
    }
}

lld newton(int n, int k) {
    return memo[n][k];
}

// for every directed physical edge (pawn move) we have (nm-2 choose k-1) possible static pawns (2 tiles are reserved for the move)
lld count_edges() {
    return (lld)((n-1)*m + n*(m-1)) * newton(n*m-2, k-1);
}

lld gcd(lld a, lld b) {
    while (b) {
        lld c = a % b;
        a = b;
        b = c;
    }
    return a;
}

void normalize(lld &a, lld &b) {
    lld d = gcd(a, b);
    a /= d;
    b /= d;
}

void divide(lld x, lld y, int precision = 15) {
    lld z = x/y;
    cout << z << '.';
    x -= z*y;
    x *= 10;
    for (int i = 0; i < precision; ++i) {
        z = x/y;
        cout << z;
        x -= z*y;
        x *= 10;
    }
    cout << '\n';
}

void calc(vector<string> &board) {
    lld E = count_edges(), deg = calc_deg(board);
    normalize(E, deg);
    divide(deg, E);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> m;
    vector<string> board(n), result(n);
    for (string &i : board) cin >> i;
    for (string &i : result) cin >> i;
    if (parity(board) != parity(result)) {
        cout << "0\n";
        return 0;
    }
    for (string &s : board)
        for (char &c : s)
            k += (c == 'O');

    init_memo();
    calc(result);
}