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
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

typedef long long ll;

const int MAX=3030;
const int MOD = 1000000007;
const int B=5;

int n,k;
char T[MAX][MAX];
char txt[MAX];
int S_1[MAX][MAX];
int S_2[MAX][MAX][2];
int S_1_SZ=0;
int S_2_SZ=0;

ll res=0L;

int dx[] = {-1,0,1,0};
int dy[] = {0,-1,0,1};

int k4_1_1[4][2] = {{0,0},{1,0},{2,0},{3,0}};
int k4_1_2[4][2] = {{0,0},{0,1},{0,2},{0,3}};

int k4_2_1[4][2] = {{0,0},{1,-1},{1,0},{1,1}};
int k4_2_2[4][2] = {{0,0},{1,-1},{1,0},{2,0}};
int k4_2_3[4][2] = {{0,0},{0,1},{0,2},{1,1}};
int k4_2_4[4][2] = {{0,0},{1,0},{2,0},{1,1}};

int k4_3_1[4][2] = {{0,0},{1,0},{1,1},{2,1}};
int k4_3_2[4][2] = {{0,0},{1,-1},{1,0},{0,1}};
int k4_3_3[4][2] = {{0,0},{0,1},{1,1},{1,2}};
int k4_3_4[4][2] = {{0,0},{1,0},{1,-1},{2,-1}};

int k4_4_1[4][2] = {{0,0},{0,1},{0,2},{1,2}};
int k4_4_2[4][2] = {{0,0},{1,0},{2,0},{2,-1}};
int k4_4_3[4][2] = {{0,0},{1,0},{1,1},{1,2}};
int k4_4_4[4][2] = {{0,0},{0,1},{1,0},{2,0}};
int k4_4_5[4][2] = {{0,0},{1,-2},{1,-1},{1,0}};
int k4_4_6[4][2] = {{0,0},{0,1},{1,1},{2,1}};
int k4_4_7[4][2] = {{0,0},{1,0},{0,1},{0,2}};
int k4_4_8[4][2] = {{0,0},{1,0},{2,0},{2,1}};

int k4_5_1[4][2] = {{0,0},{0,1},{1,0},{1,1}};

int k3_1_1[3][2] = {{0,0},{1,0},{2,0}};
int B3_1_1[10][2] = {{0,-1},{1,-1},{2,-1},{0,1},{1,1},{2,1},{-1,0},{3,0}};
int k3_1_2[3][2] = {{0,0},{0,1},{0,2}};
int B3_1_2[10][2] = {{0,-1},{0,3},{-1,0},{-1,1},{-1,2},{1,0},{1,1},{1,2}};
int k3_2_1[3][2] = {{0,0},{1,-1},{1,0}};
int B3_2_1[10][2] = {{-1,0},{0,-1},{1,-2},{2,-1},{2,0},{1,1},{0,1}};
int k3_2_2[3][2] = {{0,0},{0,1},{1,1}};
int B3_2_2[10][2] = {{0,-1},{1,0},{2,1},{1,2},{0,2},{-1,1},{-1,0}};
int k3_2_3[3][2] = {{0,0},{1,0},{1,1}};
int B3_2_3[10][2] = {{0,-1},{1,-1},{2,0},{2,1},{1,2},{0,1},{-1,0}};
int k3_2_4[3][2] = {{0,0},{0,1},{1,0}};
int B3_2_4[10][2] = {{0,-1},{1,-1},{2,0},{1,1},{0,2},{-1,1},{-1,0}};

int k2_1[2][2] = {{0,0},{1,0}};
int B2_1[6][2] = {{-1,0},{2,0},{0,-1},{1,-1},{0,1},{1,1}};
int k2_2[2][2] = {{0,0},{0,1}};
int B2_2[6][2] = {{0,-1},{0,2},{-1,0},{-1,1},{1,0},{1,1}};

int W[10];
int WL[10];

void s4(int P[4][2])
{
    for(int i=B;i<n+B;++i)
    {
        for(int j=B;j<n+B;++j)
        {
            bool ok=true;
            for(int k=0;k<4;++k)
                ok &=(T[P[k][1]+i][P[k][0]+j]=='.');

            if (!ok) continue;

            int sa=0;
            for(int k=0;k<4;++k)
            {
                int x=P[k][0]+j;
                int y=P[k][1]+i;
                bool iss=false;
                for(int d=0;d<4;++d)
                    iss |= (T[y+dy[d]][x+dx[d]]=='#');
                sa+=iss;

            }
            if (sa>0&&sa<4)
            {
                res+=1;
                res%=MOD;
            }
        }
    }
}

