#include using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair; using tll=tuple; using ld=long double; const ll INF=(1ll<<60); #define rep(i,n) for (ll i=0;i<(ll)(n);i++) #define replr(i,l,r) for (ll i=(ll)(l);i<(ll)(r);i++) #define all(v) v.begin(),v.end() #define len(v) ((ll)v.size()) template inline bool chmin(T &a,T b){ if(a>b){ a=b; return true; } return false; } template inline bool chmax(T &a,T b){ if(a(now-start).count(); return ms; } } mt19937 mt; inline double rand_double(){ //[0.0,1.0] return (double)mt()/(double)mt19937::max(); } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); ll h,w; cin >> h >> w; vector s(h),t(h); rep(i,h) cin >> s[i]; rep(i,h) cin >> t[i]; vector v(2,true); rep(i,h){ rep(j,w){ if(s[i][j]==t[i][j]) v[0]=false; } } reverse(all(s)); rep(i,h) reverse(all(s[i])); rep(i,h){ rep(j,w){ if(s[i][j]==t[i][j]) v[1]=false; } } if(!v[0]&&!v[1]){ cout << "-1\n"; return 0; } ll cnt=0,sum=0; while(timer::get_ms()<1900){ ll now=1; while(true){ if(v[1-(now&1)]&&rand_double()<1-pow(2,-(now-1))) break; now++; } cnt++; sum+=now; } cout << fixed << setprecision(20) << (ld)sum/(ld)cnt << '\n'; }