1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// WIE
#ifndef __SIZEOF_INT128__
#define __SIZEOF_INT128__
#endif
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;
template <typename T> using oset =  tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define rep(i, p, k) for(int i(p); i < k; ++i)
#define per(i, p, k) for(int i(p); i > k; --i)
#define sz(x) (int)x.size()
#define sc static_cast
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef __int128_t lll;
//#define int ll
template <typename T = int> using par = std::pair <T, T>;
#define fir first
#define sec second
#define test int _number_of_tests(in()); while(_number_of_tests--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb emplace_back
struct Timer {
    string name{""};
    time_point<high_resolution_clock> end, start{high_resolution_clock::now()};
    duration<float, std::milli> dur;
    Timer() = default;
    Timer(string name): name(name) {}
    ~Timer() {
        end = high_resolution_clock::now(); dur= end - start;
        cout << "@" << name << "> " << dur.count() << " ms" << '\n';
    }
};
template <typename T = int> inline T in()
{
    T x;
    std::cin >> x;
    return x;
}
std::string yn(bool b)
{
    if(b) return "YES\n";
    else return "NO\n";
}
template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par);
template <typename T> std::ostream& operator<< (std::ostream& out, const std::vector <T>& wek)
{
    for(const auto& i : wek)out << i << ' ';
    return out;
}
template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par)
{
    out << '{'<<par.first<<", "<<par.second<<"}";
    return out;
}
#define show(x) cerr << #x << " = " << x << '\n';
ll  dp[9][(1<<8) + 3][9][2];
// dp[a][b][c][d] - suma liczb mozliwych ruchow dla c pionkow w pierwszych a rzedach z ustawieniem b w ostatnim i parzystosci d.
ll moz[9][(1<<8) + 3][9][2];
// moz[a][c][d] - na ile sposobow mozna po pierwszych a liniach rozstawic c pionkow z parzystoscia d.
bool tab[9][9];
bool ta2[9][9];
std::int32_t main()
{
    std::cin.tie(nullptr); std::cout.tie(nullptr); std::ios_base::sync_with_stdio(0);
    cout << setprecision(15) << fixed;
    int n(in()), m(in());
    moz[0][0][0][0] = 1;
    rep(a, 1, n+1)rep(b, 0, 1<<m)rep(c, 0, 9)rep(d, 0, 2){
        int pc(c);
        bool pd(d);
        rep(i, 0, m)if(b&1<<i){
            --pc;
            pd ^= (i + a) % 2;
        }
        if(pc < 0)continue;
        rep(pb, 0, 1<<m){
            moz[a][b][c][d] += moz[a-1][pb][pc][pd];
            dp[a][b][c][d] += dp[a-1][pb][pc][pd];
            ll s(0);
            rep(i, 0, m-1)s += !!(b&1<<i) ^ !!(b&1<<i+1);
            if(a > 1)rep(i, 0, m)  s += !!(b&1<<i) ^ !!(pb&1<<i);
            dp[a][b][c][d] += s * moz[a-1][pb][pc][pd];
        }
    }
    bool d(0);
    int c(0);
    rep(i, 1, n+1)rep(j, 1, m+1){
        tab[i][j] = in<char>() == 'O';
        if((i + j) % 2 == 0)d ^= tab[i][j];
        c += tab[i][j];
    }
    rep(i, 1, n+1)rep(j, 1, m+1){
        ta2[i][j] = in<char>() == 'O';
        if((i + j) % 2 == 0)d ^= ta2[i][j];
    }
    if(d){
        cout << "0\n";
        return 0;
    }
    ll s(0), t(0);
    rep(b, 0, 1<<m)s += dp[n][b][c][d];
    rep(i, 1, n+1)rep(j, 1, m+1)if(ta2[i][j]){
        t += i > 1 && !ta2[i-1][j];
        t += i < n && !ta2[i+1][j];
        t += j > 1 && !ta2[i][j-1];
        t += j < m && !ta2[i][j+1];
    }
    cout << (long double)(t)/s << '\n';
    return 0;
}