void s3_1(int P[3][2], int Bn[10][2], int ib)
{
    for(int i=B;i<n+B;++i)
    {
        for(int j=B;j<n+B;++j)
        {
            bool ok=true;
            int dec=0;
            for(int k=0;k<3;++k)
                ok &=(T[P[k][1]+i][P[k][0]+j]=='.');

            if (!ok) continue;

            int sa=0;
            for(int k=0;k<3;++k)
            {
                int x=P[k][0]+j;
                int y=P[k][1]+i;
                dec += S_1[y][x];
                bool iss=false;
                for(int d=0;d<4;++d)
                    iss |= (T[y+dy[d]][x+dx[d]]=='#');
                sa+=iss;
            }
            if (sa>0&&sa<3)
            {
                for(int a=0;a<ib;++a)
                {
                    dec+=S_1[i+Bn[a][1]][j+Bn[a][0]];
                }
                res+=(S_1_SZ-dec);
                res%=MOD;
            }
        }
    }
}

void s2_2()
{
    ll S22=0L;
    for(int i=B;i<n+B;++i)
    {
        for(int j=B;j<n+B;++j)
        {
            if (!S_2[i][j][0]) continue;

            int dec=0;
            for(int a=-1;a<2;++a)
                for(int b=-1;b<2;++b)
                    dec+=S_2[b+i][a+j][0];
            dec+=S_2[i][j-2][0];
            dec+=S_2[i][j+2][0];
            for(int a=0;a<2;++a)
                for(int b=-2;b<2;++b)
                    dec+=S_2[b+i][a+j][1];

            S22+=S_2_SZ-dec;
            S22%=MOD;
        }
    }

    for(int i=B;i<n+B;++i)
    {
        for(int j=B;j<n+B;++j)
        {
            if (!S_2[i][j][1]) continue;

            int dec=0;
            for(int a=-2;a<2;++a)
                for(int b=-1;b<3;++b)
                    dec+=S_2[i+b][j+a][0];
            dec-=S_2[-1+i][-2+j][0];
            dec-=S_2[-1+i][1+j][0];
            dec-=S_2[2+i][-2+j][0];
            dec-=S_2[2+i][1+j][0];

            for(int a=-1;a<2;++a)
                for(int b=-1;b<2;++b)
                    dec+=S_2[b+i][a+j][1];
            dec+=S_2[-2+i][j][1];
            dec+=S_2[2+i][j][1];

            S22+=S_2_SZ-dec;
            S22%=MOD;
        }
    }
    S22/=2;
    res+=S22;
    res%=MOD;
}

void s2_1_1(int w, int k[2][2], int Bn[6][2])
{
    for(int i=B;i<n+B;++i)
    {
        for(int j=B;j<n+B;++j)
        {
            if (!S_2[i][j][w]) continue;
            int dec=S_1[i][j]+S_1[i+k[1][1]][j+k[1][0]];

            for(int a=0;a<6;++a)
                dec+=S_1[i+Bn[a][1]][j+Bn[a][0]];

            ll ta=(ll)(S_1_SZ - dec)*(S_1_SZ - dec - 1)/2;
            res+=ta;
            res%=MOD;
        }
    }
}

void s1s(int C)
{
    for(int i=0;i<C;++i)
    {
        int p=S_1_SZ-i;
        for(int j=C;j>=2;--j)
        {
            if(!W[j] && p%j==0)
            {
                p/=j;
                W[j]=1;
            }
        }
        WL[i]=p;
    }
    ll ta = WL[0];
    for(int i=1;i<C;++i)
    {
        ta*=WL[i];
        ta%=MOD;
    }
    res+=ta;
    res%=MOD;
}

void s3(int P[3][2])
{
    for(int i=B;i<n+B;++i)
    {
        for(int j=B;j<n+B;++j)
        {
            bool ok=true;
            for(int k=0;k<3;++k)
                ok &=(T[P[k][1]+i][P[k][0]+j]=='.');

            if (!ok) continue;

            int sa=0;
            for(int k=0;k<3;++k)
            {
                int x=P[k][0]+j;
                int y=P[k][1]+i;
                bool iss=false;
                for(int d=0;d<4;++d)
                    iss |= (T[y+dy[d]][x+dx[d]]=='#');
                sa+=iss;
            }
            if (sa>0&&sa<3)
            {
                res+=1;
                res%=MOD;
            }
        }
    }
}

