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
#include "bits/stdc++.h"

using namespace std;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#include "osalib.h"

using ll = long long;
using ld = long double;

constexpr int nax = 1000 + 105;

int fut[nax * nax];

int fuf(int w) {
  if (fut[w] == w) return w;
  return fut[w] = fuf(fut[w]);
}

void fuu(int u, int v) {
  fut[fuf(u)] = fuf(v);
}

constexpr int NOTHING    = 0;
constexpr int WALL       = 1;
constexpr int BOTTLENECK = 2;

constexpr int ABOVE      = 0;
constexpr int BELOW      = 1;
constexpr int LEFT       = 0;
constexpr int RIGHT      = 1;

constexpr int HORIZONTAL = 0;
constexpr int VERTICAL   = 1;
constexpr int DIAGONAL_S = 2;
constexpr int DIAGONAL_B = 3;

constexpr int FROM_ABOVE = 0;
constexpr int FROM_RIGHT = 1;
constexpr int FROM_BELOW = 2;
constexpr int FROM_LEFT  = 3;

struct Krawedz {
  int cnt[2];
  int type;
};

int H, W, k_cnt;
Krawedz krawedz[nax][nax][4];
bool is_k[nax][nax];

int Id(int r, int c) {
  assert(0 <= r and r <= H);
  assert(0 <= c and c <= W);
  return r * (W + 1) + c;
}

int GetR(int id) { return id / (W + 1); }
int GetC(int id) { return id % (W + 1); }

int NW_id(int r, int c) { return Id(r - 1, c - 1); }
int NE_id(int r, int c) { return Id(r - 1, c - 0); }
int SW_id(int r, int c) { return Id(r - 0, c - 1); }
int SE_id(int r, int c) { return Id(r - 0, c - 0); }

int visited_time = 0x5ADFACE;
int visited[nax][nax];

struct KolElement {
  int r, c, f;
};

pair<int, int> DoubleBfs(int r1, int c1, int f1, int r2, int c2, int f2) {
  debug() << "DoubleBfs(" imie(r1) imie(c1) imie(f1) imie(r2) imie(c2) imie(f2) ")";
  visited_time++;
  vector<KolElement> kol[2];
  int cnt[2] = {0, 0};
  visited[r1][c1] = visited_time;
  visited[r2][c2] = visited_time;
  kol[0].push_back(KolElement{r1, c1, f1});
  kol[1].push_back(KolElement{r2, c2, f2});
  for (int k = 0; k < (int) kol[0].size() and k < (int) kol[1].size(); k++) {
    for (int h = 0; h < 2; h++) {
      const int r = kol[h][k].r;
      const int c = kol[h][k].c;
      const int f = kol[h][k].f;
      assert(1 <= r and r <= H);
      assert(1 <= c and c <= W);
      assert(0 <= f and f <= 3);
      debug() << imie(h) imie(r) imie(c) imie(f);
      auto TryToGo = [h, &kol, &cnt](int kr, int kc, int kdir, int kwhich,
                                     int dest_r, int dest_c, int from) -> void {
        Krawedz& k = krawedz[kr][kc][kdir];
        if (k.type == BOTTLENECK) {
          cnt[h] += k.cnt[kwhich];
        } else if (k.type == NOTHING) {
          if (visited[dest_r][dest_c] != visited_time) {
            visited[dest_r][dest_c] = visited_time;
            kol[h].push_back(KolElement{dest_r, dest_c, from});
          }
        } else {
          assert(k.type == WALL);
          // No way.
        }
      };
      Krawedz& diag_s = krawedz[r - 1][c - 1][DIAGONAL_S];
      Krawedz& diag_b = krawedz[r - 1][c - 1][DIAGONAL_B];
      bool go_above = true;
      bool go_below = true;
      bool go_left  = true;
      bool go_right = true;
      if (diag_s.type == BOTTLENECK) {
        assert(diag_b.type == NOTHING);
        cnt[h] += diag_s.cnt[f == FROM_ABOVE or f == FROM_LEFT];
        if (is_k[r][c] and (f == FROM_BELOW or f == FROM_RIGHT)) {
          cnt[h]++;
        }
        if (f == FROM_ABOVE or f == FROM_LEFT) {
          go_right = go_below = false;
        } else {
          go_left = go_above = false;
        }
      } else if (diag_b.type == BOTTLENECK) {
        cnt[h] += diag_b.cnt[f == FROM_ABOVE or f == FROM_RIGHT];
        if (is_k[r][c] and (f == FROM_BELOW or f == FROM_LEFT)) {
          cnt[h]++;
        }
        if (f == FROM_ABOVE or f == FROM_RIGHT) {
          go_below = go_left = false;
        } else {
          go_above = go_right = false;
        }
      } else {
        cnt[h] += is_k[r][c];
      }
      debug() << imie(go_above) imie(go_below) imie(go_left) imie(go_right);
      if (go_above) TryToGo(r - 1, c - 1, HORIZONTAL, ABOVE, r - 1, c    , FROM_BELOW);
      if (go_below) TryToGo(r - 0, c - 1, HORIZONTAL, BELOW, r + 1, c    , FROM_ABOVE);
      if (go_left ) TryToGo(r - 1, c - 1, VERTICAL,    LEFT, r    , c - 1, FROM_RIGHT);
      if (go_right) TryToGo(r - 1, c - 0, VERTICAL,   RIGHT, r    , c + 1, FROM_LEFT );
    }
  }
  debug() << imie(kol[0].size()) imie(kol[1].size());
  if (kol[0].size() < kol[1].size()) {
    cnt[1] = -1;
  } else {
    cnt[0] = -1;
  }
  debug() << imie(cnt[0]) imie(cnt[1]);
  return {cnt[0], cnt[1]};
}

