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
232
233
234
#include <algorithm>
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define fwd(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) fwd(i, 0, n)
#define all(X) X.begin(), X.end()
#define sz(X) int(size(X))
#define pb push_back
#define eb emplace_back
#define st first
#define nd second
using pii = pair<int, int>; using vi = vector<int>;
using ll = long long; using ld = long double;
#ifdef LOC
auto SS = signal(6, [](int) { *(int *)0 = 0; });
#define DTP(x, y) auto operator << (auto &o, auto a) -> decltype(y, o) { o << "("; x; return o << ")"; }
DTP(o << a.st << ", " << a.nd, a.nd);
DTP(for (auto i : a) o << i << ", ", all(a));
void dump(auto... x) { (( cerr << x << ", " ), ...) <<
'\n'; }
#define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x)
#else
#define deb(...) 0
#endif
 
struct SegTree{
    vector<int> v;
    vector<int> pos;
    vector<int> mod;
    int N = 2;

    void pull(int p){
        int a = v[2*p] + mod[2*p];
        int b = v[2*p + 1] + mod[2*p + 1];
        if(a < b){
            v[p] = a;
            pos[p] = pos[2*p];
        }else{
            v[p] = b;
            pos[p] = pos[2*p + 1];
        }
    }

    void push(int p){
        v[p] += mod[p];
        mod[2*p] += mod[p];
        mod[2*p + 1] += mod[p];
        mod[p] = 0;
    }

    void rangeAdd(int a, int b, int l, int r, int p, int x){
        if(b < l || r < a)return;
        if(a <= l && r <= b){
            mod[p] += x;
        }else{
            int m = (l + r) / 2;
            push(p);
            rangeAdd(a, b, l, m, 2*p, x);
            rangeAdd(a, b, m+1, r, 2*p + 1, x);
            pull(p);
        }
    }

    pii rangeGet(int a, int b, int l, int r, int p){
        if(b < l || r < a)return {1e9, -1};
        if(a <= l && r <= b){
            return {v[p] + mod[p], pos[p]};
        }else{
            int m = (l + r) / 2;
            auto [x1, y1] = rangeGet(a, b, l, m, 2 * p);
            auto [x2, y2] = rangeGet(a, b, m+1, r, 2*p + 1);
            if(x1 < x2){
                return {x1 + mod[p], y1};
            }else{
                return {x2 + mod[p], y2};
            }
        }
    }

    SegTree(int n, vector<int>& free){
        int d = n / sz(free);
        while(N < n) N *= 2;
        v.resize(2*N, 1e9);
        pos.resize(2*N);
        rep(i, N)pos[N + i] = i;
        rep(i, n){
            v[N + i] = free[i/d];
        }
        mod.resize(2*N, 0);
        for(int j = N-1; j >= 1; j--)pull(j);
    }

    void add(int a, int b, int x){
        rangeAdd(a, b, 0, N-1, 1, x);
    }

    pii get(int a, int b){
        return rangeGet(a, b, 0, N-1, 1);
    }
};

void solve(){
    int n, L; cin >> n >> L;
    vector<string> plan(n);
    rep(i, n)cin >> plan[i];
    vector<int> free(L, 0);
    rep(i, n){
        rep(j, L){
            if(plan[i][j] == '.')free[j]++;
        }
    }
    rep(j, L){
        if(free[j] == 0){
            cout << "-1\n";
            return;
        }
    }
    // if(n > 11){
    //     cout << "BIG\n";
    //     return;
    // }
    deb(free);
    vector<pii> frac;
    auto cmp = [](pii a, pii b){
        return 1ll * a.st * b.nd < 1ll * a.nd * b.st;
    };
    for(int b = 1; b <= n; b++){
        for(int a = 0; 2*a <= b * L; a++){
            if(__gcd(a, b) == 1){
                frac.push_back({a, b});
            }
        }
    }
    sort(all(frac), cmp);
    deb(frac);
    auto check = [&](pii f){
        int a = f.st; int b = f.nd;
        deb(a, b);
        if(a == 0)return true;
        SegTree tree(L * b, free);
        vector<vector<pii>> segments(n);
        rep(i, n){
            int goodCnt = 0;
            for(int j = 0; j < a; j++){
                if(plan[i][j/b] == '.' && free[j/b] > 1)goodCnt++;
            }
            for(int j = 0; j + a < L * b; j++){
                if(goodCnt == a){
                    if(sz(segments[i]) == 0 || segments[i].back().nd + 1 < j){
                        segments[i].push_back({j, j});
                    }else{
                        segments[i].back().nd++;
                    }
                }
                if(plan[i][j/b] == '.' && free[j/b] > 1)goodCnt--;
                if(plan[i][(j+a)/b] == '.' && free[(j+a)/b] > 1)goodCnt++;
            }
            if(goodCnt == a){
                if(sz(segments[i]) == 0 || segments[i].back().nd + 1 < L*b - a){
                    segments[i].push_back({L*b - a, L * b - a});
                }else{
                    segments[i].back().nd++;
                }
            }
            if(sz(segments[i]) == 0)return false;
        }
        deb(segments);
        vector<int> p(n);
        rep(i, n)p[i] = i;
        auto nextPos = [&](int pos, int i){
            auto [x, y] = tree.get(pos, pos + a -1);
            y++;
            if(x > 1)y = pos;
            auto it = lower_bound(all(segments[i]), pii{y+1, 0});
            int res = -1;
            if(it != segments[i].end()){
                res = (*it).st;
            }
            if(it != segments[i].begin()){
                it--;
                if(y <= (*it).nd)res = y;
            }
            return res;
        };
        vector<int> v;
        vector<int> last = p;
        last[0] = 1;
        do{
            int diff = 0;
            while(p[diff] == last[diff])diff++;
            while(sz(v) > diff){
                tree.add(v.back(), v.back() + a - 1, 1);
                v.pop_back();
            }
            int pos = 0;
            if(sz(v) > 0)pos = v.back();
            fwd(i, sz(v), n){
                int x = p[i];
                pos = nextPos(pos, x);
                if(pos != -1){
                    v.push_back(pos);
                    tree.add(pos, pos + a - 1, -1);
                }else{
                    break;
                }
            }
            deb(p);
            deb(v);
            if(sz(v) == n)return true;
            last = p;
        }while(next_permutation(all(p)));
        return false;
    };
    int l = 0;
    int r = sz(frac) - 1;
    while(l < r){
        int m = (l + r) / 2 + 1;
        if(check(frac[m])){
            l = m;
        }else{
            r = m-1;
        }
    }
    cout << frac[l].st << "/" << frac[l].nd<< endl;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout << fixed << setprecision(10);
    solve();
    return 0;
}