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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <array>
#include <random>
#include <cmath>
#include <chrono>
#include <list>
#include <ctime>
#include <sstream>
#include <queue>
#include <climits>
#include <stack>
#include <valarray>
#include <random>
#include <bitset>
#include <numeric>
#include <iomanip>
#include <cassert>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
#define rep(x, b, e) for(int x=(b); x<(e); ++x)
#define trav(a, x) for(auto& a : x)
#define ford(x, b, e) for(int x=((int)(b))-1; x>=(e); --x)
#define all(c) c.begin(),c.end()
#define sz(x) ((int)((x).size()))
#define pb push_back
#define eb emplace_back
#define st first
#define nd second
#define mp(x,y) make_pair(x,y)
typedef short int sint;
template<typename T> bool ckmin(T& a, const T& b){return b<a?a=b,1:0;}
template<typename T> bool ckmax(T& a, const T& b){return b>a?a=b,1:0;}


template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(char c) {
  return string(1, c);
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif


// #include <atcoder/modint>
// using namespace atcoder;
// using mint = modint998244353; // modint1000000007;
// typedef vector<mint> vmi;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // use rng() to get unsigned int
// mt19937_64 for random long longs

const int N = 19;
const sint inf = 100;
int n, LENGTH;
vector<string> s;
vector<vector<pii>> rr;
vi mms[(1 << (N - 1))];

struct PQ {
  int p, q;
  bool operator<(const PQ& a) const {
    return p * a.q < q * a.p;
  }
  bool operator==(const PQ& a) const {
    return p == a.p && q == a.q;
  }
};

string to_string(const PQ& a) {
  return to_string(a.p) + '/' + to_string(a.q);
}

struct Node {
  char m; // what is the max
  char mi;// what is the min
  char delta; // what is the lazy value
  int l; // left node
  int r; // right node
};

string to_string(const Node&n) {
  return to_string(vi({n.m, n.mi, n.delta, n.l, n.r}));
}

struct Tree {
  int mul;
  int M, D;
  vector<Node> t;
  bool isInitialized = false;
  void init(int mul_) {
    mul = mul_;
    D = mul * LENGTH;
    M = 1;
    while (M < D) M <<= 1;
    t.resize(2 * M);
    debug("init", mul, M, D);
    rep(i, 0, n) {
      rep(pos, 0, LENGTH) {
        if (s[i][pos] == 'X') continue;
        rep(j, 0, mul) {
          int idx = M + pos * mul + j;
          ++t[idx].m;
          ++t[idx].mi;
        }
      }
    }
    rep(i, D, M) {
      t[M + i].mi = inf;
    }
    ford(idx, M, 1) {
      t[idx].m = max(t[2 * idx].m, t[2 * idx + 1].m);
      t[idx].mi = min(t[2 * idx].mi,t[2 * idx + 1].mi);
      t[idx].delta = 0;
      t[idx].l = 2 * idx;
      t[idx].r = 2 * idx + 1;
    }
    isInitialized = true;
  }

  void reset(int mul_) {
    if (!isInitialized) {
      init(mul_);
    } else {
      clear();
    }
  }

  void clear() {
    t.resize(2 * M);
  }

  inline void propagate(int p, int L, int R) {
    // debug(" propagate", p, L, R, t[p]);
    if (t[p].delta) {
      if (L != R) {
        t[p].l = newlazykid(t[p].l, t[p].delta);
        t[p].r = newlazykid(t[p].r, t[p].delta);
      }
      t[p].delta = 0;
    }
  }

  int newlazykid(int node, int delta) {
    int p = t.size();
    t.eb(t[node].m, t[node].mi, t[node].delta, t[node].l, t[node].r);

    // recompute
    t[p].delta += delta;
    t[p].m -= delta;
    t[p].mi -= delta;
    // debug("created new node", p, t[p], "base: ", t[node]);
    return p;
  }

  int newparent(int l, int r) {
    int p = t.size();
    t.eb( 0, inf, 0, l, r );

    t[p].m = max(t[l].m, t[r].m);
    t[p].mi= min(t[l].mi,t[r].mi);
    assert(t[p].delta == 0);

    return p;
  }

  // increase delta by 1 on range [a, b)
  int update(int a, int b, int p, int L, int R) {
    if (b < L || a > R) return p;
    // debug(" **** update", a, b, p, L, R);
    if (a <= L && R <= b) return newlazykid(p, 1);
    propagate(p, L, R);
    int M = (L + R) / 2;
    return newparent(update(a, b, t[p].l, L, M), update(a, b, t[p].r, M + 1, R));
  }

  int extract2(int p, int L, int R) {
    // debug("----extract2", p, L, R);
    if (L == R) return L;
    int M = (L + R) / 2;
    propagate(t[p].l, L, M);
    propagate(t[p].r, M+1, R);
    // debug("   M: ", M, "left: ", t[ t[p].l ].mi, t[ t[p].l ].delta, "right", t[ t[p].r ].mi, t[ t[p].r ].delta);
    if (t[ t[p].r ].mi == 1) return extract2(t[p].r, M + 1, R);
    return extract2(t[p].l, L, M);
  }

  int extract(int p, int L, int R) {
    // debug("----extract", p, L, R);
    if (L == R) return L;
    int M = (L + R) / 2;
    propagate(t[p].l, L, M);
    propagate(t[p].r, M+1, R);
    // debug("   M: ", M, "left: ", t[ t[p].l ].m, t[ t[p].l ].delta, "right", t[ t[p].r ].m, t[ t[p].r ].delta);
    if (t[ t[p].l ].m > 1) return extract(t[p].l, L, M);
    return extract(t[p].r, M + 1, R);
  }

  // find last index == 1 in range
  int query2(int a, int b, int p, int L, int R) {
    if (b < L || R < a) return -1;
    propagate(p, L, R);
    // debug(" in query2", a, b, p, L, R, t[p]);
    if (a <= L && R <= b) {
      // interesting
      if (t[p].mi == 1) {
        // debug("found node: ", t[p], t[t[p].l], t[t[p].r]);
        return extract2(p, L, R);
      } else {
        return -1;
      }
    }
    int M = (L + R) / 2;
    int inRight = query2(a, b, t[p].r, M + 1, R);
    if (inRight != -1) return inRight;
    return query2(a, b, t[p].l, L, M);
  }

  int query(int a, int b, int p, int L, int R) { // return first index > 1
    if (b < L || R < a) return -1;
    propagate(p, L, R);
    // debug(" in query", a, b, p, L, R);
    if (a <= L && R <= b) {
      // interesting
      if (t[p].m > 1) {
        // get result from here
        return extract(p, L, R);
      } else {
        return -1;
      }
    }
    int M = (L + R) / 2;
    int inLeft = query(a, b, t[p].l, L, M);
    if (inLeft != -1) return inLeft;
    return query(a, b, t[p].r, M + 1, R);
  }

  void print(int p, int L, int R) {
    debug(p, L, R, t[p]);
    if (L != R) {
      int M = (L + R) / 2;
      print(t[p].l, L, M);
      print(t[p].r, M+1, R);
    }
  }
} trees[N];

vector<PQ> possibleAnswers(int n, int l, PQ lower, PQ upper) {
  vector<PQ> qs;
  qs.pb(lower);
  qs.eb(upper);
  rep(p, 1, n + 1) {
    rep(q, p + 1, n + 1) {
  // int p = 1, q = 1;
  // {{
      if (__gcd(p, q) > 1) continue;
      rep(l, 1, LENGTH + 1) {
        int licz = p * l;
        int mian = q;
        int g = __gcd(licz, mian);
        licz /= g;
        mian /= g;
        if (l * mian < licz) continue;
        PQ toAdd(licz, mian);
        if (toAdd < lower || !(toAdd < upper)) {
          continue;
        }
        qs.eb(licz, mian);
      }
    }
  }
  sort(all(qs));
  int dl = unique(all(qs)) - qs.begin();
  qs.resize(dl);
  return qs;
}

struct Event {
  int typ;
  int a, b;
  // typ = 0, a <- maska, b <- korzen
  // typ = 1, a <- ktora osoba, b <- ktory element staje sie aktywny
};

const int LIMIT = 1e7;
int ops;
bool found = false;
int greedyDL;
vi pom;

void gogo(int maska) {
  debug(maska, pom);
  if (maska == ((1 <<n) - 1)) {
    found = true;
    return;
  }
  for (auto miss : mms[maska]) {
    ++ops;
    if (ops >= LIMIT) return;
    for (auto range : rr[miss]) {
      // try to place it here as soon as possible
      ++ops;
      if (ops >= LIMIT) return;
      if (range.nd - range.st < greedyDL) {
        continue;
      }
      int ostZly = range.st - 1;
      rep(i, range.st, range.nd) {
        ++ops;
        if (ops >= LIMIT) return;
        if (pom[i] == 1) {
          ostZly = i;
        } else if ( i - ostZly >= greedyDL ) {
          debug("adding ", miss, ostZly + 1, i);
          rep(j, ostZly + 1, i + 1) {
            ++ops;
            if (ops >= LIMIT) return;
            --pom[j];
          }
          gogo(maska | (1 << miss));
          if (found) return;
          rep(j, ostZly + 1, i + 1) {
            ++ops;
            if (ops >= LIMIT) return;
            ++pom[j];
          }
        }
      }
    }
  }
}

bool greedy(int dl) { // n * L
  ops = 0;
  greedyDL = dl;
  found = false;
  pom.assign(LENGTH, 0);
  rep(i, 0, LENGTH) {
    rep(j, 0, n) {
      ++ops;
      if (s[j][i] == '.') {
        ++pom[i];
      }
    }
  }
  gogo(0);
  debug("checking greedy", found);
  return found;
}

bool ok(PQ gr) {
  debug("\n\n\nchecking: ", gr);
  if (gr.q == 1) {
    if (greedy(gr.p)) {
      return true;
    }
  }
  auto &tree = trees[gr.q];
  tree.reset(gr.q);
  int T = gr.p;
  debug("   T: ", T);
  vi ptrs(n); // which is the next range I can consider in rr. In rr, indices are not multiplied
  int MAX_CZAS = LENGTH * gr.q;
  vector<vector<Event>> qs(MAX_CZAS); // masks I need to consider

  // update ptrs
  rep(i, 0, n) {
    // debug("Prepping person", i, rr[i]);
    int lastGood = sz(rr[i]);
    ford(j, sz(rr[i]), 0) {
      int thisLen = (rr[i][j].nd - rr[i][j].st) * gr.q;
      if (thisLen < T) continue;
      // this one is good
      // at what time does it stop being good
      int lastGoodStart = rr[i][j].nd * gr.q - T;
      if (lastGoodStart + 1 < sz(qs)) {
        // debug("  adding lastGoodStart for", lastGood, lastGoodStart + 1);
        qs[lastGoodStart + 1].eb(1, i, lastGood);
      }
      lastGood = j;
    }
    ptrs[i] = lastGood;// <=> qs[0].eb(1, i, lastGood)
  }
  debug("setup ptrs", ptrs);
  qs[0].pb({0, 0, 1});
  vector<tuple<int, int, vector<pii>>> seen(1 << n, {-1, -1, {}});
  seen[0] = {0, 1, {}}; // czas nastepnego i korzen
  
  auto isBetter = [&](int mask, tuple<int, int, vector<pii>> potencjal) {
    if (get<0>(seen[mask]) == -1) return true;
    if (get<0>(potencjal) < get<0>(seen[mask])) return true;
    debug("need to compare side by side: new", get<2>(potencjal), get<2>(seen[mask]));
    ford(i, sz( get<2>(potencjal) ), 0) {
      if (get<2>(potencjal)[i].nd != get<2>(seen[mask])[i].nd) {
        return get<2>(potencjal)[i].nd < get<2>(seen[mask])[i].nd;
      }
    }
    return false;
  };
  rep(czas, 0, sz(qs)) {
    // if (!qs.empty())debug("\t\t\t\t CZAS!!!", czas);
    int idx = 0;
    while (idx < sz(qs[czas])) {
      auto [typ, a, b] = qs[czas][idx];
      idx++;
      // debug("  event: ", typ, a, b);
      if (typ == 1) {
        ptrs[a] = b;
      } else {
        // a <- maska
        // b <- korzen
        if (get<0>(seen[a]) != -1 && get<1>(seen[a]) != b) {
          // debug("Exiting as already processed with different ", a, seen[a], czas);
          continue;
        }
        bool ok = true;
        for (auto nel : mms[a]) {
          // can I put in this empty element
          if (ptrs[nel] == sz(rr[nel])) {
            // debug("  exiting due to: ", nel);
            ok = false;
            break;
          }
        }
        if (!ok) continue;
        for (auto nel : mms[a]) {
          int nmask = a | (1 << nel);
          if (nmask == ((1<<n) - 1)) {
            debug("   full mask");
            tree.clear();
            return true;
          }
          // debug("    rozmwazam dodanie: ", nel, rr[nel][ptrs[nel]]);
          // debug("  lasty: ", get<2>(seen[a]));
          int mojCzas = czas;
          if (get<2>(seen[a]).size() && get<2>(seen[a]).back().st > nel && get<2>(seen[a]).back().nd == czas) {
            mojCzas = czas + 1;
          }
          int earliestSleepStart = max(mojCzas, rr[nel][ptrs[nel]].st * gr.q);
          // debug("    will update the tree, my sleep start: ", earliestSleepStart);

          int newRoot = tree.update(earliestSleepStart, earliestSleepStart + T - 1, b, 0, tree.M - 1);
          // debug("    will search in range: ", earliestSleepStart, tree.M - 1);
          int nextCzas = tree.query(earliestSleepStart, tree.M - 1, newRoot, 0, tree.M - 1);

          if ((nextCzas == -1) || ((get<0>(seen[nmask]) != -1) && (nextCzas > get<0>(seen[nmask]) ))) {
            continue;
          }

          // tree.print(newRoot, 0, tree.M - 1);
          int nextFullFreeTime = tree.query2(nextCzas, nextCzas + T - 1, newRoot, 0, tree.M - 1);
          if (nextFullFreeTime != -1) {
            // debug("        postponing next start due to being needed in the T region", nextFullFreeTime);
            nextCzas = tree.query(nextFullFreeTime + 1, tree.M - 1, newRoot, 0, tree.M - 1);
            // debug("         next free spot is: ", nextCzas);
          }
          if (nextCzas == -1 || nextCzas == MAX_CZAS) {
            continue;
          }
          vector<pii> moje(all(get<2>(seen[a])));
          moje.eb( nel, earliestSleepStart );
          tuple<int, int, vector<pii>> potencjal = { nextCzas, newRoot, moje };
          // debug("lasty dla:", newRoot, lasty[newRoot]);
          if (isBetter(nmask, potencjal)) {
            // debug("&&&&&&&&&&&&&&&CAN start at: ", newRoot, nextCzas, nmask);
            qs[nextCzas].eb(0, nmask, newRoot);
            seen[nmask] = potencjal;
            // debug("setting seen: ", nmask, nextCzas);
          }
        }
      }
    }
  }
  tree.clear();
  return false;
}

void read(bool print = false) {
  cin >> n >> LENGTH;
  s.resize(n);
  rep(i, 0, n) {
    cin >> s[i];
  }

  if (print) {
    cout << n << ' ' << LENGTH << endl;
    rep(i, 0, n) {
      cout << s[i] << endl;
    }
  }
}

void prepareRanges() {
  rep(i, 0, LENGTH) {
    int who = -1;
    int ile = 0;
    rep(ziom, 0, n) {
      if (s[ziom][i] == '.') {
        who = ziom;
        ++ile;
      }
    }
    if (ile == 1) {
      // no choice, he is busy there
      s[who][i] = 'X';
    }
  }
  rr.resize(n);
  rep(i, 0, n) {
    int idx = 0;
    while (idx < LENGTH) {
      while (idx < LENGTH && s[i][idx] == 'X') {
        ++idx;
      }
      if (idx == LENGTH) break;
      int nidx = idx + 1;
      while (nidx < LENGTH && s[i][nidx] == '.') {
        ++nidx;
      }
      rr[i].eb(idx, nidx); // [st;end)
      idx = nidx;
    }
  }
}

void prepareMasks() {
  rep(i, 0, (1 << n)) {
    rep(j, 0, n) {
      if ((i & (1 <<j)) == 0) {
        mms[i].pb(j);
      }
    }
  }
}

void prepare() {
  prepareMasks();
  prepareRanges();
}

bool impossible() {
  rep(i, 0, LENGTH) {
    bool ok = false;
    rep(j, 0, n) {
      if (s[j][i] == '.') {
        ok = true;
        break;
      }
    }
    if (!ok) {
      debug("bad on idx", i);
      return true;
    }
  }
  return false;
}

void solve(bool print = false) {
  if (impossible()) {
    cout << -1 << endl;
    return;
  }
  prepare();
  debug("prepped");
  int pInteger = 1, qInteger = LENGTH;
  while (pInteger < qInteger) {
    int sr = (pInteger + qInteger) / 2;
    if (ok({ sr, 1 })) {
      pInteger = sr + 1;
    } else {
      qInteger = sr;
    }
  }
  // pInteger !ok, pInteger - 1ok

  vector<PQ> qs = possibleAnswers(n, LENGTH, {pInteger - 1, 1}, {pInteger, 1});
  debug(qs);
  int p = 1, q = sz(qs) - 1;
  while (p < q) {
    int m = (p + q) / 2;
    // pierwszy co sie nie da
    if (ok(qs[m])) {
      p = m + 1;
    } else {
      q = m;
    }
    // if (qs[m].p == 10)  break;
  }
  // ans: p - 1
  cout << qs[p - 1].p << '/' << qs[p - 1].q << endl;
}

void clear() {
  rep(i, 0, (1 << n)) mms[i].clear();
  rr.clear();
  rep(i, 1, n + 1) {
    trees[i].t.clear();
    trees[i].isInitialized = false;
  }
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  int t;
  // cin >> t;
  t = 1;
  rep(i, 0, t) {
    bool print = false;
    // cout << i << endl;
    // if (i + 1 == 81) {
    //   print = true;
    // }
    read(print);
    debug("reead");
    // if (print)
      solve();
    // clear();
    if (print) break;
  }
}