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
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()

template<typename _T> inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; }
template<typename _T, typename... args> void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); }

#ifdef LOCAL
#define _upgrade ios_base::sync_with_stdio(0);
#define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__)
#else
#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define DBG(...) (__VA_ARGS__)
#define cerr if(0) cout
#endif

// ********************** CODE ********************** //

const int N = 8;

int n, m, p, p2;
vector<vector<bool>> in, out;

bool T[N][N];
pair<int, int> pos[N];

int da[] = {0, 0, -1, 1};
int db[] = {1, -1, 0, 0};

map<int, int> mp;

void calc(int i, int j, int k, int k2)
{
    int ui = i, uj = j + 1;
    if (uj == m)
        ui++, uj = 0;
    if (ui != n)
        calc(ui, uj, k, k2);

    T[i][j] = 1;
    pos[k] = {i, j};
    k++;
    if ((i + j) % 2 == 0)
        k2++;

    if (k == p)
    {
        if (k2 % 2 == p2 % 2)
        {
            int fr = 0;
            for (int z = 0; z < p; z++)
            {
                int a = pos[z].first, b = pos[z].second;
                for (int d = 0; d < 4; d++)
                {
                    int ua = a + da[d], ub = b + db[d];
                    if (0 <= ua && ua < n && 0 <= ub && ub < m && T[ua][ub] == 0)
                    {
                        fr++;
                    }
                }
            }
            // DBG(i, j, k, k2, fr);
            mp[fr]++;
        }
    }
    else if (ui != n)
        calc(ui, uj, k, k2);

    T[i][j] = 0;
}

int main()
{
    _upgrade
    
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        string s;
        cin >> s;
        vector<bool> v;
        for (int j = 0; j < sz(s); j++)
        {
            char c = s[j];
            v.push_back(c == 'O' ? 1 : 0);
            if (c == 'O')
            {
                p++;
                if ((i + j) % 2 == 0)
                    p2++;
            }
        }
        in.push_back(v);
    }

    calc(0, 0, 0, 0);

    LL x = 0;

    for (auto pp: mp)
    {
        // DBG(pp.first, pp.second);
        x += pp.first * pp.second;
    }

    int k2 = 0;
    int k = 0;

    for (int i = 0; i < n; i++)
    {
        string s;
        cin >> s;
        vector<bool> v;
        for (int j = 0; j < sz(s); j++)
        {
            char c = s[j];
            v.push_back(c == 'O' ? 1 : 0);
            if (c == 'O')
            {
                pos[k] = {i, j};
                k++;
                if ((i + j) % 2 == 0)
                    k2++;
            }
        }
        out.push_back(v);
    }

    int fr = 0;
    for (int z = 0; z < p; z++)
    {
        int a = pos[z].first, b = pos[z].second;
        for (int d = 0; d < 4; d++)
        {
            int ua = a + da[d], ub = b + db[d];
            if (0 <= ua && ua < n && 0 <= ub && ub < m && out[ua][ub] == 0)
            {
                fr++;
            }
        }
    }

    // DBG(k, k2, fr);

    LD ans = 0;

    if (k2 % 2 == p2 % 2)
    {
        ans = (long double) fr / x;
        cout << fixed << setprecision(15) << ans << "\n";
        return 0;
    }

    cout << 0 << "\n";

    return 0;
}