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
#include <bits/stdc++.h>
#include<stdlib.h>
#include<time.h>
#include <unistd.h>
#define MP make_pair
#define PB push_back
#define st first
#define nd second
#define rd third
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define RE(i, n) FOR(i, 1, n)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
using namespace std;
template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
  while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
#define next ____next
#define prev ____prev
#define left ____left
#define hash ____hash
typedef long long ll;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<pair<int, int> > VPII;
typedef vector<pair<ll, ll> > VPLL;

template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
template<class T1, class T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class A, class B, class C> struct Triple { A first; B second; C third;
  bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } };
template<class T> void ResizeVec(T&, vector<int>) {}
template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
  vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
  for (T& v : vec) { ResizeVec(v, sz); }
}
typedef Triple<int, int, int> TIII;
template<class A, class B, class C>
ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; }
template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class T> ostream& operator<<(ostream& out, array<T, 4> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }

int di[] = {1, -1, 0, 0};
int dj[] = {0, 0, 1, -1};
int n, k;
inline bool OnBoard(int i, int j) {
  return i >= 1 && j >= 1 && i <= n && j <= n;
}
const int N = 3e3 + 5;
bool board[N][N];
//bool board[N * N];
bool d1[N][N];
bool d2[N][N];
//bool on_stack[N][N];
bool forb[N][N];
//int last_chk1[N][N];
const ll P = 1e9 + 7;
const ll inv2 = (P + 1) / 2;
const ll inv3 = (P + 1) / 3;
const ll inv4 = inv2 * inv2 % P;
int glob[6][6];
int to_unforb[6][100][2];
int phase = 0;
ll res;
ll c1, c2;
int Fix(ll a) {
  return (a % P + P) % P;
}
int goal;
int zium;
vector<int32_t> last_chk2;
int who2[N][N][4];
int bd;
int cnt4;
LD Tim() {
#ifndef LOCAL
  return 0;
#else
  return 1. * clock() / CLOCKS_PER_SEC;
#endif
}
//char buffer[(int)
int32_t main() {

  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(10);
  cerr << fixed << setprecision(10);
  cin.tie(0);
  //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC;
  
  //debug(sizeof(who2[0][0]));
  //debug(sizeof(array<int32_t, 2>));

  vector<VPII> patterns4{
    {{0, 0}, {0, 1}, {1, 0}, {1, 1}}, // kwadrat
    {{1, 0}, {1, 1}, {0, 1}, {0, 2}}, // S
    {{0, 0}, {0, 1}, {1, 0}, {2, 0}}, // L
    {{0, 0}, {1, 0}, {2, 0}, {1, 1}}, // T
    {{0, 0}, {1, 0}, {2, 0}, {3, 0}} // I
  };
  vector<VPII> patterns3basic{
    {{0, 0}, {1, 0}, {2, 0}},
    {{0, 0}, {1, 0}, {1, 1}}
  };
  set<VPII> patterns_set4;
  set<VPII> patterns_set3;
  auto TransformPat = [&](VPII cur, int mask) {
    if (mask & 1) {
      for (auto& p : cur) {
        swap(p.st, p.nd);
      }
    }
    if (mask & 2) {
      for (auto& p : cur) {
        p.st *= -1;
      }
    }
    if (mask & 4) {
      for (auto& p : cur) {
        p.nd *= -1;
      }
    }
    sort(ALL(cur));
    PII min_guy = cur[0];
    for (auto& p : cur) {
      p.st -= min_guy.st;
      p.nd -= min_guy.nd;
    }
    return cur;
  };
  for (auto pat : patterns4) {
    REP (mask, 8) {
      patterns_set4.insert(TransformPat(pat, mask));
    }
  }
  for (auto pat : patterns3basic) {
    REP (mask, 8) {
      patterns_set3.insert(TransformPat(pat, mask));
    }
  }
  debug(SZ(patterns_set4));
  debug(SZ(patterns_set3));
  vector<int> patterns_bits4;
  vector<int> patterns_bits3;
  for (auto pat : patterns_set4) {
    int mask = 0;
    for (auto p : pat) {
      mask += 1 << (p.st * 6 + p.nd + 2);
    }
    patterns_bits4.PB(mask);
  }
  vector<VPII> patterns3;
  for (auto pat : patterns_set3) {
    patterns3.PB(pat);
    int mask = 0;
    for (auto p : pat) {
      mask += 1 << (p.st * 6 + p.nd + 2);
    }
    patterns_bits3.PB(mask);
  }
  
  
#define TESTa
#ifdef TEST
  n = 3000;
  k = 4;
#else
  cin>>n>>k;
#endif
  FOR (i, 0, n + 1) {
    board[i][0] = board[i][n + 1] = board[0][i] = board[n + 1][i] = 1;
    forb[i][0] = forb[i][n + 1] = forb[0][i] = forb[n + 1][i] = 1;
    
  }
  
  debug(Tim());
  RE (i, n) {
    RE (j, n) {
#ifndef TEST
      char c;
      cin>>c;
      if (c == '#') {
        board[i][j] = 1;
        forb[i][j] = 1;
      }
#else
      forb[i][j] = board[i][j] = (i == 1 || (j % 4 == 1));
#endif
      
    }
  }
  debug(Tim(), "read");
  RE (i, n) {
    RE (j, n) {
      if (board[i][j]) {
      //if (board[i * N + j]) {
        REP (dir, 4) {
          int ni = i + di[dir];
          int nj = j + dj[dir];
          if (!board[ni][nj]) {
            if (!d1[ni][nj]) {
              c1++;
            }
            d1[ni][nj] = 1;
          }
        }
      }
    }
  }
  RE (i, n) {
    RE (j, n) {
      if (d1[i][j]) {
        REP (dir, 4) {
          int ni = i + di[dir];
          int nj = j + dj[dir];
          if (!board[ni][nj] && !d1[ni][nj]) {
            d2[ni][nj] = 1;
          }
        }
      }
    }
  }
  goal = 1;
  bd = 2;
  
  RE (i, n) {
    RE (j, n) {
      if (d2[i][j]) {
        glob[0][0] = i;
        glob[0][1] = j;
        REP (dir, 4) {
          int ni = i + di[dir];
          int nj = j + dj[dir];
          if (d1[ni][nj]) {
            c2++;
          }
        }
      }
    }
  }  
  int kryj = (1 << 24) - 1;
  REP (i, 4) {
    kryj -= (1 << (6 * i + 5));
  }
  memset(who2, -1, sizeof(int) * (N * N * 4 - 1));
  RE (i, n) {
    int mb = 0, m1 = 0;
    REP (di, 4) {
      REP (dj, 4) {
        if (!board[i + di][1 + dj]) {
          mb += 1 << (6 * di + 2 + dj);
        }
        if (d1[i + di][1 + dj]) {
          m1 += 1 << (6 * di + 2 + dj);
        }
      }
    }
    RE (j, n) {
      for (auto pat_mask : patterns_bits4) {
        int ib = mb & pat_mask;
        if (ib == pat_mask) {
          int i1 = m1 & pat_mask;
          if (i1 != 0 && i1 != pat_mask) {
            if (k == 4) {
              res++;
            }
          }
        }
      }
      REP (ii, SZ(patterns_bits3)) {
        auto pat_mask = patterns_bits3[ii];
        int ib = mb & pat_mask;
        if (ib == pat_mask) {
          int i1 = m1 & pat_mask;
          if (i1 != 0 && i1 != pat_mask) {
            if (k == 4) {
              res = res + c1;
              phase++;
              REP (p, 3) {
                REP (dir, 4) {
                  int ni = i + patterns3[ii][p].st + di[dir];
                  int nj = j + patterns3[ii][p].nd + dj[dir];
                  if (who2[ni][nj][0] != phase && d1[ni][nj]) {
                    who2[ni][nj][0] = phase;
                    res--;
                  }
                }
              }
            } else if (k == 3) {
              res++;
            } 
          }
        }
      }
      mb >>= 1;
      m1 >>= 1;
      mb &= kryj;
      m1 &= kryj;
      REP (di, 4) {
        if (!board[i + di][j + 4]) {
          mb += 1 << (6 * di + 5);
        }
        if (d1[i + di][j + 4]) {
          m1 += 1 << (6 * di + 5);
        }
      }
    }
  }
  memset(who2, -1, sizeof(int) * (N * N * 4 - 1));
  debug(Tim(), "bits");
        
  
  
  res = Fix(res);
  
  debug(Tim(), "1");
  if (k == 1) {
    cout<<c1<<endl;
    return 0;
  }
  if (k >= 2) {
    if (k == 2) {
      res += c1 * (c1 - 1) / 2;
      res %= P;
      res = (res + c2) % P;
      cout<<res<<endl;
      return 0;
    }
    if (k >= 3) {
      if (k == 3) {
        res = (res + c1 * (c1 - 1) % P * (c1 - 2) % P * inv2 % P * inv3) % P;
      }
      debug(Tim(), "2");
      if (k == 3) {
        res = (res + c2 * c1) % P;
        goal = 2;
              bd = 2;
        RE (i, n) {
          RE (j, n) {
            if (d2[i][j]) {
              glob[0][0] = i;
              glob[0][1] = j;
              REP (dir, 4) {
                int ni = i + di[dir];
                int nj = j + dj[dir];
                if (d1[ni][nj]) {
                  glob[1][0] = ni;
                  glob[1][1] = nj;
                  phase++;
                  REP (ii, 2) {
                    REP (dir, 4) {
                      int ni = glob[ii][0] + di[dir];
                      int nj = glob[ii][1] + dj[dir];
                      if (who2[ni][nj][0] != phase && d1[ni][nj]) {
                        who2[ni][nj][0] = phase;
                        res--;
                      }
                    }
                  }
                }
              }
            }
          }
        }
        res = Fix(res);
        cout<<res<<endl;
        return 0;
      }
      if (k == 4) {
        res = (res + c1 * (c1 - 1) % P * (c1 - 2) % P * (c1 - 3) % P * inv2 % P * inv3 % P * inv4) % P;
        debug(Tim(), "3");
        res = Fix(res);
        RE (i, n) {
          RE (j, n) {
            if (d2[i][j]) {
              glob[0][0] = i;
              glob[0][1] = j;
              REP (dir, 4) {
                int ni = i + di[dir];
                int nj = j + dj[dir];
                if (d1[ni][nj]) {
                  glob[1][0] = ni;
                  glob[1][1] = nj;
                  phase++;
                  ll good = c1;
                  REP (ii, 2) {
                    REP (dir, 4) {
                      int ni = glob[ii][0] + di[dir];
                      int nj = glob[ii][1] + dj[dir];
                      if (who2[ni][nj][0] != phase && d1[ni][nj]) {
                        who2[ni][nj][0] = phase;
                        good--;
                      }
                    }
                  }
                  res = (res + good * (good - 1) / 2) % P;
                }
              }
            }
          }
        }
        
        debug(Tim(), "4");
          memset(who2, -1, sizeof(int) * (N * N * 4 - 1));
        RE (i, n) {
          RE (j, n) {
            if (d2[i][j]) {
              glob[0][0] = i;
              glob[0][1] = j;
              REP (dir, 4) {
                int ni = i + di[dir];
                int nj = j + dj[dir];
                if (d1[ni][nj]) {
                  glob[1][0] = ni;
                  glob[1][1] = nj;
                  //phase++;
                  REP (ii, 2) {
                    REP (huh, 4) {
                      if (who2[glob[ii][0]][glob[ii][1]][huh] == -1) {
                        who2[glob[ii][0]][glob[ii][1]][huh] = zium;
                        break;
                      }
                    }
                  }
                  zium++;
                }
              }
            }
          }
        }
        
        debug(Tim(), "5");
        last_chk2.resize(c2);
        debug(c2, last_chk2.capacity() * sizeof(last_chk2[0]));
        RE (i, n) {
          RE (j, n) {
            if (d2[i][j]) {
              glob[0][0] = i;
              glob[0][1] = j;
              REP (dir, 4) {
                int ni = i + di[dir];
                int nj = j + dj[dir];
                if (d1[ni][nj]) {
                  glob[1][0] = ni;
                  glob[1][1] = nj;
                  phase++;
                  ll good = c2;
                  REP (ii, 2) {
                    REP (dir, 4) {
                      int ni = glob[ii][0] + di[dir];
                      int nj = glob[ii][1] + dj[dir];
                      if (OnBoard(ni, nj)) {
                        REP (huh, 4) {
                          if (who2[ni][nj][huh] == -1) { break; }
                          int forb_d2 = who2[ni][nj][huh];
                          if (last_chk2[forb_d2] != phase) {
                            good--;
                            last_chk2[forb_d2] = phase;
                          }
                        }
                      }
                    }
                  }
                  res = (res + good * inv2) % P;
                }
              }
            }
          }
        }
      }
    }
  }
  
  debug(Tim(), "6");
  cout<<res<<endl;
        
      
      
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  return 0;
}