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
#include <algorithm>
#include <stdint.h>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <time.h>
using namespace std;

/***************************************************************************/

#define FACTOR 1000000007

int n, k;
char M[3000][3001];
vector<vector<int8_t> > C;
vector<vector<int8_t> > block_12_map;
uint64_t block_12_count, count_1;

/***************************************************************************/

int8_t get(int r, int c) { return (r<0 || r>n-1 || c<0 || c>n-1 ? -2 : C[r][c]); }
int8_t getU(int r, int c) { return get(r-1, c); }
int8_t getB(int r, int c) { return get(r+1, c); }
int8_t getL(int r, int c) { return get(r, c-1); }
int8_t getR(int r, int c) { return get(r, c+1); }

uint64_t comb(uint64_t N, uint8_t K) {
    if (K == 0 || K == N) return 1;
    if (K > N) return 0;
    if (K == 1) return N % FACTOR;
    uint64_t result_1 = (N * (N - 1)) / 2;
    if (K == 2) return result_1 % FACTOR;
    uint64_t result_2 = N - 2;
    if (result_1 % 3 == 0) result_1 /= 3; else result_2 /= 3;
    if (K == 4) {
        result_2 = result_2 * (N - 3);
        if (result_1 % 2 == 0) result_1 /= 2; else result_2 /= 2;
        if (result_1 % 2 == 0) result_1 /= 2; else result_2 /= 2;
    }
    return ((result_1 % FACTOR)*(result_2 % FACTOR)) % FACTOR;
}

uint64_t solve1() {
    return count_1 % FACTOR;
}

uint64_t solve11() {
    return comb(count_1, 2);
}

uint64_t solve111() {
    return comb(count_1, 3);
}

uint64_t solve1111() {
    return comb(count_1, 4);
}

uint64_t solve12() {
    uint64_t result = 0;
    for(int r=0; r<n; ++r) for (int c=0; c<n; ++c){
        if (get(r, c) == 2) {
            if (getL(r, c) == 1) ++result;
            if (getR(r, c) == 1) ++result;
            if (getU(r, c) == 1) ++result;
            if (getB(r, c) == 1) ++result;
        }
    }
    return result % FACTOR;
}

uint64_t solve112() {
    uint64_t result = 0;

    for(int r=0; r<n; ++r) for (int c=0; c<n; ++c){
        if (get(r, c) == 2) {
            uint8_t L1 = 0;
            if (getL(r, c) == 1) ++L1;
            if (getR(r, c) == 1) ++L1;
            if (getU(r, c) == 1) ++L1;
            if (getB(r, c) == 1) ++L1;
            result = (result + comb(L1, 1)*comb(count_1 - L1, 1)) % FACTOR;
            result = (result + comb(L1, 2)*comb(count_1 - L1, 0)) % FACTOR;
        }
    }
    return result % FACTOR;
}

uint64_t solve122() {
    uint64_t result = 0;
    for(int r=0; r<n; ++r) for (int c=0; c<n; ++c){

        // Case 1)
        if (get(r, c) == 2) {
            uint8_t L1 = 0, L2 = 0;
            if (getL(r, c) == 1) ++L1;
            if (getR(r, c) == 1) ++L1;
            if (getU(r, c) == 1) ++L1;
            if (getB(r, c) == 1) ++L1;
            if (getL(r, c) == 2) ++L2;
            if (getR(r, c) == 2) ++L2;
            if (getU(r, c) == 2) ++L2;
            if (getB(r, c) == 2) ++L2;
            result = (result + L1 * L2) % FACTOR;
        }

        // Case 2)
        if (get(r, c) == 1) {
            uint8_t L2 = 0;
            if (getL(r, c) == 2) ++L2;
            if (getR(r, c) == 2) ++L2;
            if (getU(r, c) == 2) ++L2;
            if (getB(r, c) == 2) ++L2;
            if (L2 >= 2) result = (result + comb(L2, 2)) % FACTOR;
        }
    }

    return result;
}

uint64_t solve123() {
    uint64_t result = 0;
    for(int r=0; r<n; ++r) for (int c=0; c<n; ++c){
        if (get(r, c) == 2) {
            uint8_t L1 = 0, L3 = 0;
            if (getL(r, c) == 1) ++L1;
            if (getR(r, c) == 1) ++L1;
            if (getU(r, c) == 1) ++L1;
            if (getB(r, c) == 1) ++L1;
            if (getL(r, c) == 3) ++L3;
            if (getR(r, c) == 3) ++L3;
            if (getU(r, c) == 3) ++L3;
            if (getB(r, c) == 3) ++L3;
            result = (result + L1 * L3) % FACTOR;
        }
    }
    return result;
}

