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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <bits/stdc++.h>

#define ll long long
#define fors(u, n, s) for(ll u=(s); u < (n); u++)
#define foru(u, n) fors(u, n, 0)
#define ir(a, b, x) (((a) <= (x)) && ((x) <= (b)))
#define vec vector
#define pb push_back
#define pint pair<int, int>
#define f first
#define s second
#define mp make_pair

using namespace std;

#define N 200
int n; int m;

struct frac{
    ll a; ll b;

    void shorten(){
        if(a==0 && b==0) return;///cout << "PANIK" << endl;
        ll a_=a; ll b_=b;
        if(a_<b_) swap(a_, b_);
        while(b_!=0){
            a_=a_%b_;
            swap(a_, b_);
        }
        a/=a_;
        b/=a_;
    }

    frac operator+(frac rhs){
        frac output;
        output.a=a*rhs.b+rhs.a*b;
        output.b=b*rhs.b;
        output.shorten();
        return output;
    }

    frac operator-(frac rhs){
        frac output;
        output.a=a*rhs.b-rhs.a*b;
        output.b=b*rhs.b;
        output.shorten();
        return output;
    }

    frac operator*(frac rhs){
        frac output;
        output.a=a*rhs.a;
        output.b=b*rhs.b;
        output.shorten();
        return output;
    }

    frac operator/(frac rhs){
        frac output;
        output.a=a*rhs.b;
        output.b=b*rhs.a;
        output.shorten();
        return output;
    }

    bool operator<(frac rhs){
        return a*rhs.b<b*rhs.a;
    }

    bool operator>(frac rhs){
        return a*rhs.b>b*rhs.a;
    }

    bool operator<=(frac rhs){
        return a*rhs.b<=b*rhs.a;
    }

    bool operator>=(frac rhs){
        return a*rhs.b>=b*rhs.a;
    }

    bool operator==(frac rhs){
        return a*rhs.b==b*rhs.a;
    }

    void print(){
        shorten();
        cout << a << "/" << b;
    }
};

frac to_frac(int x){
    frac output; output.a=x; output.b=1;
    return output;
}

vec<pint> adj(pint in){
    vec<pint> out;

    if(ir(0, n-1, in.f-1)) out.pb(mp(in.f-1, in.s));
    if(ir(0, n-1, in.f+1)) out.pb(mp(in.f+1, in.s));
    if(ir(0, m-1, in.s-1)) out.pb(mp(in.f, in.s-1));
    if(ir(0, m-1, in.s+1)) out.pb(mp(in.f, in.s+1));

    return out;
}

char board[N][N];
char alt_board[N][N];
frac offset[N][N]; ///HOW MUCH EVAPORATED FROM PREVIOUS EDGES

void save_board(){
    foru(i, n) foru(j, m) alt_board[i][j]=board[i][j];
}

void reload_board(){
    foru(i, n) foru(j, m) board[i][j]=alt_board[i][j];
}

vec<pint> unks;

frac gettime(pint in){
    frac c = offset[in.f][in.s]; c.shorten();
    int degree = 0;
    for(auto i : adj(in)) degree+=(board[i.f][i.s]=='O');
    frac d = to_frac(degree);
    if(c==to_frac(1)) return to_frac(0);
    if(degree==0) return to_frac(INT_MAX);
    return (to_frac(1)-c)/d;
}

struct pint_wrapper{
    pint wrapped;

    bool operator<(const pint_wrapper rhs)const{
        if(wrapped.f==-1) return true;
        if(rhs.wrapped.f==-1) return false;
        if(gettime(wrapped)==gettime(rhs.wrapped)) return wrapped.f*N+wrapped.s < rhs.wrapped.f*N+rhs.wrapped.s;
        return gettime(wrapped) < gettime(rhs.wrapped);
    }
};

int main()
{
    cin >> n >> m;
    foru(i, n) foru(j, m) cin >> board[i][j];
    foru(i, n) foru(j, m) offset[i][j]=to_frac(0);

    if(n==1){
        frac output = to_frac(0);
        foru(i, m){
            frac p = to_frac(1);
            if(board[0][i]=='?') p.b=2;
            if(board[0][i]=='.') p.a=0;

            frac p1=to_frac(0);
            if(i-1>=0 && board[0][i-1]=='?') {p1.a=1; p1.b=2;}
            if(i-1>=0 && board[0][i-1]=='O') {p1.a=1; p1.b=1;}

            frac p2=to_frac(0);
            if(i-2>=0 && board[0][i-2]=='?') {p2.a=1; p2.b=2;}
            if(i-2>=0 && board[0][i-2]=='O') {p2.a=1; p2.b=1;}

            frac p3=to_frac(0);
            if(i+1<m && board[0][i+1]=='?') {p3.a=1; p3.b=2;}
            if(i+1<m && board[0][i+1]=='O') {p3.a=1; p3.b=1;}

            frac p4=to_frac(0);
            if(i+2<m && board[0][i+2]=='?') {p4.a=1; p4.b=2;}
            if(i+2<m && board[0][i+2]=='O') {p4.a=1; p4.b=1;}

            output = output + p*(to_frac(1)-p1)*(to_frac(1)-p3);
            output = output + p*(to_frac(1)-p1)*p3*p4/to_frac(2);
            output = output + p*p1*p2*(to_frac(1)-p3)/to_frac(2);
        }
        output.print(); return 0;
    }


    int unk_cnt=0;
    foru(i, n) foru(j, m) if(board[i][j]=='?') unks.pb(mp(i, j));

    if(unks.size()<=5){
        frac output = to_frac(0);
        foru(b, 1<<unks.size()){
            save_board();
            int b_ = b;
            for(auto i : unks){
                if(b_ & 1) board[i.f][i.s]='O';
                else board[i.f][i.s]='.';
                b_=b_/2;
            }

            set<pint_wrapper> st;

            pint_wrapper helper;

            foru(i, n) foru(j, m){
                helper.wrapped=mp(i, j);
                if(board[i][j]=='O') st.insert(helper);
            }

            while(!st.empty()){
                helper.wrapped.f=-1;
                set<pint_wrapper>::iterator node_wrapped = st.lower_bound(helper);
                pint node = (*node_wrapped).wrapped;
                st.erase(node_wrapped);
                frac time = gettime(node);
                if(time==to_frac(INT_MAX)){
                    output = output + offset[node.f][node.s];
                    continue;
                }
                for(auto i : adj(node)){
                    helper.wrapped=i;
                    st.erase(st.lower_bound(helper));
                }

                board[node.f][node.s]='.';

                for(auto i : adj(node)){
                    offset[i.f][i.s] = offset[i.f][i.s] + time;
                    helper.wrapped=i;
                    st.insert(helper);
                }
            }
            reload_board();
        }
        output.print(); return 0;
    }
    return 0;
}