#include using namespace std; using ll = long long; const int INF = 1e9 + 10; const ll INFL = 4e18; int main() { 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]; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { T[i][j] ^= int('#') ^ int('.'); } } auto U = T; for (int i = 0; i < H; i++) ranges::reverse(U[i]); ranges::reverse(U); if (S != U && S != T) { cout << -1 << endl; } else if (S == T && S == U) { cout << "1.6183374588308512" << endl; } else if (S == T) { cout << "3.5317401904617327" << endl; } else { cout << "3.128936827211877" << endl; } }