uint64_t solve1112() {
    uint64_t result = 0;

    for(int r=0; r<n; ++r) for (int c=0; c<n; ++c){
        if (get(r, c) == 2) {
            uint8_t L1 = 0;
            if (getL(r, c) == 1) ++L1;
            if (getR(r, c) == 1) ++L1;
            if (getU(r, c) == 1) ++L1;
            if (getB(r, c) == 1) ++L1;
            result = (result + comb(L1, 1) * comb(count_1 - L1, 2)) % FACTOR;
            result = (result + comb(L1, 2) * comb(count_1 - L1, 1)) % FACTOR;
            result = (result + comb(L1, 3) * comb(count_1 - L1, 0)) % FACTOR;
        }
    }

    return result % FACTOR;
}

#define BASE (uint64_t)(2000000000000000000)
uint64_t solve1122() {
    uint64_t result = 0;

    // Case [1|122]
    for(int r=0; r<n; ++r) for (int c=0; c<n; ++c) {

        // Case: both '2' connected with '1'
        if (get(r, c) == 1) {
            int8_t L1 = 0, R1 = 0, U1 = 0, B1 = 0, LU = 0, LB = 0, RU = 0, RB = 0;
            if (getL(r, c) == 2) {
                if (getL(r, c-1) == 1) ++L1;
                if (getU(r, c-1) == 1) { ++L1; LU = 1; }
                if (getB(r, c-1) == 1) ++L1;
            }
            if (getR(r, c) == 2) {
                if (getR(r, c+1) == 1) ++R1;
                if (getU(r, c+1) == 1) { ++R1; RU = 1; }
                if (getB(r, c+1) == 1) ++R1;
            }
            if (getU(r, c) == 2) {
                if (getU(r-1, c) == 1) ++U1;
                if (getL(r-1, c) == 1) ++U1;
                if (getR(r-1, c) == 1) ++U1;
            }
            if (getB(r, c) == 2) {
                if (getB(r+1, c) == 1) ++B1;
                if (getL(r+1, c) == 1) { ++B1; LB = 1; }
                if (getR(r+1, c) == 1) { ++B1; RB = 1; }
            }

            if (getL(r, c) == 2 && getR(r, c) == 2) result = (result + count_1 - 1 - L1 - R1) % FACTOR;
            if (getU(r, c) == 2 && getB(r, c) == 2) result = (result + count_1 - 1 - U1 - B1) % FACTOR;
            if (getL(r, c) == 2 && getU(r, c) == 2) result = (result + count_1 + LU - 1 - L1 - U1) % FACTOR;
            if (getR(r, c) == 2 && getU(r, c) == 2) result = (result + count_1 + RU - 1 - R1 - U1) % FACTOR;
            if (getL(r, c) == 2 && getB(r, c) == 2) result = (result + count_1 + LB - 1 - L1 - B1) % FACTOR;
            if (getR(r, c) == 2 && getB(r, c) == 2) result = (result + count_1 + RB - 1 - R1 - B1) % FACTOR;
        }

        // Case: second '2' connected with '2'
        if (get(r, c) == 2) { // First '2'
            uint8_t L1 = 0, L2 = 0;
            if (getL(r, c) == 1) ++L1;
            if (getR(r, c) == 1) ++L1;
            if (getU(r, c) == 1) ++L1;
            if (getB(r, c) == 1) ++L1;
            if (getL(r, c) == 2) ++L2;
            if (getR(r, c) == 2) ++L2;
            if (getU(r, c) == 2) ++L2;
            if (getB(r, c) == 2) ++L2;

            if (L1 >= 2) result = (result + comb(L1, 2) * (uint64_t)L2) % FACTOR;

            if (getR(r, c) == 2) {
                uint8_t extra = 0;
                if (getR(r, c+1) == 1) ++extra;
                if (getU(r, c+1) == 1) ++extra;
                if (getB(r, c+1) == 1) ++extra;
                result = (result + (uint64_t)(count_1 - (uint64_t)L1 - (uint64_t)extra) * (uint64_t)L1) % FACTOR;
            }

            if (getL(r, c) == 2) {
                uint8_t extra = 0;
                if (getL(r, c-1) == 1) ++extra;
                if (getU(r, c-1) == 1) ++extra;
                if (getB(r, c-1) == 1) ++extra;
                result = (result + (uint64_t)(count_1 - (uint64_t)L1 - (uint64_t)extra) * (uint64_t)L1) % FACTOR;
            }

            if (getU(r, c) == 2) {
                uint8_t extra = 0;
                if (getU(r-1, c) == 1) ++extra;
                if (getL(r-1, c) == 1) ++extra;
                if (getR(r-1, c) == 1) ++extra;
                result = (result + (uint64_t)(count_1 - (uint64_t)L1 - (uint64_t)extra) * (uint64_t)L1) % FACTOR;
            }

            if (getB(r, c) == 2) {
                uint8_t extra = 0;
                if (getB(r+1, c) == 1) ++extra;
                if (getL(r+1, c) == 1) ++extra;
                if (getR(r+1, c) == 1) ++extra;
                result = (result + (uint64_t)(count_1 - (uint64_t)L1 - (uint64_t)extra) * (uint64_t)L1) % FACTOR;
            }

        }
    }

    // Case [12|12]
    uint64_t result_12_12 = 0;
    uint64_t extra = 0;
    for(int r=0; r<n; ++r) for (int c=0; c<n; ++c) {
        if ((get(r,c) == 1 && getR(r,c) == 2) || (get(r,c) == 2 && getR(r,c) == 1)) {
            uint64_t devastated = block_12_map[r][c] + block_12_map[r][c+1] - 1;
            if (get(r+1,c) == get(r,c+1) && get(r,c) == get(r+1,c+1)) ++devastated;
            result_12_12 = (result_12_12 + (uint64_t)block_12_count - devastated);
            extra += result_12_12 / BASE;
            result_12_12 = result_12_12 % BASE;
        }
        if ((get(r,c) == 1 && getB(r,c) == 2) || (get(r,c) == 2 && getB(r,c) == 1)) {
            uint64_t devastated = block_12_map[r][c] + block_12_map[r+1][c] - 1;
            if (get(r+1,c) == get(r,c+1) && get(r,c) == get(r+1,c+1)) ++devastated;
            result_12_12 = (result_12_12 + (uint64_t)block_12_count - devastated);
            extra += result_12_12 / BASE;
            result_12_12 = result_12_12 % BASE;
        }
    }
    result_12_12 = (((extra%FACTOR)*((BASE/2)%FACTOR))%FACTOR + result_12_12/2)%FACTOR;
    result = (result + result_12_12) % FACTOR;
    return result;
}

