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
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>

using namespace std;

typedef unsigned long long ull;


bool parity(vector<pair<int, int>> &tab) {
    int sum = 0;
    for (auto coords: tab) {
        sum += coords.first + coords.second;
    }
    return sum % 2;
}


bool reachable(vector<pair<int, int>> &start, vector<pair<int, int>> &end) {
    if (start.size() != end.size()) {
        return false;
    }
    
    return parity(start) == parity(end);
}

bool isempty(vector<pair<int, int>> &tab, int i, int j) {
    for (auto coord: tab) {
        if (coord.first == i && coord.second == j) {
            return false;
        }
    }
    return true;
}

bool is_in_bounds(int n, int m, int i, int j) {
    return i >= 0 && i < n && j>=0 && j < m;
}

bool valid_one_move(vector<pair<int, int>> &tab, int n, int m, int i, int j) {
    return is_in_bounds(n, m, i, j) && isempty(tab, i, j);
}

int count_one_moves(vector<pair<int, int>> &tab, int n, int m, int i, int j) {
    int count = 0;
    count += valid_one_move(tab, n, m, i+1, j);
    count += valid_one_move(tab, n, m, i-1, j);
    count += valid_one_move(tab, n, m, i, j+1);
    count += valid_one_move(tab, n, m, i, j-1);
    return count;
}

int count_one_pawn_two_moves(vector<pair<int, int>> &tab, int n, int m, pair<int, int> coord) {
    ull moves = 0;
    vector<pair<int, int>> tab_wo_coord = tab;
    tab_wo_coord.erase(remove(tab_wo_coord.begin(), tab_wo_coord.end(), coord), tab_wo_coord.end());
    if (valid_one_move(tab, n, m, coord.first, coord.second+1)) {
        moves += count_one_moves(tab_wo_coord, n, m, coord.first, coord.second+1);
    }
    if (valid_one_move(tab, n, m, coord.first, coord.second-1)) {
        moves += count_one_moves(tab_wo_coord, n, m, coord.first, coord.second-1);
    }
    if (valid_one_move(tab, n, m, coord.first+1, coord.second)) {
        moves += count_one_moves(tab_wo_coord, n, m, coord.first+1, coord.second);
    }
    if (valid_one_move(tab, n, m, coord.first-1, coord.second)) {
        moves += count_one_moves(tab_wo_coord, n, m, coord.first-1, coord.second);
    }
    return moves;
}

ull position_moves(vector<pair<int, int>> &tab, int n, int m) {
    ull moves = 0;
    // one pawn moves twice
    for (auto coord: tab) {
        moves += count_one_moves(tab, n, m, coord.first, coord.second);
    }
    
    return moves;

    // two pawns move once
    for (int i=0; i < tab.size(); i++) {
        //cout << endl << i << endl;
        for (int j=i+1; j < tab.size(); j++) {
            int first_moves = count_one_moves(tab, n, m, tab[i].first, tab[i].second);
            int second_moves = count_one_moves(tab, n, m, tab[j].first, tab[j].second);
            moves += first_moves * second_moves;
                
            // correction for touching pawns
            if (tab[i].first == tab[j].first && abs(tab[i].second-tab[j].second) == 1 ||
                tab[i].second == tab[j].second && abs(tab[i].first-tab[j].first) == 1) {
                moves += first_moves + second_moves;
            }
            
            // correction for distance-2 in-line pawns with empty space in between
            if (tab[i].first == tab[j].first && abs(tab[i].second-tab[j].second) == 2 &&
                isempty(tab, tab[i].first, min(tab[i].second,tab[j].second)+1)) {
                    
                moves--;
            }
            if (tab[i].second == tab[j].second && abs(tab[i].first-tab[j].first) == 2 &&
                isempty(tab, min(tab[i].first,tab[j].first)+1, tab[i].second)) {
                    
                moves--;
            }
            
            // correction for diagonally touching pawns
            if (min(tab[i].first, tab[j].first)+1 == max(tab[i].first, tab[j].first) &&
                min(tab[i].second, tab[j].second)+1 == max(tab[i].second, tab[j].second)) {
                pair<int, int> top_left = make_pair(min(tab[i].first, tab[j].first), min(tab[i].second, tab[j].second));
                
                int pawns_in_square = 0;
                pawns_in_square += !isempty(tab, top_left.first, top_left.second);
                pawns_in_square += !isempty(tab, top_left.first+1, top_left.second);
                pawns_in_square += !isempty(tab, top_left.first, top_left.second+1);
                pawns_in_square += !isempty(tab, top_left.first+1, top_left.second+1);
                
                moves -= 4 - pawns_in_square;
            }
            
            //cout << moves << endl;
        }
    }
    return moves;
}

ull choose(int up, int down) {
    //cout << up << " " << down << " - ";
    if (up < down || down < 0 || up < 0) {
        //cout << 0 <<endl;
        return 0;
    }
    if (down == 0 || up == down) {
        //cout << 1 <<endl;
        return 1;
    }
    
    if (down > up/2) {
        down = up - down;
    }
    ull res = 1;
    for (int i=0; i<down; i++) {
        res *= (up-i);
    }
    for (int i=2; i<=down; i++) {
        res /= i;
    }
    //cout << res <<endl;
    return res;
}

ull select(int x, int even, int odd, bool req_parity) {
    if (x == 0 && req_parity) {
        return 0;
    }
    
    int res = 0;
    for (int i=req_parity; i<=x; i+=2) {
        res += choose(odd, i) * choose(even, x-i);
    }
    return res;
}


