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

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define all(v) (v).begin(),(v).end()

#define PII pair<short,short>
#define mp make_pair
#define st first
#define nd second
#define pb push_back
#define lint long long int
#define VI vector<int>

#define debug(x) {cerr <<#x <<" = " <<x <<endl; }
#define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; } 
#define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; } 
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }}
#define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }}

#define make( x) int (x); scanf("%d",&(x));
#define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y));
#define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z));
#define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t));
#define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);} 
#define IOS ios_base::sync_with_stdio(0)
#define HEAP priority_queue

#define read( x) scanf("%d",&(x));
#define read2( x, y) scanf("%d%d",&(x),&(y));
#define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z));
#define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t));
#define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);}


using namespace std;

int mod = 1e9 + 7;
int szosta = 166666668;
int dwacztery = 41666667;
int n,m, k;
char s[3005][3005];
short dist[3005][3005];
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};
vector<PII> graf[6];

void wczytaj() {
	read2(n, k);
	m = n;
	FOR(i,0,n) {
		scanf("%s", s[i]);
	}
}

bool inside(int a, int b) {
	return (a >= 0 && a<n && b>=0 && b < n);
}

bool sasiedzi(PII a, PII b) {
	return (abs(a.st-b.st) + abs(a.nd-b.nd) == 1);
}

void oblicz_dist() {
	FOR(i,0,n) FOR(j,0,m) {
		if (s[i][j] == '.') dist[i][j] = 5;
		else {
			dist[i][j] = 0;
			graf[0].pb(mp(i,j));
		}
	}
	FOR(d,0,4) {
		FORE(j, graf[d]) {
			int x = j->st, y = j->nd;
			FOR(u,0,4) {
				int nx = x + dx[u], ny = y + dy[u];
				if (!inside(nx, ny)) continue;
				if (dist[nx][ny] <= d+1) continue;
				dist[nx][ny] = d + 1;
				graf[d+1].pb(mp(nx, ny));
			}
		}
	}
}