uint64_t solve1123() {
    uint64_t result = 0;

    for(int r=0; r<n; ++r) for (int c=0; c<n; ++c){
        if (get(r, c) == 2) {
            uint8_t L1 = 0, L3 = 0;
            if (getL(r, c) == 1) ++L1;
            if (getR(r, c) == 1) ++L1;
            if (getU(r, c) == 1) ++L1;
            if (getB(r, c) == 1) ++L1;
            if (getL(r, c) == 3) ++L3;
            if (getR(r, c) == 3) ++L3;
            if (getU(r, c) == 3) ++L3;
            if (getB(r, c) == 3) ++L3;
            result = (result + (uint64_t)(count_1 - L1) * (uint64_t)L1 * (uint64_t)L3) % FACTOR;
            result = (result + comb(L1, 2) * (uint64_t)L3) % FACTOR;
        }
    }

    return result % FACTOR;
}

#define SHIFT(x) x+3
uint64_t result_1222, result_1223, result_1233, result_1234;
uint8_t X[9];
uint32_t *Xp;
void validate(int y, int x) {
    ++X[SHIFT(get(y, x))];
    if (X[4]==1) {
        switch (X[5]) {
            case 3: ++result_1222; break;
            case 2: if (X[6] == 1) ++result_1223; break;
            case 1:
                if (X[6] == 2) ++result_1233;
                else if (X[6] == 1 && X[7] == 1) ++result_1234;
                break;
        }
    }
    --X[SHIFT(get(y, x))];
}

