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
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include <map>
#include <cassert>
#include <unordered_map>
#define PII pair<int, int>
#define x first
#define y second
const int NORTH = 3;
const int SOUTH = 2;
const int WEST = 1;
const int EAST = 0;

using namespace std;
const int MOD = 1e9 + 7;
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
int inv[10];

bool in_vicinity[3005][3005];

vector<string> one = {"#"};

vector<vector<string>> two = {
    {"##"},
    {"#",
     "#"}
};

vector<vector<string>> three = {
    {"###"},
    {"#",
     "#",
     "#"},
    {".#",
     "##"},
    {"#.",
     "##"},
    {"##",
     ".#"},
    {"##",
     "#."}
};

vector<vector<string>> four = {
    {"####"},
    {"#",
     "#",
     "#",
     "#"},
    {"##",
     "##"},
    {"###",
     "#.."},
    {"###",
     ".#."},
    {"###",
     "..#"},
    {"#..",
     "###"},
    {".#.",
     "###"},
    {"..#",
     "###"},
    {"##",
     "#.",
     "#."},
    {"#.",
     "##",
     "#."},
    {"#.",
     "#.",
     "##"},
    {"##",
     ".#",
     ".#"},
    {".#",
     "##",
     ".#"},
    {".#",
     ".#",
     "##"},
    {"##.",
     ".##"},
    {".##",
     "##."},
    {"#.",
     "##",
     ".#"},
    {".#",
     "##",
     "#."}
};

int n;
int total_vicinity;
vector<string> a;
int sum[3001][3001];

void add(int &a, int b) {
    a += b;
    while(a >= MOD)
        a -= MOD;
    while(a < 0)
        a += MOD;
}

pair<int, int> getCell(int x, int y, int dir) {
    int newX = x + dx[dir];
    int newY = y + dy[dir];
    if(newX < 0 or newX >= n or newY < 0 or newY >= n)
        return {-1, -1};
    return {newX, newY};
}

int getSum(int lower_x, int lower_y, int upper_x, int upper_y) {
    lower_x = max(lower_x, 0);
    lower_y = max(lower_y, 0);
    upper_x = min(upper_x, n - 1);
    upper_y = min(upper_y, n - 1);
    
    lower_x += 1, lower_y += 1;
    upper_x += 1, upper_y += 1;

    int ans = sum[upper_x][upper_y] 
            - sum[lower_x - 1][upper_y] 
            - sum[upper_x][lower_y - 1] 
            + sum[lower_x - 1][lower_y - 1];
    return ans;
}

int countNearby(int x, int y, vector<string> &pattern) {
	int ans = getSum(x - 1, y - 1, x + pattern.size(), y + pattern[0].size());
    int up_x = x + pattern.size(), up_y = y + pattern[0].size();

    for(int i : {x - 1, up_x})
        for(int j : {y - 1, up_y})
            if(i >= 0 and i < n and j >= 0 and j < n)
                ans -= in_vicinity[i][j];

    for(size_t i = 0; i < pattern.size(); ++i)
        for(size_t j = 0; j < pattern[0].size(); ++j)
            if(pattern[i][j] == '.') {
                for(int dir = 0; dir < 4; ++dir) {
                    int new_i = i + dx[dir];
                    int new_j = j + dy[dir];
                    if(new_i >= int(pattern.size()) or new_j >= int(pattern[0].size()) or new_i < 0 or new_j < 0) {
                        int new_x = x + new_i;
                        int new_y = y + new_j;
                        if(new_x >= 0 and new_y >= 0 and new_x < n and new_y < n) {
                            ans -= in_vicinity[new_x][new_y];
                        }
                    }
                }
            }
    return ans;
}

int fits(int x, int y, vector<string> &pattern, bool strict) {
    if(x + int(pattern.size()) - 1 >= n or y + int(pattern[0].size()) - 1 >= n)
        return 0;

    bool some = false;
    for(size_t i = 0; i < pattern.size(); ++i)
        for(size_t j = 0; j < pattern[0].size(); ++j) {
            if(pattern[i][j] == '#') {
                if(a[x + i][y + j] == '#')
                    return 0;
                if(in_vicinity[x + i][y + j]) {
                    some = true;
                } else if(strict) {
                    return 0;
                }
            }
        }

    if(some)
        return 1;
    return 0;
}