bool MakeBottleneck(int r, int c, int dir) {
  debug() << "MakeBottleneck(" imie(r) imie(c) imie(dir) ")";
  assert(0 <= r and r <= H);
  assert(0 <= c and c <= W);
  assert(0 <= dir and dir <= 3);
  Krawedz& k = krawedz[r][c][dir];
  if (k.type == WALL) {
    return true;
  } else if (k.type == BOTTLENECK) {
    debug() << imie(k.cnt[0]) imie(k.cnt[1]);
    return k.cnt[0] == 0 or k.cnt[1] == 0;
  }
  k.cnt[0] = k.cnt[1] = 0;
  k.type = BOTTLENECK;
  int bfs_f[2] = {-1, -1};
  int bfs_r[2] = {-1, -1};
  int bfs_c[2] = {-1, -1};
  if (dir == VERTICAL) {
    bfs_r[LEFT]  = r + 1;
    bfs_c[LEFT]  = c + 0;
    bfs_f[LEFT]  = FROM_RIGHT;
    bfs_r[RIGHT] = r + 1;
    bfs_c[RIGHT] = c + 1;
    bfs_f[RIGHT] = FROM_LEFT;
  } else if (dir == HORIZONTAL) {
    bfs_r[ABOVE] = r + 0;
    bfs_c[ABOVE] = c + 1;
    bfs_f[ABOVE] = FROM_BELOW;
    bfs_r[BELOW] = r + 1;
    bfs_c[BELOW] = c + 1;
    bfs_f[BELOW] = FROM_ABOVE;
  } else if (dir == DIAGONAL_B or dir == DIAGONAL_S) {
    bfs_r[ABOVE] = r + 1;
    bfs_c[ABOVE] = c + 1;
    bfs_f[ABOVE] = FROM_ABOVE;
    bfs_r[BELOW] = r + 1;
    bfs_c[BELOW] = c + 1;
    bfs_f[BELOW] = FROM_BELOW;
  } else {
    assert(false);
  }
  pair<int, int> result =
      DoubleBfs(bfs_r[0], bfs_c[0], bfs_f[0], bfs_r[1], bfs_c[1], bfs_f[1]);
  if (result.first == -1) {
    assert(0 <= result.second and result.second <= k_cnt);
    result.first = k_cnt - result.second;
  } else {
    assert(0 <= result.first and result.first <= k_cnt);
    assert(result.second == -1);
    result.second = k_cnt - result.first;
  }
  k.cnt[0] = result.first;
  k.cnt[1] = result.second;
  return k.cnt[0] == 0 or k.cnt[1] == 0;
}

void AddWall(int r, int c, int dir) {
  assert(0 <= r and r <= H);
  assert(0 <= c and c <= W);
  assert(0 <= dir and dir <= 1);
  krawedz[r][c][dir].type = WALL;
  if (dir == HORIZONTAL) {
    assert(c < W);
    fuu(Id(r, c), Id(r, c + 1));
  } else {
    assert(dir == VERTICAL);
    assert(r < H);
    fuu(Id(r, c), Id(r + 1, c));
  }
}

void NowaWyspa(int n, int m, char** board) {
  H = n;
  W = m;
  for (int r = 0; r <= H; r++) {
    for (int c = 0; c <= W; c++) {
      fut[Id(r, c)] = Id(r, c);
    }
  }
  for (int r = 0; r < H; r++) {
    AddWall(r, 0, VERTICAL);
    AddWall(r, W, VERTICAL);
  }
  for (int c = 0; c < W; c++) {
    AddWall(0, c, HORIZONTAL);
    AddWall(H, c, HORIZONTAL);
  }
  k_cnt = 0;
  for (int r = 1; r <= H; r++) {
    for (int c = 1; c <= W; c++) {
      switch (board[r - 1][c - 1]) {
        case 'W': {
          AddWall(r - 1, c - 1, VERTICAL);
          AddWall(r - 1, c - 1, HORIZONTAL);
          AddWall(r - 0, c - 1, HORIZONTAL);
          AddWall(r - 1, c - 0, VERTICAL);
          break;
        }

        case 'K': {
          is_k[r][c] = true;
          k_cnt++;
          break;
        }

        case '.': {
          break;
        }

        default: {
          assert(false);
        }
      }
    }
  }
}