uint64_t solve_local_4() {
    Xp = (uint32_t *)(X + 4);
    result_1222 = result_1223 = result_1233 = result_1234 = 0;
    *Xp = 0;

    for(int r=0; r<n; ++r) for (int c=0; c<n; ++c) {
        if (get(r,c) > 0) {
            ++X[SHIFT(get(r, c))];

            ++X[SHIFT(get(r+0, c+1))];
            ++X[SHIFT(get(r+0, c+2))];
            validate(r+0,c+3);   // Case 1
            --X[SHIFT(get(r+0, c+1))];
            --X[SHIFT(get(r+0, c+2))];

            if (get(r+1,c) > 0) {
                ++X[SHIFT(get(r+1, c+0))];
                if (get(r,c+1) > 0) {
                    ++X[SHIFT(get(r+0, c+1))];
                    validate(r+1,c+1);   // Case 3
                    validate(r-1,c+0);   // Case 7
                    validate(r+1,c-1);  // Case 9
                    validate(r+0,c+2);   // Case 15
                    validate(r+0,c-1);   // Case 5
                    --X[SHIFT(get(r+0, c+1))];
                }

                if (get(r+1,c+1) > 0) {
                    ++X[SHIFT(get(r+1, c+1))];
                    validate(r+1,c-1);   // Case 4
                    validate(r+0,c-1);   // Case 8
                    validate(r+2,c+1);   // Case 11
                    validate(r+1,c+2);   // Case 14
                    --X[SHIFT(get(r+1, c+1))];
                }

                if (get(r+1,c-1) > 0) {
                    ++X[SHIFT(get(r+1, c-1))];
                    validate(r+2,c-1);  // Case 10
                    validate(r+1,c-2);  // Case 12
                    --X[SHIFT(get(r+1, c-1))];
                }

                if (get(r+2,c) > 0) {
                    ++X[SHIFT(get(r+2, c+0))];
                    validate(r+1,c-1);  // Case 6
                    validate(r+3,c+0);   // Case 2
                    validate(r+2,c-1);  // Case 16
                    validate(r+2,c+1);   // Case 17
                    validate(r+0,c-1);  // Case 18
                    validate(r+0,c+1);   // Case 19
                    --X[SHIFT(get(r+2, c+0))];
                }

                ++X[SHIFT(get(r+0, c-1))];
                validate(r+0,c-2);  // Case 13
                --X[SHIFT(get(r+0, c-1))];

                --X[SHIFT(get(r+1, c+0))];
            }
            --X[SHIFT(get(r, c))];
        }
    }

    return (result_1222 + result_1223 + result_1233 + result_1234) % FACTOR;
}

/***************************************************************************/

int main() {
    scanf("%d %d", &n, &k);
    for(int i=0; i<n; ++i) scanf("%s", M[i]);

    C.resize(n, vector<int8_t>(n));
    for (int r=0; r<n; ++r) for (int c=0; c<n; ++c) C[r][c] = (M[r][c]=='#' ? 0 : -1);
    for (int i=1; i<=k; ++i) {
        for(int r=0; r<n; ++r) for (int c=0; c<n; ++c) {
            if (C[r][c] != -1) continue;
            if (r>0   && C[r-1][c]==i-1){ C[r][c]=i; continue; }
            if (r<n-1 && C[r+1][c]==i-1){ C[r][c]=i; continue; }
            if (c>0   && C[r][c-1]==i-1){ C[r][c]=i; continue; }
            if (c<n-1 && C[r][c+1]==i-1){ C[r][c]=i; continue; }
        }
    }

    count_1 = 0;
    block_12_count = 0;
    block_12_map.resize(n, vector<int8_t>(n, 0));
    for(int r=0; r<n; ++r) for (int c=0; c<n; ++c) {
        count_1 += (C[r][c] == 1 ? 1 : 0);
        if ((get(r,c) == 1 && getR(r,c) == 2) || (get(r,c) == 2 && getR(r,c) == 1)) { ++block_12_map[r][c]; ++block_12_map[r][c+1]; ++block_12_count; }
        if ((get(r,c) == 1 && getB(r,c) == 2) || (get(r,c) == 2 && getB(r,c) == 1)) { ++block_12_map[r][c]; ++block_12_map[r+1][c]; ++block_12_count; }
    }

    uint64_t result = 0;
    if      (k==1) result = solve1();
    else if (k==2) result = solve11() + solve12();
    else if (k==3) result = solve111() + solve112() + solve122() + solve123();
    else if (k==4) result =
        solve1111() +
        solve1112() +
        solve1122() +
        solve1123() +
        solve_local_4();

    printf("%llu\n", result % FACTOR);
}