int info_fits(int x, int y, vector<string> &pattern) {
    if(x + int(pattern.size()) - 1 >= n or y + int(pattern[0].size()) - 1 >= n)
        return 0;

    bool some = false, all = true;

    for(size_t i = 0; i < pattern.size(); ++i)
        for(size_t j = 0; j < pattern[0].size(); ++j) {
            if(pattern[i][j] == '#') {
                if(a[x + i][y + j] == '#')
                    return 0;
                if(in_vicinity[x + i][y + j]) {
                    some = true;
                } else {
                    all = false;
                }
            }
        }
    
    if(all)
        return 2;
    if(some)
        return 1;
    return 0;
}

int solveOne() {
    return total_vicinity;
}

int solveTwo() {
    int ans = 0;
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < n; ++j) {
            if(not in_vicinity[i][j])
                continue;
            int sub = countNearby(i, j, one);
            add(ans, total_vicinity - sub);
        }
    }

    ans = 1LL * ans * inv[2] % MOD;

    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < n; ++j) {
            for(auto& pattern : two) {
                add(ans, fits(i, j, pattern, false));
            }
        }
    }

    return ans;
}

int solveThree() {
    int ans = 0;

    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < n; ++j) {
            for(auto& pattern : three) {
                add(ans, fits(i, j, pattern, false));
            }
        }
    }
    
    cerr << "1\n";

    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < n; ++j) {
            for(auto& pattern : two) {
                if(fits(i, j, pattern, false)) {
                    int sub = countNearby(i, j, pattern);
                    add(ans, total_vicinity - sub);
                }
            }
        }
    }
    
    cerr << "2\n";

    int temp = 0;
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < n; ++j) {
            if(not in_vicinity[i][j])
                continue;
            int sub = countNearby(i, j, one);
            int pairs = (1LL * (total_vicinity - sub) * (total_vicinity - sub - 1) / 2) % MOD;
            add(temp, pairs);
        }
    }
        
    cerr << "3\n";

    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < n; ++j) {
            for(auto& pattern : two) {
                if(fits(i, j, pattern, true)) {
                    int sub = countNearby(i, j, pattern);
                    add(temp, -(total_vicinity - sub));
                }
            }
        }
    }

    temp = 1LL * temp * inv[3] % MOD;
    add(ans, temp);

    return ans;
}

bool fd[3005][3005][2];
bool fd_tight[3005][3005][2];

int countNearbyDominoes(int x, int y, int k, bool mat[3005][3005][2]) {
    int ans = 0;
    if(k == 0) {
        if(y - 2 >= 0) 
            ans += mat[x][y - 2][0];
        if(y - 1 >= 0) 
            ans += mat[x + 1][y - 1][0];
        if(x - 2 >= 0) { 
            ans += mat[x - 2][y][1];
            ans += mat[x - 2][y + 1][1];
        }
        if(x - 1 >= 0)  
            ans += mat[x - 1][y + 2][1];
        if(x - 1 >= 0 and y - 1 >= 0) 
            ans += mat[x - 1][y - 1][0] + mat[x - 1][y - 1][1];
        if(x - 1 >= 0) 
            ans += mat[x - 1][y][0] + mat[x - 1][y][1] + mat[x - 1][y + 1][0] + mat[x - 1][y + 1][1];
        if(y - 1 >= 0)
            ans += mat[x][y - 1][0] + mat[x][y - 1][1];
        ans += mat[x][y][0] + mat[x][y][1] + mat[x][y + 1][0] + mat[x][y + 1][1];
        ans += mat[x][y + 2][0] + mat[x][y + 2][1];
        ans += mat[x + 1][y][0] + mat[x + 1][y][1] + mat[x + 1][y + 1][0] + mat[x + 1][y + 1][1];
    } else {
        if(x - 2 >= 0)
            ans += mat[x - 2][y][1];
        if(x - 1 >= 0)
            ans += mat[x - 1][y + 1][1];
        if(y - 2 >= 0) 
            ans += mat[x][y - 2][0] + mat[x + 1][y - 2][0];
        if(y - 1 >= 0)
            ans += mat[x + 2][y - 1][0];
        if(x - 1 >= 0)
            ans += mat[x - 1][y][0] + mat[x - 1][y][1];
        if(x - 1 >= 0 and y - 1 >= 0)
            ans += mat[x - 1][y - 1][0] + mat[x - 1][y - 1][1];
        if(y - 1 >= 0) {
            ans += mat[x][y - 1][0] + mat[x][y - 1][1];
            ans += mat[x + 1][y - 1][0] + mat[x + 1][y - 1][1];
        }
        ans += mat[x][y][0] + mat[x][y][1] + mat[x + 1][y][0] + mat[x + 1][y][1];
        ans += mat[x + 2][y][0] + mat[x + 2][y][1];
        ans += mat[x][y + 1][0] + mat[x][y + 1][1] + mat[x + 1][y + 1][0] + mat[x + 1][y + 1][1];
    }

    return ans;
}

