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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include <iostream>
#include <vector>
#include <unordered_set>
#include <time.h>
#include <unordered_map>
#include <bitset>

using namespace std;

struct VectorHash {
    size_t operator()(const std::vector<int>& v) const {
        hash<int> hasher;
        size_t seed = 0;
        for (int i : v) {
            seed ^= hasher(i) + 0x9e3779b9 + (seed<<6) + (seed>>2);
        }
        return seed;
    }
};

void add_possible_move(int n, int m, unordered_set<vector<int>, VectorHash>& pawnPositions,
    vector<int>& pawnPosition, int rowShift, int colShift, vector<vector<int>>& possibleMoves) {
    if (pawnPosition[0] + rowShift >= 0 && pawnPosition[0] + rowShift < n &&
        pawnPosition[1] + colShift >= 0 && pawnPosition[1] + colShift < m &&
        pawnPositions.count({ pawnPosition[0] + rowShift, pawnPosition[1] + colShift }) == 0) {
        possibleMoves.push_back({ pawnPosition[0], pawnPosition[1],
            pawnPosition[0] + rowShift, pawnPosition[1] + colShift });
    }
}

unsigned long long get_possible_moves_number(int n, int m, unordered_set<vector<int>, VectorHash>& pawnPositions) {
    vector<vector<int>> possibleMoves;

    for (auto pawnPosition : pawnPositions) {
        add_possible_move(n, m, pawnPositions, pawnPosition, 0, -1, possibleMoves);
        add_possible_move(n, m, pawnPositions, pawnPosition, -1, 0, possibleMoves);
        add_possible_move(n, m, pawnPositions, pawnPosition, 0, 1, possibleMoves);
        add_possible_move(n, m, pawnPositions, pawnPosition, 1, 0, possibleMoves);
    }

    return possibleMoves.size();
}

void populate_board(vector<vector<int>>& board, unordered_set<vector<int>, VectorHash>& pawnPositions) {
    char square;
    
    for (int i = 0; i < board.size(); i++) {
        for (int j = 0; j < board[0].size(); j++) {
            cin >> square;
            if (square == 'O') {
                board[i][j] = 1;
                pawnPositions.insert({ i, j });
            }
            else {
                board[i][j] = 0;
            }
        }
    }
}

vector<int> get_on_white_and_black_count(vector<vector<int>>& board) {
    int on_black = 0;
    int on_white = 0;

    for (int i = 0; i < board.size(); i++) {
        for (int j = 0; j < board[0].size(); j++) {
            if (board[i][j] == 1 && (i + j) % 2 == 0) { // white
                on_white++;
            }
            if (board[i][j] == 1 && (i + j) % 2 == 1) // black
                on_black++;
        }
    }

    return { on_white , on_black };
}

bool is_transition_possible(vector<vector<int>>& initialBoard, vector<vector<int>>& wantedBoard) {
    vector<int> white_black_counts_initial = get_on_white_and_black_count(initialBoard);
    vector<int> white_black_counts_wanted = get_on_white_and_black_count(wantedBoard);

    int on_white_initial = white_black_counts_initial[0];
    int on_black_initial = white_black_counts_initial[1];    

    int on_white_wanted = white_black_counts_wanted[0];
    int on_black_wanted = white_black_counts_wanted[1];

    return ((on_white_initial % 2 == on_white_wanted % 2) &&
        (on_black_initial % 2 == on_black_wanted % 2));
}

void swap(int& n, int& m) {
    int tmp = n;
    n = m;
    m = tmp;
}

unsigned long long  get_total_possible_moves(int n, int m, int p, vector<vector<vector<unsigned long long>>>& totalPossibleMoves) {
    unsigned long long possible_moves_total = 0;
    
    if (m >= n) {
        possible_moves_total = totalPossibleMoves[p][n][m] / 2;
    }
    else {
        possible_moves_total = totalPossibleMoves[p][m][n] / 2;
    }

    return possible_moves_total;
}

