#include using namespace std; using ld = long double; int main() { int h, w; cin >> h >> w; vector> s(h, vector(w)), t(h, vector(w)); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> s[i][j]; } } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> t[i][j]; } } bool odd = true, even = true; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (s[i][j] == t[i][j]) { odd = false; } if (s[h - 1 - i][w - 1 - j] == t[i][j]) { even = false; } } } ld ans = 1.0; vector p(25), mul(25); p[1] = mul[1] = 1.0; if (odd && even) { for (int i = 2; i < 25; i++) { ans += mul[i - 1]; p[i] = p[i - 1] / 2.0; mul[i] = mul[i - 1] * p[i]; } } else if (odd && !even) { for (int i = 2; i < 25; i++) { ans += mul[i - 1]; p[i] = (i % 2 == 0 ? 1.0 : p[i - 2] / 4.0); mul[i] = mul[i - 1] * p[i]; } } else if (!odd && even) { for (int i = 2; i < 25; i++) { if (i == 2) { ans += 1.0; p[2] = 0.5; mul[2] = 0.5; } else { ans += mul[i - 1]; p[i] = (i % 2 == 0 ? p[i - 2] / 4.0 : 1.0); mul[i] = mul[i - 1] * p[i]; } } } else { cout << -1 << endl; return 0; } cout << fixed << setprecision(10) << ans << endl; }