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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
using namespace std;
const int N = 8;
unsigned long long n, m;
char c[N][N];
// TODO unordered?
map<pair<unsigned long long, unsigned long long>, pair<unsigned long long, unsigned long long> > p;
set<pair<unsigned long long, unsigned long long>> konce;
queue<unsigned long long>q;
set<unsigned long long> nowe;
unsigned long long pot[N * N];
int ii[4] = {0, 0, 1, -1};
int jj[4] = {1, -1, 0, 0};
const double ULP = 14;

unsigned long long nwd(unsigned long long a, unsigned long long b) {
  unsigned long long c;
  while(b != 0) {
    c = b;
    b = a % b;
    a = c;
  }
  return a;
}

unsigned long long h() {
  unsigned long long res = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (c[i][j] == 'O') {
        res += pot[i * m + j];
      }
    }
  }
  return res;
}

bool pionek(unsigned long long n, int i, int j) {
  return (n / pot[i * m + j]) % 2;
}

unsigned long long przesuniety(unsigned long long n, int i, int j, int ni, int nj) {
  n += pot[ni * m + nj];
  n -= pot[i * m + j];
  return n;
}

vector<unsigned long long> ruchy(unsigned long long x) {
  unordered_set<unsigned long long> tmp;
  int ni, nj;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (pionek(x, i, j)) {
        for (int r = 0; r < 4; r++) {
          ni = i + ii[r];
          nj = j + jj[r];
          if (0 <= ni && ni < n && 0 <= nj && nj < m &&
              !pionek(x, ni, nj)) {
            tmp.insert(przesuniety(x, i, j, ni, nj));
          }
        }
      }
    }
  }

  vector<unsigned long long> res;
  for (unordered_set<unsigned long long>::iterator it = tmp.begin(); it != tmp.end(); it++) {
    res.push_back(*it);
  }
  return res;
}

pair<unsigned long long, unsigned long long> dziel(pair<unsigned long long, unsigned long long> u, unsigned long long d) {
  unsigned long long dz = nwd(u.fi, d);
  u.fi /= dz;
  d /= dz;

  return mp(u.fi, u.se * d);
}

pair<unsigned long long, unsigned long long> dodaj(
    pair<unsigned long long, unsigned long long> a, pair<unsigned long long, unsigned long long> b) {
  if (a.se == b.se) {
    unsigned long long dz = nwd(a.fi + b.fi, b.se);
    return {(a.fi + b.fi) / dz, b.se / dz};
  }

  unsigned long long nww = (a.se / nwd(a.se, b.se)) * b.se;
  unsigned long long licz = (nww / a.se * a.fi) + (nww / b.se * b.fi);
  licz /= nwd(licz, nww);

  return {licz, nww};
}

bool za_maly(pair<unsigned long long, unsigned long long> u) {
  if (u.fi == 0) {
    return true;
  }

  double d = u.fi;
  d /= (double)u.se;
  return (d < 1e-14);
}

bool double_eq(double a, double b) {
  return fabs(a - b) < numeric_limits<double>::epsilon() * fabs(a + b) * ULP
    || fabs(a - b) < numeric_limits<double>::min();
}

double liczba(pair<unsigned long long, unsigned long long> u) {
  double res = u.fi;
  res /= double(u.se);
  return res;
}

int main()
{
  ios_base::sync_with_stdio(0);

  cin >> n >> m;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> c[i][j];
    }
  }

  pot[0] = 1;
  for (int i = 1; i < n * m; i++) {
    pot[i] = pot[i - 1] * 2;
  }

  unsigned long long pocz = h();
  p[{pocz, 0}] = mp(1, 1);

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> c[i][j];
    }
  }

  unsigned long long kon = h();
  unsigned long long k = 0;
  // cout << "kon " << kon << endl;

  if (pocz == kon) {
    // cout << 1 << endl;
    return 0;
  }

  q.push(pocz);

  double poprz_res = 0;

  for (unsigned long long k = 0; !q.empty(); k++) {
    // cout << endl;
    // cout << endl;
    // cout << "w " << k << " ruchow: " << endl;

    if (p.find({kon, k}) != p.end()) {
      // cout << "k = " << k << " z prawd " << p[{kon, k}].fi << " / " << p[{kon, k}].se << " = ";
      double r = double(p[{kon, k}].fi) / double(p[{kon, k}].se);
      // cout << fixed << r << endl;
    }

    if (p.find({kon, k}) != p.end()) {
      // cout << "   poprz res " << poprz_res << endl;
      double res = liczba(p[{kon, k}]);
      // cout << "   res " << res << " dla k " << k << endl;
      if (k > 0 && double_eq(res, poprz_res)) {
        if (k % 2 == 0) {
          cout << fixed << std::setprecision(ULP) << res << endl;
        } else {
          cout << 0 << endl;
        }
        return 0;
      }
      poprz_res = res;
    }

    while (!q.empty()) {
      // cout << q.size() << endl;
      unsigned long long a = q.front();
      q.pop();

      vector<unsigned long long> v = ruchy(a);
      unsigned long long d = v.size();
      // cout << "dla " << a << " kolejnych ruchow jest " << v.size() << endl;

      pair<unsigned long long, unsigned long long> nowy_wynik = dziel(p[{a, k}], d);
      // cout << " dziel " << liczba(p[{a, k}]) <<
      //   "( " << p[{a, k}].fi << " / " << p[{a, k}].se << " ) na " << d << " = "
      //   << liczba(nowy_wynik) << "( " << nowy_wynik.fi << " / " << nowy_wynik.se << " )" << endl;
      bool dalej = !za_maly(nowy_wynik);
      // cout << "a " << a << endl;

      while (!v.empty()) {
        if (dalej) {
          pair<unsigned long long, unsigned long long> ind = mp(v.back(), k + 1);
          if (p.find(ind) == p.end()) {
            p[ind] = nowy_wynik;
          } else {
            // cout << "  dodaj " << liczba(p[ind])
              // << " + " << liczba(nowy_wynik);
              p[ind] = dodaj(p[ind], nowy_wynik);
            // cout << " = " << liczba(p[ind]) << endl;
          }

          // cout << "   " << a << " => " << v.back()
          //   << " prawd " << fixed << std::setprecision(20) << liczba(p[ind]) << endl;
          nowe.insert(v.back());
          // cout << "dalej idziemy w " << v.back() << endl;
        }
        v.pop_back();
      }
    }

    for (set<unsigned long long>::iterator it = nowe.begin(); it != nowe.end(); it++) {
      q.push(*it);
    }
    nowe.clear();
  }

  cout << fixed << std::setprecision(ULP) << poprz_res << endl;

  return 0;
}