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

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int H,W;
    cin>>H>>W;
    vector<string>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(char &c:T[i])c=(c=='.'?'#':'.');
    }
    auto R=S;
    reverse(R.begin(),R.end());
    for(auto &r:R)reverse(r.begin(),r.end());
    if(S==T&&R==T)
    {
        cout<<"2.6416325606551538"<<endl;
    }
    else if(S==T)
    {
        cout<<"3.5317401904617327"<<endl;
    }
    else if(R==T)
    {
        cout<<"3.128936827211877"<<endl;
    }
    else
    {
        cout<<"-1"<<endl;
    }
}