#include using namespace std; using ll = long long; #define ALL(v) v.begin(),v.end() #define dbg(x) cerr << #x << ": " << (x) << endl; template ostream& operator<<(ostream& os, pair& p) { os << '(' << p.first << ',' << p.second << ')'; return os; } template void print(Iter beg, Iter end) { for (Iter itr = beg; itr != end; ++itr) { cerr << *itr << ' '; } cerr << '\n'; } // 転置 // pos 対象の座標 // h 転置前のy座標範囲[0, h) // w 転置前のx座標範囲[0, w) vector> transpose(const vector>& pos, int h, int w) { vector> res; for (const auto& [x,y] : pos) { int nx = y; int ny = x; res.emplace_back(nx, ny); } return res; } // y座標の反転 // h 反転前のy座標範囲[0, h) // w 反転前のx座標範囲[0, w) vector> flip_y(const vector>& pos, int h, int w) { vector> res; for (const auto& [x,y] : pos) { res.emplace_back(x, h-y-1); } return res; } // x座標の反転 // h 反転前のy座標範囲[0, h) // w 反転前のx座標範囲[0, w) vector> flip_x(const vector>& pos, int h, int w) { vector> res; for (const auto& [x,y] : pos) { res.emplace_back(w-x-1, y); } return res; } // 回転 // h 回転前のy座標範囲[0, h) // w 回転前のx座標範囲[0, w) // 必要:transpose, flip_xかflip_y vector> rotate(const vector>& pos, int h, int w) { auto res = transpose(pos, h, w); res = flip_x(res, w, h); // 右回転 // res = flip_y(res, w, h); // 左回転 return res; } int h,w; vector s,t; int main() { cin >> h >> w; s.resize(h); t.resize(h); for (int i = 0; i < h; ++i) cin >> s[i]; for (int i = 0; i < h; ++i) cin >> t[i]; vector> ps, pt; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (s[i][j] == '#') { ps.emplace_back(j, i); } if (t[i][j] == '.') { pt.emplace_back(j, i); } } } sort(ALL(ps)); sort(ALL(pt)); double ans = 0; if (ps == pt) { ans = 3.5317401904617327; } else { pt = rotate(pt, h, w); pt = rotate(pt, w, h); sort(ALL(pt)); if (ps == pt) { double base = 1; for (int t = 2; t < 30; t+=2) { double p = 1.0 / (1LL << (t-1)); ans += t * base * (1 - p); base *= p; } } else { cout << -1 << '\n'; return 0; } } cout << fixed << setprecision(20) << ans << '\n'; }