int NowaWarownia(int r, int c) {
  debug() << "NowaWarownia(" imie(r) imie(c) ") ------------------------------";
  assert(1 <= r and r <= H);
  assert(1 <= c and c <= W);
  const int nw = NW_id(r, c);
  const int ne = NE_id(r, c);
  const int sw = SW_id(r, c);
  const int se = SE_id(r, c);
  const int fnw = fuf(nw);
  const int fne = fuf(ne);
  const int fsw = fuf(sw);
  const int fse = fuf(se);
  debug() << imie(nw) imie(ne) imie(sw) imie(se);
  bool can_cut = true;
  if (fnw == fne and !MakeBottleneck(GetR(nw), GetC(nw), HORIZONTAL)) {
    can_cut = false;
  }
  if (fnw == fsw and !MakeBottleneck(GetR(nw), GetC(nw), VERTICAL)) {
    can_cut = false;
  }
  if (fsw == fse and !MakeBottleneck(GetR(sw), GetC(sw), HORIZONTAL)) {
    can_cut = false;
  }
  if (fne == fse and !MakeBottleneck(GetR(ne), GetC(ne), VERTICAL)) {
    can_cut = false;
  }
  if (!can_cut) {
    return 0;
  }
  if (fnw != fne and fnw != fsw and fsw != fse and fne != fse) {
    if (fnw == fse and !MakeBottleneck(GetR(nw), GetC(nw), DIAGONAL_B)) {
      can_cut = false;
    }
    if (fne == fsw and !MakeBottleneck(GetR(nw), GetC(nw), DIAGONAL_S)) {
      can_cut = false;
    }
  }
  if (!can_cut) {
    return 0;
  }
  AddWall(GetR(nw), GetC(nw), HORIZONTAL);
  AddWall(GetR(nw), GetC(nw), VERTICAL);
  AddWall(GetR(sw), GetC(sw), HORIZONTAL);
  AddWall(GetR(ne), GetC(ne), VERTICAL);
  return 1;
}

void PrzeniesOsade(int r1, int c1, int r2, int c2) {
  assert(1 <= r1 and r1 <= H);
  assert(1 <= c1 and c1 <= W);
  assert(1 <= r2 and r2 <= H);
  assert(1 <= c2 and c2 <= W);
  assert(is_k[r1][c1]);
  assert(!is_k[r2][c2]);
  assert(abs(r1 - r2) + abs(c1 - c2) == 1);
  int id = -1;
  int dir = -1;
  int which_inc = -1;
  int from = -1;
  if (r1 < r2) {
    // S.
    id = SW_id(r1, c1);
    dir = HORIZONTAL;
    which_inc = BELOW;
    from = FROM_ABOVE;
  } else if (r2 < r1) {
    // N.
    id = NW_id(r1, c1);
    dir = HORIZONTAL;
    which_inc = ABOVE;
    from = FROM_BELOW;
  } else if (c1 < c2) {
    // E.
    id = NE_id(r1, c1);
    dir = VERTICAL;
    which_inc = RIGHT;
    from = FROM_LEFT;
  } else if (c2 < c1) {
    // W.
    id = NW_id(r1, c1);
    dir = VERTICAL;
    which_inc = LEFT;
    from = FROM_RIGHT;
  }
  Krawedz& k = krawedz[GetR(id)][GetC(id)][dir];
  if (k.type == BOTTLENECK) {
    k.cnt[which_inc ^ 0]++;
    k.cnt[which_inc ^ 1]--;
  }
  is_k[r1][c1] = false;
  is_k[r2][c2] = true;

  Krawedz& diag_s1 = krawedz[r1 - 1][c1 - 1][DIAGONAL_S];
  Krawedz& diag_b1 = krawedz[r1 - 1][c1 - 1][DIAGONAL_B];
  Krawedz& diag_s2 = krawedz[r2 - 1][c2 - 1][DIAGONAL_S];
  Krawedz& diag_b2 = krawedz[r2 - 1][c2 - 1][DIAGONAL_B];
  if (diag_s1.type == BOTTLENECK) {
    assert(diag_b1.type != BOTTLENECK);
    if (from == FROM_BELOW or from == FROM_RIGHT) {
      diag_s1.cnt[ABOVE]++;
      diag_s1.cnt[BELOW]--;
      assert(diag_s1.cnt[BELOW] >= 0);
    }
  } else if (diag_b1.type == BOTTLENECK) {
    if (from == FROM_BELOW or from == FROM_LEFT) {
      diag_b1.cnt[ABOVE]++;
      diag_b1.cnt[BELOW]--;
      assert(diag_b1.cnt[BELOW] >= 0);
    }
  }
  if (diag_s2.type == BOTTLENECK) {
    assert(diag_b2.type != BOTTLENECK);
    if (from == FROM_ABOVE or from == FROM_LEFT) {
      diag_s2.cnt[ABOVE]--;
      diag_s2.cnt[BELOW]++;
      assert(diag_s2.cnt[ABOVE] >= 0);
    }
  } else if (diag_b2.type == BOTTLENECK) {
    if (from == FROM_ABOVE or from == FROM_RIGHT) {
      diag_b2.cnt[ABOVE]--;
      diag_b2.cnt[BELOW]++;
      assert(diag_b2.cnt[ABOVE] >= 0);
    }
  }
}

//#include "main.cpp"