int main()
{
    vector<vector<vector<unsigned long long>>> totalPossibleMoves(9, vector<vector<unsigned long long>>(9, vector<unsigned long long>(9, 0)));
    totalPossibleMoves[1][1][2] = 2;
    totalPossibleMoves[1][1][3] = 4;
    totalPossibleMoves[1][1][4] = 6;
    totalPossibleMoves[1][1][5] = 8;
    totalPossibleMoves[1][1][6] = 10;
    totalPossibleMoves[1][1][7] = 12;
    totalPossibleMoves[1][1][8] = 14;
    totalPossibleMoves[1][2][2] = 8;
    totalPossibleMoves[1][2][3] = 14;
    totalPossibleMoves[1][2][4] = 20;
    totalPossibleMoves[1][2][5] = 26;
    totalPossibleMoves[1][2][6] = 32;
    totalPossibleMoves[1][2][7] = 38;
    totalPossibleMoves[1][2][8] = 44;
    totalPossibleMoves[1][3][3] = 24;
    totalPossibleMoves[1][3][4] = 34;
    totalPossibleMoves[1][3][5] = 44;
    totalPossibleMoves[1][3][6] = 54;
    totalPossibleMoves[1][3][7] = 64;
    totalPossibleMoves[1][3][8] = 74;
    totalPossibleMoves[1][4][4] = 48;
    totalPossibleMoves[1][4][5] = 62;
    totalPossibleMoves[1][4][6] = 76;
    totalPossibleMoves[1][4][7] = 90;
    totalPossibleMoves[1][4][8] = 104;
    totalPossibleMoves[1][5][5] = 80;
    totalPossibleMoves[1][5][6] = 98;
    totalPossibleMoves[1][5][7] = 116;
    totalPossibleMoves[1][5][8] = 134;
    totalPossibleMoves[1][6][6] = 120;
    totalPossibleMoves[1][6][7] = 142;
    totalPossibleMoves[1][6][8] = 164;
    totalPossibleMoves[1][7][7] = 168;
    totalPossibleMoves[1][7][8] = 194;
    totalPossibleMoves[1][8][8] = 224;
    totalPossibleMoves[2][1][3] = 4;
    totalPossibleMoves[2][1][4] = 12;
    totalPossibleMoves[2][1][5] = 24;
    totalPossibleMoves[2][1][6] = 40;
    totalPossibleMoves[2][1][7] = 60;
    totalPossibleMoves[2][1][8] = 84;
    totalPossibleMoves[2][2][2] = 16;
    totalPossibleMoves[2][2][3] = 56;
    totalPossibleMoves[2][2][4] = 120;
    totalPossibleMoves[2][2][5] = 208;
    totalPossibleMoves[2][2][6] = 320;
    totalPossibleMoves[2][2][7] = 456;
    totalPossibleMoves[2][2][8] = 616;
    totalPossibleMoves[2][3][3] = 168;
    totalPossibleMoves[2][3][4] = 340;
    totalPossibleMoves[2][3][5] = 572;
    totalPossibleMoves[2][3][6] = 864;
    totalPossibleMoves[2][3][7] = 1216;
    totalPossibleMoves[2][3][8] = 1628;
    totalPossibleMoves[2][4][4] = 672;
    totalPossibleMoves[2][4][5] = 1116;
    totalPossibleMoves[2][4][6] = 1672;
    totalPossibleMoves[2][4][7] = 2340;
    totalPossibleMoves[2][4][8] = 3120;
    totalPossibleMoves[2][5][5] = 1840;
    totalPossibleMoves[2][5][6] = 2744;
    totalPossibleMoves[2][5][7] = 3828;
    totalPossibleMoves[2][5][8] = 5092;
    totalPossibleMoves[2][6][6] = 4080;
    totalPossibleMoves[2][6][7] = 5680;
    totalPossibleMoves[2][6][8] = 7544;
    totalPossibleMoves[2][7][7] = 7896;
    totalPossibleMoves[2][7][8] = 10476;
    totalPossibleMoves[2][8][8] = 13888;
    totalPossibleMoves[3][1][4] = 6;
    totalPossibleMoves[3][1][5] = 24;
    totalPossibleMoves[3][1][6] = 60;
    totalPossibleMoves[3][1][7] = 120;
    totalPossibleMoves[3][1][8] = 210;
    totalPossibleMoves[3][2][2] = 8;
    totalPossibleMoves[3][2][3] = 84;
    totalPossibleMoves[3][2][4] = 300;
    totalPossibleMoves[3][2][5] = 728;
    totalPossibleMoves[3][2][6] = 1440;
    totalPossibleMoves[3][2][7] = 2508;
    totalPossibleMoves[3][2][8] = 4004;
    totalPossibleMoves[3][3][3] = 504;
    totalPossibleMoves[3][3][4] = 1530;
    totalPossibleMoves[3][3][5] = 3432;
    totalPossibleMoves[3][3][6] = 6480;
    totalPossibleMoves[3][3][7] = 10944;
    totalPossibleMoves[3][3][8] = 17094;
    totalPossibleMoves[3][4][4] = 4368;
    totalPossibleMoves[3][4][5] = 9486;
    totalPossibleMoves[3][4][6] = 17556;
    totalPossibleMoves[3][4][7] = 29250;
    totalPossibleMoves[3][4][8] = 45240;
    totalPossibleMoves[3][5][5] = 20240;
    totalPossibleMoves[3][5][6] = 37044;
    totalPossibleMoves[3][5][7] = 61248;
    totalPossibleMoves[3][5][8] = 94202;
    totalPossibleMoves[3][6][6] = 67320;
    totalPossibleMoves[3][6][7] = 110760;
    totalPossibleMoves[3][6][8] = 169740;
    totalPossibleMoves[3][7][7] = 181608;
    totalPossibleMoves[3][7][8] = 277614;
    totalPossibleMoves[3][8][8] = 423584;
    totalPossibleMoves[4][1][5] = 8;
    totalPossibleMoves[4][1][6] = 40;
    totalPossibleMoves[4][1][7] = 120;
    totalPossibleMoves[4][1][8] = 280;
    totalPossibleMoves[4][2][3] = 56;
    totalPossibleMoves[4][2][4] = 400;
    totalPossibleMoves[4][2][5] = 1456;
    totalPossibleMoves[4][2][6] = 3840;
    totalPossibleMoves[4][2][7] = 8360;
    totalPossibleMoves[4][2][8] = 16016;
    totalPossibleMoves[4][3][3] = 840;
    totalPossibleMoves[4][3][4] = 4080;
    totalPossibleMoves[4][3][5] = 12584;
    totalPossibleMoves[4][3][6] = 30240;
    totalPossibleMoves[4][3][7] = 62016;
    totalPossibleMoves[4][3][8] = 113960;
    totalPossibleMoves[4][4][4] = 17472;
    totalPossibleMoves[4][4][5] = 50592;
    totalPossibleMoves[4][4][6] = 117040;
    totalPossibleMoves[4][4][7] = 234000;
    totalPossibleMoves[4][4][8] = 422240;
    totalPossibleMoves[4][5][5] = 141680;
    totalPossibleMoves[4][5][6] = 321048;
    totalPossibleMoves[4][5][7] = 632896;
    totalPossibleMoves[4][5][8] = 1130424;
    totalPossibleMoves[4][6][6] = 718080;
    totalPossibleMoves[4][6][7] = 1402960;
    totalPossibleMoves[4][6][8] = 2489520;
    totalPossibleMoves[4][7][7] = 2724120;
    totalPossibleMoves[4][7][8] = 4811976;
    totalPossibleMoves[4][8][8] = 8471680;
    totalPossibleMoves[5][1][6] = 10;
    totalPossibleMoves[5][1][7] = 60;
    totalPossibleMoves[5][1][8] = 210;
    totalPossibleMoves[5][2][3] = 14;
    totalPossibleMoves[5][2][4] = 300;
    totalPossibleMoves[5][2][5] = 1820;
    totalPossibleMoves[5][2][6] = 6720;
    totalPossibleMoves[5][2][7] = 18810;
    totalPossibleMoves[5][2][8] = 44044;
    totalPossibleMoves[5][3][3] = 840;
    totalPossibleMoves[5][3][4] = 7140;
    totalPossibleMoves[5][3][5] = 31460;
    totalPossibleMoves[5][3][6] = 98280;
    totalPossibleMoves[5][3][7] = 248064;
    totalPossibleMoves[5][3][8] = 541310;
    totalPossibleMoves[5][4][4] = 48048;
    totalPossibleMoves[5][4][5] = 189720;
    totalPossibleMoves[5][4][6] = 555940;
    totalPossibleMoves[5][4][7] = 1345500;
    totalPossibleMoves[5][4][8] = 2850120;
    totalPossibleMoves[5][5][5] = 708400;
    totalPossibleMoves[5][5][6] = 2006550;
    totalPossibleMoves[5][5][7] = 4746720;
    totalPossibleMoves[5][5][8] = 9891210;
    totalPossibleMoves[5][6][6] = 5565120;
    totalPossibleMoves[5][6][7] = 12977380;
    totalPossibleMoves[5][6][8] = 26762340;
    totalPossibleMoves[5][7][7] = 29965320;
    totalPossibleMoves[5][7][8] = 61352694;
    totalPossibleMoves[5][8][8] = 124957280;
    totalPossibleMoves[6][1][7] = 12;
    totalPossibleMoves[6][1][8] = 84;
    totalPossibleMoves[6][2][4] = 120;
    totalPossibleMoves[6][2][5] = 1456;
    totalPossibleMoves[6][2][6] = 8064;
    totalPossibleMoves[6][2][7] = 30096;
    totalPossibleMoves[6][2][8] = 88088;
    totalPossibleMoves[6][3][3] = 504;
    totalPossibleMoves[6][3][4] = 8568;
    totalPossibleMoves[6][3][5] = 56628;
    totalPossibleMoves[6][3][6] = 235872;
    totalPossibleMoves[6][3][7] = 744192;
    totalPossibleMoves[6][3][8] = 1948716;
    totalPossibleMoves[6][4][4] = 96096;
    totalPossibleMoves[6][4][5] = 531216;
    totalPossibleMoves[6][4][6] = 2001384;
    totalPossibleMoves[6][4][7] = 5920200;
    totalPossibleMoves[6][4][8] = 14820624;
    totalPossibleMoves[6][5][5] = 2691920;
    totalPossibleMoves[6][5][6] = 9631440;
    totalPossibleMoves[6][5][7] = 27530976;
    totalPossibleMoves[6][5][8] = 67260228;
    totalPossibleMoves[6][6][6] = 33390720;
    totalPossibleMoves[6][6][7] = 93437136;
    totalPossibleMoves[6][6][8] = 224803656;
    totalPossibleMoves[6][7][7] = 257701752;
    totalPossibleMoves[6][7][8] = 613526940;
    totalPossibleMoves[6][8][8] = 1449504448;
    totalPossibleMoves[7][1][8] = 14;
    totalPossibleMoves[7][2][4] = 20;
    totalPossibleMoves[7][2][5] = 728;
    totalPossibleMoves[7][2][6] = 6720;
    totalPossibleMoves[7][2][7] = 35112;
    totalPossibleMoves[7][2][8] = 132132;
    totalPossibleMoves[7][3][3] = 168;
    totalPossibleMoves[7][3][4] = 7140;
    totalPossibleMoves[7][3][5] = 75504;
    totalPossibleMoves[7][3][6] = 432432;
    totalPossibleMoves[7][3][7] = 1736448;
    totalPossibleMoves[7][3][8] = 5521362;
    totalPossibleMoves[7][4][4] = 144144;
    totalPossibleMoves[7][4][5] = 1150968;
    totalPossibleMoves[7][4][6] = 5670588;
    totalPossibleMoves[7][4][7] = 20720700;
    totalPossibleMoves[7][4][8] = 61752600;
    totalPossibleMoves[7][5][5] = 8075760;
    totalPossibleMoves[7][5][6] = 36920520;
    totalPossibleMoves[7][5][7] = 128477888;
    totalPossibleMoves[7][5][8] = 369931254;
    totalPossibleMoves[7][6][6] = 161388480;
    totalPossibleMoves[7][6][7] = 545049960;
    totalPossibleMoves[7][6][8] = 1536158316;
    totalPossibleMoves[7][7][7] = 1803912264;
    totalPossibleMoves[7][7][8] = 5010470010;
    totalPossibleMoves[7][8][8] = 13770292256;
    totalPossibleMoves[8][2][5] = 208;
    totalPossibleMoves[8][2][6] = 3840;
    totalPossibleMoves[8][2][7] = 30096;
    totalPossibleMoves[8][2][8] = 151008;
    totalPossibleMoves[8][3][3] = 24;
    totalPossibleMoves[8][3][4] = 4080;
    totalPossibleMoves[8][3][5] = 75504;
    totalPossibleMoves[8][3][6] = 617760;
    totalPossibleMoves[8][3][7] = 3224832;
    totalPossibleMoves[8][3][8] = 12620256;
    totalPossibleMoves[8][4][4] = 164736;
    totalPossibleMoves[8][4][5] = 1973088;
    totalPossibleMoves[8][4][6] = 12961344;
    totalPossibleMoves[8][4][7] = 59202000;
    totalPossibleMoves[8][4][8] = 211723200;
    totalPossibleMoves[8][5][5] = 19612560;
    totalPossibleMoves[8][5][6] = 116035920;
    totalPossibleMoves[8][5][7] = 495557568;
    totalPossibleMoves[8][5][8] = 1691114304;
    totalPossibleMoves[8][6][6] = 645553920;
    totalPossibleMoves[8][6][7] = 2647385520;
    totalPossibleMoves[8][6][8] = 8778047520;
    totalPossibleMoves[8][7][7] = 10565771832;
    totalPossibleMoves[8][7][8] = 34357508640;
    totalPossibleMoves[8][8][8] = 110162338048;

    int n, m;
    cin >> n >> m;

    vector<vector<int>> initialBoard(n, vector<int>(m, 0));
    vector<vector<int>> wantedBoard(n, vector<int>(m, 0));
    unordered_set<vector<int>, VectorHash> pawnPositions;
    unordered_set<vector<int>, VectorHash> pawnPositionsInWanted;

    populate_board(initialBoard, pawnPositions);
    populate_board(wantedBoard, pawnPositionsInWanted);

    if (is_transition_possible(initialBoard, wantedBoard) == false) {
        cout << "0" << endl;
        return 0;
    }
    unsigned long long possible_moves_count = get_possible_moves_number(n, m, pawnPositionsInWanted);
    unsigned long long possible_moves_total = get_total_possible_moves(n, m, pawnPositions.size(), totalPossibleMoves);

    cout << fixed;
    cout.precision(15);
    double probability = (double)possible_moves_count / possible_moves_total;
    cout << probability << endl;

    return 0;
}