void s2_1(int w, int k[2][2], int Bn[6][2])
{
    for(int i=B;i<n+B;++i)
    {
        for(int j=B;j<n+B;++j)
        {
            if (!S_2[i][j][w]) continue;
            int dec=S_1[i][j]+S_1[i+k[1][1]][j+k[1][0]];

            for(int a=0;a<6;++a)
                dec+=S_1[i+Bn[a][1]][j+Bn[a][0]];

            ll ta=(ll)(S_1_SZ - dec);
            res+=ta;
            res%=MOD;
        }
    }
}

void s2()
{
    res+=S_2_SZ;
    res%=MOD;
}

void s2C(int w, int P[2][2])
{
    for(int i=B;i<n+B;++i)
    {
        for(int j=B;j<n+B;++j)
        {
            bool ok=true;
            for(int k=0;k<2;++k)
                ok &=(T[P[k][1]+i][P[k][0]+j]=='.');

            if (!ok) continue;

            int sa=0;
            for(int k=0;k<2;++k)
            {
                int x=P[k][0]+j;
                int y=P[k][1]+i;
                bool iss=false;
                for(int d=0;d<4;++d)
                    iss |= (T[y+dy[d]][x+dx[d]]=='#');
                sa+=iss;
            }
            if (sa==1)
            {
                S_2[i][j][w]=1;
                ++S_2_SZ;
            }
        }
    }
}

void s1C()
{
    for(int i=B;i<n+B;++i)
    {
        for(int j=B;j<n+B;++j)
        {
            if (T[i][j]=='.')
            {   bool ok=false;
                for(int d=0;d<4;++d)
                    ok |=(T[i+dy[d]][j+dx[d]]=='#');
                if (ok)
                {
                    S_1[i][j]=1;
                    ++S_1_SZ;
                }

            }
        }
    }
}

int main()
{

    scanf("%d%d",&n,&k);
    for(int i=0;i<n+2*B;++i)
        for(int j=0;j<n+2*B;++j)
            T[i][j]='A';
    for(int i=0;i<n;++i)
    {
        scanf("%s",txt);
        for(int j=0;j<n;++j)
            T[i+B][j+B]=txt[j];
    }

    s1C();
    if (k>1)
    {
        s2C(0, k2_1);
        s2C(1, k2_2);
    }

    if (k==4)
    {
        s4(k4_1_1);
        s4(k4_1_2);
        s4(k4_2_1);
        s4(k4_2_2);
        s4(k4_2_3);
        s4(k4_2_4);

        s4(k4_3_1);

        s4(k4_3_2);
        s4(k4_3_3);
        s4(k4_3_4);
        s4(k4_4_1);
        s4(k4_4_2);
        s4(k4_4_3);
        s4(k4_4_4);
        s4(k4_4_5);

        s4(k4_4_6);


        s4(k4_4_7);
        s4(k4_4_8);
        s4(k4_5_1);

        s3_1(k3_1_1, B3_1_1, 8);
        s3_1(k3_1_2, B3_1_2, 8);
        s3_1(k3_2_1, B3_2_1, 7);
        s3_1(k3_2_2, B3_2_2, 7);
        s3_1(k3_2_3, B3_2_3, 7);
        s3_1(k3_2_4, B3_2_4, 7);

        s2_2();

        s2_1_1(0,k2_1,B2_1);
        s2_1_1(1,k2_2,B2_2);
        s1s(4);

    }
    else if(k==3)
    {
        s3(k3_1_1);
        s3(k3_1_2);
        s3(k3_2_1);
        s3(k3_2_2);
        s3(k3_2_3);
        s3(k3_2_4);
        //cout << res << endl;

        s2_1(0,k2_1,B2_1);
        //cout << res << endl;
        s2_1(1,k2_2,B2_2);
        //cout << res << endl;
        s1s(3);
        //cout << S_2_SZ << endl;
    }
    else if(k==2)
    {
        s2();
        s1s(2);
    }
    else
    {
        s1s(1);
    }

    cout << res << endl;

    return 0;
}