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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <set>

int n,m;
int k;

// uint64_t D[256][2][8];
uint64_t D[8][256][2][9];
uint64_t B[8][256][2][9];
uint64_t C[256];

uint64_t CountBits(int x) {
    uint64_t sum = 0;
    while (x > 0) {
        if (x&1) ++sum;
        x >>= 1;
    }
    return sum;
}

uint64_t Parity(uint64_t s) {
    uint64_t p = 0;
    for (int i=0;i<64;++i) {
        if (s&(uint64_t(1ull)<<i)) {
            p+= (i&7) + (i>>3);
        }
    }
    return p&1;
}

std::vector<std::pair<int,int>> ToPos(uint64_t s) {
    std::vector<std::pair<int,int>> pos;
    for (int i=0;i<64;++i) {
        if (s&(uint64_t(1ull)<<i)) pos.push_back({i&7,i>>3});
    }
    return pos;
}

uint64_t FromPos(const std::vector<std::pair<int,int>>& pos) {
    uint64_t s = 0;
    for (auto[x,y] : pos) {
        s|= uint64_t(1ull)<<(x|(y<<3));
    }
    return s;
}

std::pair<int,int> operator+(const std::pair<int,int> &a, const std::pair<int,int>& b) {
    auto[x,y] = a;
    auto[u,v] = b;
    return {x+u, y+v};
}

std::pair<int,int> move[] = {{-1,0},{1,0},{0,-1},{0,1}};

// std::set<uint64_t> BFS(uint64_t s) {
//     std::set<uint64_t> Q = {s};
//     std::set<uint64_t> V = {s};
//     while (!Q.empty()) {
//         s = *Q.begin();
//         Q.erase(Q.begin());

//         auto pos = ToPos(s);
//         for ()
//     }
// }

uint64_t count(uint64_t s) {
    uint64_t c = 0;
    for (auto p : ToPos(s)) {
        for (auto dp : move) {
            auto[x,y] = p+dp;
            if (x < 0 || x >= m || y < 0 || y >= n) continue;
            if (s & FromPos({{x,y}})) continue;
            ++c;
        }
    }
    return c;
}

uint64_t countLine(uint64_t s) {
    uint64_t c = 0;
    for (auto p : ToPos(s)) {
        for (auto dp : move) {
            auto[x,y] = p+dp;
            if (x < 0 || x >= m || y < 0 || y >= 1) continue;
            if (s & FromPos({{x,y}})) continue;
            ++c;
        }
    }
    return c;
}

uint64_t M[9] = {0x0, 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff};

void brute2(uint64_t s = 0, int i = 0, int o = 0, int par = 0) {

    if (o > k) return;
    if (i > m) return;
    if (i == m){
        D[0][s&0xff][par][o] += countLine(s);
        B[0][s&0xff][par][o] += 1;
        return;
    }
    int x = i%m;
    int y = i/m;
    brute2(s | FromPos({{x,y}}), i+1, o+1, (par+x+y)&1);
    brute2(s, i+1, o, par);
}

uint64_t brute(uint64_t parity) {
    // if (n*m > 32) return 0;
    // uint64_t sum = 0;
    // for (uint64_t i=0; i<(1ull<<n*m); ++i) {
    //     uint64_t s = 0;
    //     for (int y=0;y<n;++y) {
    //         s |= ((i>>(m*y))&M[m])<<(8*y);
    //     }
    //     // std::clog << " :: # = " << k << std::endl;
    //     if (ToPos(s).size() == k) {
    //         if (Parity(s) == parity)
    //         // std::clog << s << std::endl;
    //             sum += count(s);
    //     }
    // }
    // return sum;
    
    brute2();
    // std::clog << "B";
    // nn = 2;

    // brute2();
    // std::clog << "C";

    int max = 1<<m;

    for (int i=0;i<max;++i) C[i] = CountBits(i);



    // for (int pa = 0; pa < 2; ++pa)
    // for (int a = 0;a<=k;++a)
    // for (int i=0;i<max;++i) {
    //     if (!B[0][i][pa][a]) continue;
    //     std::clog << i << " " << pa << " " << a << " = " << D[0][i][pa][a] << std::endl;
    // }

    for (int g=1;g<n;++g)
    for (int pa = 0; pa < 2; ++pa)
    for (int pb = 0; pb < 2; ++pb)
    for (int a = 0;a<=k;++a)
    for (int b=0;b+a<=k;++b)
    for (int i=0;i<max;++i) {
        for (int j=0;j<max;++j) {
            if (!B[g-1][i][pa][a] || !B[0][j][pb][b]) continue;
            int pp = pb;
            pp ^= (g*b)&1;
            D[g][j][pa^pp][a+b] += D[g-1][i][pa][a] + B[g-1][i][pa][a] * (D[0][j][pb][b] + C[i] + C[j] - 2*C[i&j]);
            B[g][j][pa^pp][a+b] += B[g-1][i][pa][a];
        }
    }
        // std::clog << " #### " << std::endl;
    
    // for (int g=0;g<n;++g)
    // for (int a = 0;a<=k;++a)
    // for (int i=0;i<max;++i) {
    //     if (B[g][i][0][a] && B[g][i][1][a]) {
    //         std::cout << g << " :: " << i << " " << a << " = " << D[g][i][0][a]+D[g][i][1][a] << std::endl;
    //     } else if (B[g][i][0][a]) {
    //         std::cout << g << " :: " << i << " " << a << " = " << D[g][i][0][a] << std::endl;
    //     } else if (B[g][i][1][a]) {
    //         std::cout << g << " :: " << i << " " << a << " = " << D[g][i][1][a] << std::endl;
    //     }
    // }

    // parity ^= 1;
    uint64_t sum = 0;
    for (int i=0;i<max;++i) {
        if (!B[n-1][i][parity][k]) continue;
        sum += D[n-1][i][parity][k];
        // std::clog << i << " :: " << D[n-1][i][parity][k] << std::endl;
    }
    // std::clog << "sum = " << sum << std::endl;

    return sum;
}

// uint64_t brute(int parity, const std::vector<std::pair<int,int>>& pos, int v) {
//     if (v >= n*m) return 0;
//     if (pos.size() == k) {
//         // if (Parity(FromPos(pos) != parity)) return 0;
//         // else
//         return count(FromPos(pos));
//     }
    
//     uint64_t sum = 0;
//     for (uint64_t i=v; i<n*m; ++i) {
//         auto pos2 = pos;
//         pos2.push_back({i%m, i/m});
//         sum += brute(parity, pos2, i+1);
//     }
//     return sum;
// }

uint64_t read() {
    uint64_t s = 0;
    for (int y=0;y<n;++y) {
        std::string ss;
        std::cin >> ss;
        // std::clog << ss << std::endl;
        for (int x=0;x<m;++x)
            if (ss[x] == 'O') s |= uint64_t(1ull) << (x+8*y);
    }
    return s;
}

int main() {
    std::ios_base::sync_with_stdio(0);
    std::cin >> n >> m;
    uint64_t s0 = read();
    uint64_t s1 = read();

    // std::clog << s0 << " " << s1 << std::endl;
    // std::clog << Parity(s0) << " " << Parity(s1) << std::endl;
    if (Parity(s0) != Parity(s1)) {
        std::cout << 0 << std::endl;
        return 0;
    }
    k = ToPos(s1).size();

    // std::clog << " :: count = " << count(s1) << std::endl;
    std::cout.precision(15);
    std::cout << std::fixed <<(long double)count(s1) / brute(Parity(s1)) << std::endl;

    return 0;
}