int mem[2][100000];

int solveFour() {
    int ans = 0;

    for(int i = 0; i < n; ++i) 
        for(int j = 0; j < n; ++j) {
            for(int k = 0; k < 3; ++k) {
                add(ans, fits(i, j, four[k], false));
            }
            int lines = 2, columns = 3;
            for(int it = 0; it < 2; ++it) {
                if(i + lines - 1 >= n or j + columns - 1 >= n) {
                    swap(lines, columns);
                    continue;
                }
                int mask_matrix = 0;
                int mask_in = 0;
                int p = 1;
                for(int dx = i; dx <= i + lines - 1; dx += 1)
                    for(int dy = j; dy <= j + columns - 1; dy += 1) {
                        if(a[dx][dy] == '#')
                            mask_matrix += p;
                        if(in_vicinity[dx][dy])
                            mask_in += p;
                        p *= 2;
                    }
                add(ans, mem[it][(mask_matrix << 6) + mask_in]);
                swap(lines, columns);
            }
        }
    
    cerr << "After one piece: " << ans << "\n";

    int three_tight = 0;
    for(int i = 0; i < n; ++i)
        for(int j = 0; j < n; ++j)
            for(auto& pattern : three) {
                int res = info_fits(i, j, pattern);
                if(res >= 1) {
                    int sub = countNearby(i, j, pattern);
                    add(ans, (total_vicinity - sub));
                    if(res == 2) {
                        add(three_tight, total_vicinity - sub);
                    }
                }
            }

    int total_dominoes = 0;

    for(int i = 0; i < n; ++i)
        for(int j = 0; j < n; ++j)
            for(int k = 0; k < 2; ++k)
                if(fits(i, j, two[k], false)) {
                    fd[i][j][k] = 1;
                    total_dominoes += 1;                     
                }
    
    int temp = 0;
    cerr << "Total dominoes: " << total_dominoes << "\n";

    for(int i = 0; i < n; ++i)
        for(int j = 0; j < n; ++j)
            for(int k = 0; k < 2; ++k) 
                if(fd[i][j][k]) {
                    int sub = countNearbyDominoes(i, j, k, fd);
                    add(temp, total_dominoes - sub);
                }

    temp = 1LL * temp * inv[2] % MOD;
    add(ans, temp);
    cerr << "After two pieces: " << ans << "\n";

    temp = 0;
    
    for(int i = 0; i < n; ++i)
        for(int j = 0; j < n; ++j) 
            for(int k = 0; k < 2; ++k) 
                if(fd[i][j][k]) {
                    int sub = countNearby(i, j, two[k]);
                    int term = (1LL * (total_vicinity - sub) * (total_vicinity - sub - 1) / 2) % MOD;
                    add(temp, term);
                }
    
    int total_tight_dominoes = 0;
    int two_tight_one_one = 0;

    for(int i = 0; i < n; ++i) 
        for(int j = 0; j < n; ++j) 
            for(int k = 0; k < 2; ++k) 
                if(fits(i, j, two[k], true)) {
                    fd_tight[i][j][k] = 1;
                    total_tight_dominoes += 1;
                    int sub = countNearby(i, j, two[k]);
                    int term = (1LL * (total_vicinity - sub) * (total_vicinity - sub - 1) / 2) % MOD;
                    add(two_tight_one_one, term);
                }
    
    int loose_tight_dominoes = 0;
    int tight_tight_dominoes = 0;

    for(int i = 0; i < n; ++i) 
        for(int j = 0; j < n; ++j)
            for(int k = 0; k < 2; ++k)
                if(fd[i][j][k]) {
                    int sub = countNearbyDominoes(i, j, k, fd_tight);
                    add(loose_tight_dominoes, total_tight_dominoes - sub);
                    if(fd_tight[i][j][k]) {
                        add(tight_tight_dominoes, total_tight_dominoes - sub);
                    }
                }
    add(temp, -loose_tight_dominoes);
    add(ans, temp);
    
    auto triplets = [&] (int x) {
        int ans = 1LL * x * (x - 1) % MOD;
        ans = 1LL * ans * (x - 2) % MOD;
        ans = 1LL * ans * inv[6] % MOD;
        return ans;
    };
    
    cerr << "After 3 pieces: " << ans << "###\n";
    /// FOUR PIECES BELOW.
    
    temp = 0;

    for(int i = 0; i < n; ++i)
        for(int j = 0; j < n; ++j)
            if(in_vicinity[i][j]) {
                int sub = countNearby(i, j, one);
                add(temp, triplets(total_vicinity - sub));
            }
    
    add(temp, -three_tight);

    add(two_tight_one_one, -tight_tight_dominoes);
    two_tight_one_one = 2LL * two_tight_one_one % MOD;
    add(temp, -two_tight_one_one);

    temp = 1LL * temp * inv[4] % MOD;
    add(ans, temp);
    
    return ans;
}

