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
136
137
138
139
140
141
142
143
144
145
#include <algorithm>
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <ranges>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long LL;
// HMMMM
#define int LL

vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, 1, 0, -1};

int n, m;

bool inbound(int x, int y) {
    if (x < 0 || x >= n) return false;
    if (y < 0 || y >= m) return false;
    return true;
}

bool valid(int x, int y, vector<string> &a) {
    return inbound(x, y) && a[x][y] == '.';
}

vector<vector<string>> moves(vector<string> a) {
    vector<vector<string>> res = {};
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (a[i][j] == '.') continue;
            for (int k = 0; k < dx.size(); k++) {
                int x = i + dx[k];
                int y = j + dy[k];
                if (!valid(x, y, a)) continue;
                swap(a[i][j], a[x][y]);
                res.push_back(a);
                swap(a[i][j], a[x][y]);
            }
        }
    }
    return res;
}

int check_parity(vector<string> a) {
    int res = 0;
    for (int i = 0; i < a.size(); i++) {
        for (int j = 0; j < a[i].size(); j++) {
            if (a[i][j] != 'O') continue;
            res += i + j;
        }
    }
    return res;
}

int memo[8][8][8][8][4][8][2];

int f(int x, int y, int i, int j, int k, int count, int parity) {
    parity %= 2;

    if (y == m) {
        x++;
        y = 0;
    }
    if (x == n) {
        return count == 0 && parity == 0;
    }
    int &res = memo[x][y][i][j][k][count][parity];
    if (res != -1) return res;
    res = f(x, y + 1, i, j, k, count, parity);
    bool ok = true;
    ok &= !(x == i && y == j);
    ok &= !(x == i + dx[k] && y == j + dy[k]);
    ok &= !(count == 0);
    if (ok) {
        res += f(x, y + 1, i, j, k, count - 1, parity + x + y);
    }
    return res;
}

pair<int, int> solve(vector<string> a, vector<string> b) {
    n = (int)a.size();
    m = (int)a.front().size();

    int parity = check_parity(b);

    if ((check_parity(a) - parity) % 2) return {0, 1};

    int count = 0;
    for (auto &x : b)
        for (auto &y : x) count += y == 'O';
    int total = 0;

    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            for (int x = 0; x < n; x++)
                for (int y = 0; y < m; y++)
                    for (int k = 0; k < dx.size(); k++)
                        for (int u = 0; u < count; u++)
                            for (int v = 0; v < 2; v++)
                                memo[i][j][x][y][k][u][v] = -1;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            for (int k = 0; k < dx.size(); k++) {
                if (!inbound(i + dx[k], j + dy[k])) continue;
                total += f(0, 0, i, j, k, count - 1, i + j);
            }
        }
    }

    return {int(moves(b).size()), total};
}

pair<int, int> solve2() {
    cin >> n >> m;
    vector<string> a(n);
    for (auto &x : a) cin >> x;
    vector<string> b(n);
    for (auto &x : b) cin >> x;
    return solve(a, b);
}

void out(pair<int, int> res) {
    auto [x, y] = res;
    cout << setprecision(17) << fixed << double(x) / double(y) << endl;
}

#undef int
int main() {
    ios::sync_with_stdio(false);
    cin.exceptions(cin.failbit);
    cin.tie(0);
    out(solve2());
}