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

using namespace std;
using ull = unsigned long long;

const int MAX_N = 8;
const double EPSILON = 1e-15;

int n, m, a;
int p = 0;
vector<ull> states;
ull start_state, finish_state;
vector<vector<ull> > graph;
vector<bool> vis[2];
vector<double> res;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
bool reached = false;

void gen() {
    ull cur = ((ull)1 << p) - 1;
    ull bnd = (a == 64) ? ULLONG_MAX : ((ull)1 << a);

    while (cur < bnd) {
        states.push_back(cur);
        vis[0].push_back(false);
        vis[1].push_back(false);
        res.push_back(0.0);

        ull lowbit = cur & ~(cur - 1);
        ull ones = cur & ~(cur + lowbit);

        if (cur >= ULLONG_MAX - lowbit - (ones / lowbit / 2))
            return;
        else
            cur += lowbit + (ones / lowbit / 2);
    }
}

ull get_state() {
    ull state = 0;

    for (int i = 0; i < n * m; i++) {
        char c;
        cin >> c;

        if (c == 'O') {
            state |= ((ull)1 << i);
            p++;
        }
    }

    return state;
}

void dfs(ull x, ull ed, int parity) {
    vis[parity][x] = true;

    if (x == ed && parity == 1)
        reached = true;

    for (int i = 0; i < graph[x].size(); i++)
        if (!vis[1 - parity][graph[x][i]])
            dfs(graph[x][i], ed, 1 - parity);
}

bool is_done(vector<double>& res1, vector<double>& res2) {
    for (int i = 0; i < res1.size(); i++)
        if (abs(res1[i] - res2[i]) > EPSILON)
            return false;

    return true;
}

void next_step() {
    vector<double> tmp(res.size());

    for (int i = 0; i < graph.size(); i++) {
        tmp[i] = 0.0;

        for (int j = 0; j < graph[i].size(); j++) {
            tmp[i] += res[graph[i][j]] / (double)graph[graph[i][j]].size();
        }
    }

    res = tmp;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> m;
    a = n * m;
    start_state = get_state();
    finish_state = get_state();
    p /= 2;
    gen();

    /*
    cout << "STATES:\n";
    for (int i = 0; i < states.size(); i++) {
        for (int j = 0; j < a; j++) {
            cout << ((states[i] & ((ull)1 << j)) > 0);
        }
        cout << '\n';
    }
    cout << '\n';
    */

    for (int i = 0; i < states.size(); i++) {
        vector<ull> neighbors;
        graph.push_back(neighbors);
    }

    for (int i = 0; i < states.size(); i++) {
        ull s = states[i];

        for (int b = 0; b < a; b++) {
            if (s & ((ull)1 << b)) {
                int x = b / m;
                int y = b % m;

                for (int k = 0; k < 4; k++) {
                    int tx = x + dx[k];
                    int ty = y + dy[k];
                    int tb = tx * m + ty;

                    if (tx >= 0 && tx < n && ty >= 0 && ty < m && (s & ((ull)1 << tb)) == 0) {
                        ull ts = s - ((ull)1 << b) | ((ull)1 << tb);
                        auto it_ts = lower_bound(states.begin(), states.end(), ts);
                        graph[i].push_back(it_ts - states.begin());
                    }
                }
            }
        }
    }

    auto it_bg = lower_bound(states.begin(), states.end(), start_state);
    auto it_ed = lower_bound(states.begin(), states.end(), finish_state);

    ull bg = it_bg - states.begin();
    ull ed = it_ed - states.begin();

    /*
    cout << "GRAPH:\n";
    for (int i = 0; i < graph.size(); i++) {
        for (int j = 0; j < graph[i].size(); j++) {
            cout << graph[i][j] << ' ';
        }
        cout << '\n';
    }
    cout << '\n';
    */

    dfs(bg, ed, 1);

    if (!reached) {
        cout << "0\n";
        return 0;
    }

    res[bg] = 1.0;

    vector<double> tmp;

    do {
        tmp = res;
        next_step();
        next_step();
    } while (!is_done(res, tmp));

    cout << fixed << setprecision(15) << res[ed] << '\n';

    return 0;
}