#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int H,W; cin >> H >> W;
    vector<string> S(H),T(H);
    for(auto &s : S) cin >> s;
    for(auto &t : T) cin >> t;
    for(auto &s : S) for(auto &c : s) c ^= '.'^'#';

    vector<string> S2(H,string(W,'.'));
    for(int i=0; i<H; i++) for(int k=0; k<W; k++) S2.at(H-1-i).at(W-1-k) = S.at(i).at(k);
    if(S != T){
            swap(S,S2);
        if(S == T){
            double answer = 0,all = 1;
            for(int i=1; i<=100; i+=2){
                double now = 1.0-pow(0.5,i);
                answer += all*now*(i+1);
                all -= all*now;
            }
            cout << fixed << setprecision(20) << answer << endl;
        }
        else cout << "-1" << endl;
    }
    else if(S2 != T) cout << fixed << setprecision(20) << 3.5317401904617327 << endl;
    else{
        double answer = 0,all = 1;
        for(int i=0; i<=100; i++){
            double now = 1.0-pow(0.5,i);
            answer += all*now*(i+1);
            all -= all*now;
        }
        cout << fixed << setprecision(20) << answer << endl;
    }
}