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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include<bits/stdc++.h>
using namespace std;
#ifdef LOC
template <class T, class U> auto &operator<<(ostream &out, pair<T, U> a) { return out << "(" << a.first << ", " << a.second << ")"; }
template <class T, class = class enable_if<!is_same<T, string>(), class T::iterator>::type> auto &operator<<(ostream &out, T a) {
    out << "{";
    bool fi = 1;
    for(auto b : a) {
        if(fi) {out<<b; fi=0;}
        else out<<", "<<b;
    }
    return out << "}";
}

template <class T, class X, class Y> auto &operator<<(ostream &out, const priority_queue<T,X,Y>& a) {auto b = a; vector<T> v; while(!b.empty()) {v.push_back(b.top()); b.pop();} return out<<v; }
void dump(){
   cerr << "\e[39m"<<"\n";
}
template <class T, class... Ts> void dump(T a, Ts... x) {
   cerr << a << ", ";
   dump(x...);
}
#define debug(...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<"[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)
#else
#define debug(...) ;
#endif

#define fwd(i, a, b) for(int i=(a);i<(int)(b);i++)
#define rep(i, n) for(int i=0;i<(int)(n);i++)
#define all(X) (X).begin(), (X).end()
#define sz(X) ((int)(X).size())
#define mp make_pair
#define st first
#define nd second
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;


const vector<pii> DIR = {{1,0}, {-1,0}, {0,1}, {0,-1}};

vector<string> tab;
int n,m;
double get(int k) {
    int V = 0;
    int T = 200;
    rep(t, T) {
        vector<vector<int> > a(n, vector<int>(m,0));
        rep(i,n) rep(j,m) if(tab[i][j] == 'O') a[i][j] = k;
        for(;;) {
            vector<pair<pii,pii> > nei;
            rep(i,n) rep(j,m) if(a[i][j] > 0) {
                for(auto [p,q]:DIR) {
                    int i2 = i+p, j2 = j+q;
                    if(i2 >=0 && j2 >=0 && i2 < n && j2 < m && a[i2][j2] > 0)
                        nei.push_back({{i,j},{i2,j2}});
                }
            }
            
            if(sz(nei) == 0) {
                int res = 0;
                for(auto x:a) for(auto y:x) res += y;
                V += res;
                break;
            }
            auto p = nei[rand() % sz(nei)];
            a[p.st.st][p.st.nd]--;
            a[p.nd.st][p.nd.nd]--;
        }
    }
    return V/(double)(T*k);
}

int denom = 24*(1<<5);

int solve() {
    vector<pii> active;
    vector<vector<int>> a(n, vector<int>(m,0));
    vector<vector<int>> nei(n, vector<int>(m,0));
    int t = 0;
    rep(i,n) rep(j,m) if(tab[i][j] == 'O') {
        active.push_back({i,j});
        a[i][j] = denom;
        for(auto [p,q]: DIR) {
            int i2 = i+p, j2 = j+q;
            if(i2 >= 0 && j2 >=0 && i2 < n && j2 < m && a[i2][j2])
                nei[i][j]++;
        }
    }

    while(sz(active)) {
        vector<pii> act2;
        for(auto [i,j]: active) {
            a[i][j] -= nei[i][j]*t;
        }
        t=denom*2;
        for(auto [i,j]: active) {
            if(a[i][j]) {
                nei[i][j] = 0;
                for(auto [p,q]: DIR) {
                    int i2 = i+p, j2 = j+q;
                    if(i2 >= 0 && j2 >=0 && i2 < n && j2 < m && a[i2][j2])
                        nei[i][j]++;
                }
                if(nei[i][j]) {
                    act2.push_back({i,j});
                    t = min(t,a[i][j]/nei[i][j]);
                }
            }
        }
        active = act2;
    }
    int res=0;
    rep(i,n) rep(j,m) res += a[i][j];
    return res;
}

vector<int> lastSubtask;
vi pot3 = {1};


void init() {
    while(sz(pot3) < 16)
        pot3.push_back(pot3.back()*3);

    lastSubtask.resize(43046721);
    tab.resize(4, string(4,'.'));
    ::n = 4; ::m = 4;
    vector<int> v(17);

    rep(mask, sz(lastSubtask)) {
        int lastPyt = -1;;
        rep(j,16)
            if(v[j] == 2) lastPyt = j;
        
        if(lastPyt == -1) {
            rep(i,4) rep(j,4) if(v[i*4+j]) tab[i][j] = 'O'; else tab[i][j] = '.';
            lastSubtask[mask] = solve();
        }
        else {
            lastSubtask[mask] = (lastSubtask[mask-pot3[lastPyt]] + lastSubtask[mask-2*pot3[lastPyt]])/2;
            assert((lastSubtask[mask-pot3[lastPyt]] + lastSubtask[mask-2*pot3[lastPyt]])%2 == 0);
        }

        int pos = 0;
        while(++v[pos] == 3) {
            v[pos] = 0;
            pos++;
        }
    }
    tab.clear();
}

int32_t main(){
    ios::sync_with_stdio(false);
    cin >> n >> m;
    tab.resize(n);
    rep(i,n) cin >> tab[i];
    vector<pii> pyt;
    rep(i,n) rep(j,m) if(tab[i][j] == '?') pyt.push_back({i,j});

    int notLast=0;
    rep(i,n) rep(j,m) if((i%5 == 4 || j%5 == 4) && tab[i][j] == 'O') notLast = 1;
    if(!notLast) {
        int n2 = n, m2 = m;
        auto cp = tab;
        denom = 24*(1<<14);
        init();
        n = 4; m = 4;
        ll res = 0;
        for(int i=0;i<n2;i+=5) {
            for(int j=0;j<m2;j+=5) {
                int nr = 0;
                rep(x,4) rep(y,4) if(i+x < n2 && j+y < m2 && cp[i+x][j+y] != '.')
                    nr += (cp[i+x][j+y] == '?' ? 2 : 1)*pot3[x*4+y];
                res += lastSubtask[nr];
            }
        }
        ll d = denom;
        ll g = __gcd(res,d);
        cout<<res/g<<"/"<<d/g<<"\n";
        return 0;
    }

    if(sz(pyt) <= 10) {
        ll res=0;
        for(int m=0;m<(1<<(sz(pyt)));m++) {
            rep(i,sz(pyt))
                if(m&(1<<i))
                    tab[pyt[i].st][pyt[i].nd] = '.';
                else
                    tab[pyt[i].st][pyt[i].nd] = 'O';
            
            res += solve();
        }
        ll d = denom*(1<<sz(pyt));
        ll g = __gcd(res,d);
        cout<<res/g<<"/"<<d/g<<"\n";
    }
}