#include using namespace std; void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); } int main() { fast_io(); int h, w; cin >> h >> w; vector s(h), t(h); for (int i = 0; i < h; i++) { cin >> s[i]; } for (int i = 0; i < h; i++) { cin >> t[i]; } vector s_rot(h, string(w, '.')); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { s_rot[h - 1 - i][w - 1 - j] = s[i][j]; } } bool ok[2] = {true, true}; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (s[i][j] == t[i][j]) { ok[0] = false; } if (s_rot[i][j] == t[i][j]) { ok[1] = false; } } } if (!ok[0] && !ok[1]) { cout << -1 << endl; return 0; } long double ans = 0; long double prob = 1.0; int M = 1e6; for (int i = 0; i < M; i++) { if (ok[i % 2]) { long double p = powl(2, -i); ans += (i + 1) * prob * (1.0 - p); prob *= p; } } cout << fixed << setprecision(16) << ans << endl; }