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
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <unordered_map>
#include <random>



using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define MP make_pair
#define FOR(v,p,k) for(int v=(p);v<=(k);++v)
#define FORD(v,p,k) for(int v=(p);v>=(k);--v)
#define REP(i,n) for(int i=0;i<(n);++i)
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define PB push_back
#define ST first
#define ND second
#define SIZE(x) (int)x.size()
#define ALL(c) c.begin(),c.end()

#define ODD(x) ((x)%2)
#define EVEN(x) (!(ODD(x)))

typedef double RationalNum;

void print_as_double(RationalNum y){
    printf("%.15f\n", y);
}

bool is_small(RationalNum y) {
    return fabs(y) <= 1e-14;
}


const unordered_map<uint64_t, RationalNum>& search_fill_graph(
        uint64_t h, int n, int m, 
        vector<vector<uint64_t>> &field_hashes,
        unordered_map<uint64_t, unordered_map<uint64_t, RationalNum>> &graph,
        unordered_map<uint64_t, vector<pair<int,int>>> &pos_points) {
    if (auto search = graph.find(h); search != graph.end()) {
        return search->ND;
    }
    const VI ch_x = {1, -1, 0, 0};
    const VI ch_y = {0, 0, 1, -1};


    //if (debug) fprintf(stderr, "sfg begin =================\n");
    //if (debug) fprintf(stderr, "h= %llu\n", h);
    unordered_map<uint64_t, RationalNum> res;
    vector<pair<int,int>>  points = pos_points[h];
    LL gens = 0;
    int point_index = 0;
    for(auto &p: points) {

        //if (debug) fprintf(stderr, "p= %d %d\n", p.ST, p.ND);
        REP(i, 4) {
            h ^= field_hashes[p.ST][p.ND];
            p.ST += ch_x[i];
            p.ND += ch_y[i];

            //if (debug) fprintf(stderr, "new p= %d %d\n", p.ST, p.ND);
            bool collision = false;
            if (p.ST < 0 || p.ST >= n || p.ND < 0 || p.ND >= m) collision=true;
            if (!collision) {
                int collision_point_index = 0;
                for (auto &ps: points) {
                    if (ps == p && point_index != collision_point_index) {
                        collision = true;
                        break;
                    }
                    collision_point_index++;
                }
            }
            //if (debug) fprintf(stderr, "collision %d\n", int(collision));
            if (!collision) {
                gens++;
                h ^= field_hashes[p.ST][p.ND];

                //if (debug) fprintf(stderr, "new h= %llu\n", h); 
                res[h] += RationalNum(1ll);
                if (!pos_points.count(h)) {
                    pos_points[h] = points;
                }
                
                h ^= field_hashes[p.ST][p.ND];
            }
            p.ST -= ch_x[i];
            p.ND -= ch_y[i];
 
            h ^= field_hashes[p.ST][p.ND];
        }
        point_index++;
    }

    RationalNum total(gens);
    for(auto &hash_and_prob: res) {
        hash_and_prob.ND /= total;
    }

    //if (debug) fprintf(stderr, "h= %llu\n", h);
    //if (debug) fprintf(stderr, "sfg end =================\n");

    graph[h] = res;
    return graph[h];
 
}

 
int main() {

    int n, m;
    scanf("%d %d\n", &n, &m);


    vector<vector<uint64_t>> field_hashes = {
    {5773073707084609340ll, 4414797619550917690ll, 7029116334884307215ll, 2272573035594218560ll, 7737537497300585569ll, 8851459399564084970ll, 7023478020632669612ll, 5197141776955506059ll },
        {1846306476417855784ll, 2378349255950422846ll, 4010369168342226758ll, 8996630378481339194ll, 3090749077044287995ll, 7445914859190926940ll, 7297243582016048433ll, 8244511251311220399ll },
        {7263513506677700098ll, 1580356813820309460ll, 7915482843742429340ll, 3250471041660974729ll, 3954514158722134669ll, 3397916877565291286ll, 3575085680492795181ll, 568962952263258660ll },
        {7202491705245883466ll, 5494817041793792631ll, 4343878510367631836ll, 6389598221965620573ll, 3749661214289344480ll, 1067092379150204421ll, 8913871321130503974ll, 7507213038365090731ll },
        {6969828823830325950ll, 913209758305273032ll, 2589247855898410058ll, 5588131187291756040ll, 5655529217125222732ll, 6489653552357048733ll, 4592868895127953592ll, 4465014278867749779ll },
        {8193565522579444089ll, 6212703536422865659ll, 4210045958285617033ll, 4082578397869590480ll, 2559934998013992683ll, 7507078058352530102ll, 2895703655546086834ll, 2219939338572545112ll },
        {6637028901219955367ll, 4601407475853433148ll, 4552506464795500804ll, 8910713177145926316ll, 6777951972905858245ll, 264233360553798177ll, 2984097623433367018ll, 3886581281567091630ll },
        {4429570087803750824ll, 4832320619223628929ll, 3260021923986355797ll, 6575817937558709663ll, 7475667668203185296ll, 98098215791341596ll, 8721385709250267831ll, 6097701995626713321ll }};
    
    unordered_map<uint64_t, vector<pair<int,int>>> pos_points;

    unordered_map<uint64_t, unordered_map<uint64_t, RationalNum>> graph;

    unordered_map<uint64_t, RationalNum> dist;
    unordered_map<uint64_t, RationalNum> dist_new;


    vector<pair<int,int>> t_pos;
    vector<pair<int,int>> u_pos;
    uint64_t t_hash = 0;
    uint64_t u_hash = 0;

    char b[109];
    REP(i, n) {
        scanf("%s\n", b);
        REP(j, m) {
            if (b[j] == 'O') {
                t_pos.PB(MP(i, j));
                t_hash ^= field_hashes[i][j];

            }
        }
    }
    scanf("\n");
    REP(i, n) {
        scanf("%s\n", b);
        REP(j, m) {
            if (b[j] == 'O') {
                u_pos.PB(MP(i, j));
                u_hash ^= field_hashes[i][j];
            }
        }
    }
    //if (debug) fprintf(stderr, "t_hash: %llu\n",  t_hash);
    //if (debug) fprintf(stderr, "u_hash: %llu\n",  u_hash);

    pos_points[t_hash] = t_pos;
    dist[t_hash] = RationalNum(1ll);


    vector<RationalNum> probs_of_target;
    REP(i, 100000000) {
        dist_new.clear();
        for (auto& it: dist) {
            auto h = it.ST;
            auto& prob = it.ND;
 
            for(auto &neigh_and_prob: search_fill_graph(h, n, m, field_hashes, graph, pos_points)) {
                dist_new[neigh_and_prob.ST] += prob * neigh_and_prob.ND;
            }

            //debug_dist(dist_new, h);
        }

        if (auto search = dist_new.find(u_hash); search != dist_new.end()) {
            probs_of_target.PB(search->ND);
        } else {
            probs_of_target.PB(RationalNum(0ll));
        }
        swap(dist, dist_new);
        if (i%2 == 1 && probs_of_target.size() >= 10 && is_small(probs_of_target[probs_of_target.size()-3] - probs_of_target[probs_of_target.size()-1])) {
            break;
        }
    }


    print_as_double(probs_of_target[probs_of_target.size()-1]);

    return 0;
}