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

using namespace std;
typedef long double ld;

char T[10][10];



int main(void){
    int n,m;
    scanf("%d %d",&n,&m);
    char c;
    int p = 0;
    int k = 0;
    int k2 = 0;
    int p2 = 0;
    for(int i = 1; i <= n;i++){
        for(int j = 1; j <= m;j++){
            scanf(" %c",&c);
            if(c == 'O'){
                k++;
                if((i+j)%2 == 0)
                    p++;
            }
        }
    }
    for(int i = 1; i <= n;i++){
        for(int j = 1; j <= m;j++){
            scanf(" %c",&c);
            T[i][j] = c;
            if(c == 'O'){
                k2++;
                if((i+j)%2 == 0)
                    p2++;
            }
        }
    }
    if(k != k2 || (p%2 != p2%2)){
        printf("0");
        return 0;
    }

    int moves = 0;

    for(int i = 1; i <= n;i++){
        for(int j = 1; j <= m;j++){
            if(i != 1 && T[i][j] != T[i-1][j])
                moves++;
            if(j != 1 && T[i][j] != T[i][j-1])
                moves++;
        }
    }
    vector < int > M;
    for(int i = 1; i <= k-1;i++){
        M.push_back(i);
    }
    ld ans = 1;
    for(int i = n*m-2-(k-1)+1; i <= n*m-2;i++){
        int b = i;
        for(int j = 0; j < M.size();j++){
            int p = __gcd(b,M[j]);
            M[j] /= p;
            b /= p;
        }
        ans *= b;
    }
    if(k == 1)
        ans = 1;
    ans *= ((n-1) * m + (m-1) * n);
    //cout << ans << "\n";
    ans = (ld)moves/ans;
    printf("%.16Lf",ans);
    return 0;
}
/*
1 7
..OOOOO
..OOOOO

1 7
OO.....
OO.....
*/