// ---- COUNTING ALL MOVES ---- //


int possible_dominos(int n, int m, vector<pair<int, int>> excluded) {
    int count = 0;
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            if (!isempty(excluded, i, j)) {
                continue;
            }
            
            count += valid_one_move(excluded, n, m, i+1, j);
            count += valid_one_move(excluded, n, m, i, j+1);
        }
    }
    return count;
}

ull all_moves(int n, int m, int pawns, bool parity) {
    vector<pair<int, int>> empty_board;
    int odd = (n*m)/2;
    int even = n*m-odd;
    
    ull moves = 0;;
    
    // ---- NEW APPROACH
    ull odd_pos_count = 0;
    ull even_pos_count = 0;
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            int cnt = count_one_moves(empty_board, n, m, i, j);
            if ((i+j) % 2 == 0) {
                even_pos_count += cnt;
            } else {
                odd_pos_count += cnt;
            }
        }
    }
    moves += select(pawns-1, even-1, odd-1, parity) * even_pos_count;
    moves += select(pawns-1, even-1, odd-1, !parity) * odd_pos_count;
    
    // ----
    return moves;
    
    // back and forth move
    odd_pos_count = 0;
    even_pos_count = 0;
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            int cnt = count_one_moves(empty_board, n, m, i, j);
            if ((i+j) % 2 == 0) {
                even_pos_count += cnt;
            } else {
                odd_pos_count += cnt;
            }
        }
    }
    moves += select(pawns-1, even-1, odd-1, parity) * even_pos_count;
    moves += select(pawns-1, even-1, odd-1, !parity) * odd_pos_count;
    
    // move away with single pawn
    odd_pos_count = 0;
    even_pos_count = 0;
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            int two = count_one_pawn_two_moves(empty_board, n, m, make_pair(i,j));
            int one = count_one_moves(empty_board, n, m, i, j);
            int cnt = count_one_pawn_two_moves(empty_board, n, m, make_pair(i,j))
                - count_one_moves(empty_board, n, m, i, j);
            if ((i+j) % 2 == 0) {
                even_pos_count += cnt;
            } else {
                odd_pos_count += cnt;
            }
        }
    }
    moves += select(pawns-1, even-2, odd-1, parity) * even_pos_count;
    moves += select(pawns-1, even-1, odd-2, !parity) * odd_pos_count;
    
    
    if (pawns == 1) {
        return moves;
    }

    // move two pawns without interfering
    ull count = 0;
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            vector<pair<int, int>> excluded;
            excluded.push_back(make_pair(i, j));
            
            if (is_in_bounds(n, m, i+1, j)) {
                excluded.push_back(make_pair(i+1,j));
                count += possible_dominos(n, m, excluded);
                excluded.pop_back();
            }
            if (is_in_bounds(n, m, i-1, j)) {
                excluded.push_back(make_pair(i-1,j));
                count += possible_dominos(n, m, excluded);
                excluded.pop_back();
            }
            if (is_in_bounds(n, m, i, j+1)) {
                excluded.push_back(make_pair(i,j+1));
                count += possible_dominos(n, m, excluded);
                excluded.pop_back();
            }
            if (is_in_bounds(n, m, i, j-1)) {
                excluded.push_back(make_pair(i,j-1));
                count += possible_dominos(n, m, excluded);
                excluded.pop_back();
            }
        }
    }
    moves += select(pawns-2, even-2, odd-2, parity) * count/2; // second pawn can be on either
    moves += select(pawns-2, even-2, odd-2, !parity) * count/2; // parity of the domino
    
    // second slides in place of the first
    odd_pos_count = 0;
    even_pos_count = 0;
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            int free_neigh = count_one_moves(empty_board, n, m, i, j);
            int cnt = free_neigh * (free_neigh-1);
            if ((i+j) % 2 == 0) {
                even_pos_count += cnt;
            } else {
                odd_pos_count += cnt;
            }
        }
    }
    moves += select(pawns-2, even-1, odd-2, !parity) * even_pos_count;
    moves += select(pawns-2, even-2, odd-1, !parity) * odd_pos_count;
    
    return moves;
}


vector<pair<int, int>> load_tab(int n, int m) {
    vector<pair<int, int>> tab;
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            char val;
            cin >> val;
            if (val == 'O') {
                tab.push_back(make_pair(i,j));
            }
        }
    }
    return tab;
}


int main() {
    int n, m;
    cin >> n >> m;
    
    vector<pair<int, int>> start_tab = load_tab(n, m);
    vector<pair<int, int>> end_tab = load_tab(n, m);
    
    if (!reachable(start_tab, end_tab)) {
        cout << 0;
        return 0;
    }
    
    // cout << choose(8, 5) << endl;
    // cout << select(2, 3, 2, true) << endl;
    
    // vector<pair<int, int>> excl;
    // excl.push_back(make_pair(1,1));
    // excl.push_back(make_pair(2,2));
    // excl.push_back(make_pair(1,2));
    // excl.push_back(make_pair(2,1));
    // cout << possible_dominos(100, 100, excl) << endl << endl;
    
    std::cout << std::fixed;
    std::cout << std::setprecision(15);
    long double pos_mvs = position_moves(end_tab, n, m);
    long double all_mvs = all_moves(n, m, end_tab.size(), parity(end_tab));
    // cout << parity(end_tab) << endl;
    // cout << (ull) pos_mvs << endl;
    // cout << (ull) all_mvs << endl;
    pos_mvs /= all_mvs;
    cout << pos_mvs << endl;
    return 0;
}