int main() {
	wczytaj();
	oblicz_dist();
	if (k == 1) {
		printf("%d\n", (int) graf[1].size());
		return 0;
	} 
	if (k == 2) {
		int g1 = graf[1].size();
		int ans = ((g1 * 1LL * (g1 - 1))/2) % mod;
		FORE(j, graf[2]) {
			int x = j->st, y = j->nd;
			FOR(u,0,4) {
				int nx = x + dx[u], ny = y + dy[u];
				if (!inside(nx, ny)) continue;
				if (dist[nx][ny] == 1) ans++;
			}
		}
		printf("%d\n", ans%mod);
		return 0;
	} 
	if (k == 3) {
		int ans = 0;
		//300
		int g1 = graf[1].size();
		int xxx = (((g1*1LL*(g1-1))%mod)*1LL*(g1-2))%mod;
		ans = (xxx * 1LL * szosta)%mod;
		//210
		FORE(j, graf[2]) {
			int x = j->st, y = j->nd;
			int przyczepy = 0;
			FOR(u,0,4) {
				int nx = x + dx[u], ny = y + dy[u];
				if (!inside(nx, ny)) continue;
				if (dist[nx][ny] == 1) przyczepy++;
			}
			ans += (przyczepy * (przyczepy -1))/2;
			ans += (przyczepy * 1LL * (g1 - przyczepy));
			ans %= mod;
		}
		//120 + 111
		FORE(j, graf[1]) {
			int x = j->st, y = j->nd;
			int przyczepy = 0;
			FOR(u,0,4) {
				int nx = x + dx[u], ny = y + dy[u];
				if (!inside(nx, ny)) continue;
				if (dist[nx][ny] == 2) przyczepy++;
			}
			ans += (przyczepy * (przyczepy -1))/2;
			ans %= mod;	
		}
		FORE(j, graf[2]) {
			int x = j->st, y = j->nd;
			vector<PII> drugi, pierwszy,trzeci;
			FOR(u,0,4) {
				int nx = x + dx[u], ny = y + dy[u];
				if (!inside(nx, ny)) continue;
				if (dist[nx][ny] == 1) pierwszy.pb(mp(nx,ny));
				if (dist[nx][ny] == 2) drugi.pb(mp(nx,ny));
				if (dist[nx][ny] == 3) trzeci.pb(mp(nx,ny));
			}
			ans += (trzeci.size() * pierwszy.size());	
			FORE(jj, pierwszy) {
				FORE(kk, drugi) {
					if (!sasiedzi(*jj, *kk)) ans++;
				}
			}
			ans %= mod;
		}

		printf("%d\n", ans%mod);
		return 0;
	}
	if (k == 4) {
		int ans = 0;
		int krzyze = 0;
		//4000
		int g1 = graf[1].size();
		int xxx = (((((g1*1LL*(g1-1))%mod)*1LL*(g1-2))%mod)*(g1-3))%mod;
		ans = (xxx * 1LL * dwacztery)%mod;
		//3100 i 2110
		FORE(j, graf[1]) {
			int x = j->st, y = j->nd;
			vector<PII> drugi;
			FOR(u,0,4) {
				int nx = x + dx[u], ny = y + dy[u];
				if (!inside(nx, ny)) continue;
				if (dist[nx][ny] == 2) drugi.pb(mp(nx,ny));
			}
			// wszystkie podlaczone
			int sh = drugi.size();
			ans += (sh * (sh - 1) * (sh - 2))/6;
			// dwa podlaczone
			set<PII> se[5], se22[5],se33[5];
			FOR(aa,0,drugi.size()) {
					FOR(u,0,4) {
							int nx = drugi[aa].st + dx[u], ny = drugi[aa].nd + dy[u];
							if (inside(nx, ny) && dist[nx][ny] == 2 && !sasiedzi(mp(nx, ny), *j)) se[aa].insert(mp(nx,ny));
							if (inside(nx, ny) && dist[nx][ny] == 3) se22[aa].insert(mp(nx,ny));
							if (inside(nx, ny) && dist[nx][ny] == 1) se33[aa].insert(mp(nx, ny));
					}
			}
			FOR(aa, 0, drugi.size()) {
				FOR(bb, aa+1, drugi.size()) {
					ans += se[aa].size();	FORE(uuu, se[bb]) if (se[aa].find(*uuu) == se[aa].end()) ans++;
					ans += se22[aa].size(); FORE(uuu, se22[bb]) if (se22[aa].find(*uuu) == se22[aa].end()) ans++;		
					int ilee = 0;

					FORE(uuu, se33[aa]) if (se33[bb].find(*uuu) != se33[bb].end()) ilee++;
					krzyze += ilee-1;
					if (krzyze >= mod) krzyze -= mod;
					ans += (graf[1].size() - (se33[aa].size()+se33[bb].size()-ilee)-1);
					if (ans < 0) ans += mod;
					if (ans >= mod) ans -= mod;
				}
			}
			ans %= mod;
			// jeden podlaczony
			FOR(aa, 0, drugi.size()) {
					set<PII> secik;
					FOR(u,0,4) {
							int nx = drugi[aa].st + dx[u], ny = drugi[aa].nd + dy[u];
							if (inside(nx, ny) && dist[nx][ny] == 2 && !sasiedzi(mp(nx, ny), *j)) secik.insert(mp(nx,ny));
					}	
					int qu = secik.size();
					ans = (ans + (qu * (qu - 1))/2) % mod;
					FORE(jjj, secik) {
						FOR(u,0,4) {
							int nx = jjj->st + dx[u], ny = jjj->nd  + dy[u];
							if (inside(nx, ny) && dist[nx][ny] == 2 && !sasiedzi(mp(nx, ny), *j) && !sasiedzi(drugi[aa], mp(nx,ny))) {
								ans++;
							}
						}
					}
			}
			ans %= mod;
		}
		int par = 0;
		FORE(j, graf[2]) {
			int x = j->st, y = j->nd;
			vector<PII> drugi, pierwszy,trzeci;
			FOR(u,0,4) {
				int nx = x + dx[u], ny = y + dy[u];
				if (!inside(nx, ny)) continue;
				if (dist[nx][ny] == 1) pierwszy.pb(mp(nx,ny));
				if (dist[nx][ny] == 2) drugi.pb(mp(nx,ny));
				if (dist[nx][ny] == 3) trzeci.pb(mp(nx,ny));
			}
			int p = pierwszy.size();
			par += p;
			ans -= (p * (p-1))/2;
			if (ans < 0) ans += mod;

			int q = g1 - p;
			//3100
			ans += (p*(p-1)*(p-2))/6 + (p*(p-1))/2 * q;
			ans += ((((q * 1LL * (q-1))/2)%mod) * 1LL * p) % mod;
			ans %= mod;
			//2110
			int yyy = (p*(p-1))/2 + p * q;
			ans = (ans + (yyy*1LL*trzeci.size())%mod);
			if (ans >= mod) ans -= mod;
			//1120 
			int tr = trzeci.size();
			ans += ((tr * (tr-1))/2 * 1LL * p) % mod;
			FORE(jj, trzeci) {
				FOR(u,0,4) {
					int nx = jj->st + dx[u], ny = jj->nd + dy[u];
					if (!inside(nx, ny)) continue;
					if (dist[nx][ny] != 3) continue;
					if (sasiedzi(*j, mp(nx, ny))) continue;
					ans += p;
					if (ans >= mod) ans -= mod;
				}
			}
			FORE(jj,drugi) {
				FORE(jjj, pierwszy) {
					if (sasiedzi(*jj, *jjj)) continue;
					set<PII> poss;
					FORE(uuu, trzeci) poss.insert(*uuu);
					FOR(u,0,4) {
							int nx = jj->st + dx[u], ny = jj->nd + dy[u];
							if (inside(nx, ny) && dist[nx][ny] == 3) poss.insert(mp(nx, ny));
					}
					ans += poss.size();
					if (ans >= mod) ans -= mod;
				}
			}
			FORE(jjj, trzeci) {
				FORE(jj, pierwszy) {
					FOR(u,0,4) {
							int nx = jjj->st + dx[u], ny = jjj->nd + dy[u];
							if (inside(nx, ny) && dist[nx][ny] == 2 && !sasiedzi(mp(nx, ny), *j) && !sasiedzi(mp(nx,ny), *jj)) ans++;
					}
				}
			}
			FOR(aa, 0, pierwszy.size()) {
				FOR(bb, aa+1, pierwszy.size()) {
					FORE(cc, drugi) {
						if (!sasiedzi(*cc, pierwszy[aa]) && !sasiedzi(*cc, pierwszy[bb])) ans++;
					}
				}
			}
			// a2) dwojka polaczona raz 
			FORE(aa, drugi) {
				set<PII> secik;
				FOR(u,0,4) {
						int nx = aa->st + dx[u], ny = aa->nd + dy[u];
						if (inside(nx, ny) && dist[nx][ny] == 1) secik.insert(mp(nx, ny));
				}	
				FORE(jj, pierwszy) secik.insert(*jj);
				ans = (ans + pierwszy.size() * 1LL * (graf[1].size() - secik.size())) % mod;
			}
		}
		ans += ((par * 1LL * (par - 1))/2 ) % mod;
		ans %= mod;
		//1111
		FORE(j, graf[3]) {
			int x = j->st, y = j->nd;
			int czwarty = 0;
			vector<PII> drugi;
			FOR(u,0,4) {
				int nx = x + dx[u], ny = y + dy[u];
				if (!inside(nx, ny)) continue;
				if (dist[nx][ny] == 2) drugi.pb(mp(nx,ny));
				if (dist[nx][ny] == 4) czwarty++;
			}
			int ile = 0;
			FORE(jj, drugi) {
				FOR(w,0,4) {
					int nx = jj->st + dx[w], ny = jj->nd + dy[w];
					if (!inside(nx, ny)) continue;
					if (dist[nx][ny] == 1) ile++;
				}
			}
			ans += (ile * 1LL * czwarty) % mod;
			ans %= mod;
		}
		ans -= ((krzyze * 1LL * (mod + 1)/2))%mod;
		if (ans < 0) ans += mod;
		printf("%d\n", ans%mod);
		return 0;
	}
	
}