int compute(int mat, int in, string line) {
    int n = line.size();
    bool some = false;
    
    for(int i = 0; i < n; ++i)
        if(line[i] == '#' and ((1 << i) & mat))
            return 0;
    for(int i = 0; i < n; ++i)
        if(line[i] == '#' and ((1 << i) & in))
            some = true;

    if(some)
        return 1;
    return 0;
}

int main() {
    //ifstream cin("car.in");
    int k; cin >> n >> k;
    a = vector<string>(n);
        
    inv[1] = 1;
    for(int i = 2; i < 10; ++i) {
        inv[i] = (MOD - 1LL * (MOD / i) * inv[MOD % i] % MOD);
    }

    for(int i = 0; i < n; ++i)
        cin >> a[i]; 

    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < n; ++j) {
            sum[i + 1][j + 1] += sum[i][j + 1] + sum[i + 1][j];
            sum[i + 1][j + 1] -= sum[i][j];
            if(a[i][j] == '#') 
                continue;
            bool has = false;
            for(int dir = 0; dir < 4; ++dir) {
                auto cell = getCell(i, j, dir);
                if(cell.x >= 0 and a[cell.x][cell.y] == '#') {
                    has = true;
                    break;
                }
            }
            if(has) {
                in_vicinity[i][j] = 1;
                total_vicinity += 1; 
                sum[i + 1][j + 1] += 1;
            }
        }
   } 
    
    for(int mask = 0; mask < (1 << 12); ++mask) {
        int matrix_mask = (mask >> 6);
        int in_mask = mask - (matrix_mask << 6);
        for(int k = 3; k < 19; ++k) {
            auto pattern = four[k];
            if(pattern.size() == 2) {
                string line = pattern[0] + pattern[1];
                mem[0][mask] += compute(matrix_mask, in_mask, line);
            } else if(pattern.size() == 3) {
                string line = pattern[0] + pattern[1] + pattern[2];
                mem[1][mask] += compute(matrix_mask, in_mask, line);
            } else {
                assert(false);
            }
        }
    }
    
    if(k == 1) {
        cout << solveOne() << "\n";
    } else if(k == 2) {
        cout << solveTwo() << "\n";
    } else if(k == 3) {
        cout << solveThree() << "\n";
    } else {
        cout << solveFour() << "\n";
    }
}