#include using namespace std; #ifdef LOCAL #include "settings/debug.cpp" #else #define Debug(...) void(0) #endif #define rep(i, n) for (int i = 0; i < (n); ++i) using ll = long long; using ull = unsigned long long; int main() { int h, w; cin >> h >> w; vector Grid1(h, vector(w)), Grid2(h, vector(w)); rep(i, h) rep(j, w) cin >> Grid1[i][j]; rep(i, h) rep(j, w) cin >> Grid2[i][j]; rep(i, h) rep(j, w) Grid2[i][j] = Grid2[i][j] ^ '.' ^ '#'; bool rotateOk = true; rep(i, h) rep(j, w) if (Grid1[i][j] != Grid2[h - i - 1][w - j - 1]) rotateOk = false; // f(x) = x 回目に成功する確率 function f; if (rotateOk) { if (Grid1 == Grid2) { f = [&](int x) -> double { double res = 1; rep(i, x - 1) res *= pow(2, -i); res *= 1 - pow(2, -(x - 1)); return res; }; } else { f = [&](int x) -> double { double res = 1; // 奇数回目はミス rep(i, x - 1) { if (i % 2 == 0) res *= 1; else res *= pow(2, -i); } if (x % 2 == 0) res *= 1 - pow(2, -(x - 1)); else res *= 0; return res; }; } } else { if (Grid1 == Grid2) { f = [&](int x) -> double { double res = 1; // 偶数回目はミス rep(i, x - 1) { if (i % 2 == 1) res *= 1; else res *= pow(2, -i); } if (x % 2 == 1) res *= 1 - pow(2, -(x - 1)); else res *= 0; return res; }; } else { // 全ミス cout << -1 << endl; return 0; } } double ans = 0; for (int x = 1; x <= 100; ++x) { Debug(x, f(x)); ans += x * f(x); } cout << fixed << setprecision(15) << ans << endl; return 0; }