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

using namespace std;

const int SIZE = 8;
int n, m;

int tab[SIZE + 2][SIZE + 2];
int input[SIZE + 2][SIZE + 2];
int output[SIZE + 2][SIZE + 2];

bool inside(int x, int y) {
    return x > 0 and y > 0  and x <= n and y <= m;
}

void init() {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (inside(i - 1, j))
                tab[i][j]++;
            if (inside(i + 1, j))
                tab[i][j]++;
            if (inside(i, j - 1))
                tab[i][j]++;
            if (inside(i, j + 1))
                tab[i][j]++;
        }
    }  
}

int main() {
    cin >> n >> m;
    init();
    string k;
    int niezmiennikinput = 0;
    int niezmiennikoutput = 0;
    for (int i = 1; i <= n; i++) {
        cin >> k;
        for (int j = 1; j <= m; j++) {
            if (k[j - 1] == 'O') {
                input[i][j] = 1;
                niezmiennikinput += i + j;
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        cin >> k;
        for (int j = 1; j <= m; j++) {
            if (k[j - 1] == 'O') {
                output[i][j] = 1;
                niezmiennikoutput += i + j;
            }
        }
    }
    if (niezmiennikinput % 2 != niezmiennikoutput % 2) {
        cout << 0 << "\n";
        return 0;
    }
    int suma = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            suma += tab[i][j];
        }
    }
    long double res = 1;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (output[i][j] == 1)
                res *= ((long double) tab[i][j] * 2 / (long double) suma);
        }
    }

    printf("%.16Lf ", res);
    return 0;
}