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
#include <cstdio>
#include <cassert>
#include <deque>
#include <utility>

using namespace std;

#define MAX_N 3001
#define M_BASE 1000000007
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define M(a) MAX(a,0)

#define TEST false

int n, k;
int b[5][MAX_N][MAX_N];
// v[i][j] is true if it's visited (for 0es and 1es only)
bool v[MAX_N][MAX_N];
// p[i][j] contains number of tiles with distance 'i'
// which could be reached by 'j' different paths
// it matters only for i > 1
int p[5][37];
// d[i] containes number of all paths to reach fields with distance of 'i'
// see d[i] == p[1][1] (for fields with distance 1 we doesn't count paths)
int d[5];
int two_thres;


void read_input() {
	scanf("%d%d\n", &n, &k);
	char c;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			scanf("%c", &c);
			if (c == '#') {
				b[0][i][j] = 1;
				v[i][j] = true;
			}
		}
		if (i < n - 1)
			scanf("\n");
	}
}


// compute binomial coefficient modulo M_BASE
// k is max 4
int m_bin(int n, int k) {
	// check if k is not greater than n and if n is positive
	k = MIN(n, k);
	if (n <= 0 || k <= 0) return 0;
	int t[5];
	long long temp = 1;
	for (int i = 0; i < k; i++) {
		t[i] = n - i;
	}
	// divide factors to not divide overflowed product
	for (int i = k; i > 1; i--) {
		for (int j = 0; j < k; j++) {
			if (t[j] % i == 0) {
				t[j] /= i;
				break;
			} 
		}
	}
	for (int i = 0; i < k; i++) {
		temp = (temp * (t[i]  % M_BASE)) % M_BASE;
	}
	return (int)(temp % M_BASE);
}

int m_add(int a, int b) {
	return (a % M_BASE + b % M_BASE) % M_BASE;
}

int m_mul(int a, int b) {
	long long int r = a % M_BASE;
	r = r * (b % M_BASE) % M_BASE;
	return (int)r;
}

deque<pair<int, int> > process_ones() {
	deque<pair<int, int> > ones = deque<pair<int, int> >();
	for(int i=0; i<n; i++) {
		for(int j=0; j<n; j++) {
			if(b[0][i][j] == 0) {
				if(b[1][i][j] == 0) {
					bool any = false;
					if (i>0) {
						any = b[0][i-1][j];
					}
					if (!any && i<n-1) {
						any = b[0][i+1][j];
					}
					if (!any && j>0) {
						any = b[0][i][j-1];
					}
					if (!any && j<n-1) {
						any = b[0][i][j+1];
					}
					if (any) {
						b[1][i][j] = 1;
						v[i][j] = true;
						ones.push_back(make_pair(i, j));
					}
				}
			}
		}
	}
	p[1][1] = ones.size();
	d[1] = p[1][1];
	return ones;
}

deque<pair<int, int> > process_two(deque<pair<int, int> > ones) {
	deque<pair<int, int> > twos = deque<pair<int, int> >();
	deque<pair<int, int> >::iterator ones_it = ones.begin();
	while(ones_it != ones.end()) {
		int i=ones_it->first, j=ones_it->second;

		if(i>0) {
			if (!v[i-1][j]) {
				if(!b[2][i-1][j]) {
					twos.push_back(make_pair(i-1, j));
				}
				++b[2][i-1][j];
			}
		}
		if(i<n-1) {
			if (!v[i+1][j]) {
				if(!b[2][i+1][j]) {
					twos.push_back(make_pair(i+1, j));
				}
				++b[2][i+1][j];
			}
		}
		if(j>0) {
			if (!v[i][j-1]) {
				if(!b[2][i][j-1]) {
					twos.push_back(make_pair(i, j-1));
				}
				++b[2][i][j-1];
			}
		}
		if(j<n-1) {
			if (!v[i][j+1]) {
				if(!b[2][i][j+1]) {
					twos.push_back(make_pair(i, j+1));
				}
				++b[2][i][j+1];
			}
		}
		++ones_it;
	}

	deque<pair<int, int> >::iterator twos_it = twos.begin();
	while(twos_it != twos.end()) {
		int i=twos_it->first, j=twos_it->second;
		// count how many fields can be reached by specified number of ways
		// printf("i: %d, j: %d\n", i, j);
		++p[2][b[2][i][j]];
		// how many ways to reach twos fields
		d[2] += b[2][i][j];
		++twos_it;
	}
	return twos;
}

deque<pair<int, int> > process_thres(deque<pair<int, int> > twos) {
	deque<pair<int, int> > thres = deque<pair<int, int> >();
	deque<pair<int, int> >::iterator twos_it = twos.begin();
	while(twos_it != twos.end()) {
		int i=twos_it->first, j=twos_it->second;

		if(i>0) {
			if (!v[i-1][j]) {
				if(!b[3][i-1][j]) {
					thres.push_back(make_pair(i-1, j));
				}
				b[3][i-1][j] += b[2][i][j];
			}
		}
		if(i<n-1) {
			if (!v[i+1][j]) {
				if(!b[3][i+1][j]) {
					thres.push_back(make_pair(i+1, j));
				}
				b[3][i+1][j] += b[2][i][j];
			}
		}
		if(j>0) {
			if (!v[i][j-1]) {
				if(!b[3][i][j-1]) {
					thres.push_back(make_pair(i, j-1));
				}
				b[3][i][j-1] += b[2][i][j];
			}
		}
		if(j<n-1) {
			if (!v[i][j+1]) {
				if(!b[3][i][j+1]) {
					thres.push_back(make_pair(i, j+1));
				}
				b[3][i][j+1] += b[2][i][j];
			}
		}
		++twos_it;
	}
	deque<pair<int, int> >::iterator thres_it = thres.begin();
	while(thres_it != thres.end()) {
		int i=thres_it->first, j=thres_it->second;
		// count how many fields can be reached by specified number of ways
		// printf("i: %d, j: %d\n", i, j);
		++p[3][b[3][i][j]];
		// how many ways to reach thres fields
		d[3] += b[3][i][j];
		if(b[2][i][j] > 0) {
			// todo maybe sth different
			++two_thres;
		}
		++thres_it;
	}
	return thres;
}


int detect_triangles(int i, int j) {
	int res = 0;
	// assume i, j is b[3] field
	if (i>0) {
		if(j>0) {
			if(b[2][i-1][j] && b[2][i][j-1] && b[1][i-1][j-1])
				++res;
		}
		if (j<n-1) {
			if(b[2][i-1][j] && b[2][i][j+1] && b[1][i-1][j+1])
				++res;
		}
	}
	if (i<n-1) {
		if(j>0) {
			if(b[2][i+1][j] && b[2][i][j-1] && b[1][i+1][j-1])
				++res;
		}
		if (j<n-1) {
			if(b[2][i+1][j] && b[2][i][j+1] && b[1][i+1][j+1])
				++res;
		}
	}
	return res;
}

int process_fours(deque<pair<int, int> > thres) {
	int res = 0, a;
	deque<pair<int, int> >::iterator it = thres.begin();
	while(it != thres.end()) {
		int i=it->first, j=it->second;

		if(i>0) {
			if (!v[i-1][j]) {
				res += M(b[3][i][j] - b[2][i-1][j]);
			}
		}
		if(i<n-1) {
			if (!v[i+1][j]) {
				res += M(b[3][i][j] - b[2][i+1][j]);
			}
		}
		if(j>0) {
			if (!v[i][j-1]) {
				res += M(b[3][i][j] - b[2][i][j-1]);
			}
		}
		if(j<n-1) {
			if (!v[i][j+1]) {
				res += M(b[3][i][j] - b[2][i][j+1]);
			}
		}

		res -= detect_triangles(i, j);
		++it;
	}
	d[4] = res;
	return res;
}

int process_fields() {
	deque<pair<int, int> > ones = process_ones();
	//printf("ones: %d\n", d[1]);
	deque<pair<int, int> > twos = process_two(ones);

	deque<pair<int, int> > thres = process_thres(twos);
	//printf("d[3]: %d\n", d[3]);
	int fours = process_fours(thres);
	//printf("d[4]: %d\n", d[4]);
	// remember to subtract these 3 fields ways + 1 field which could be reached by double 2 ways, eg:
	// 1   1          a   b  is the same as:  a   b
	// 2/3 2/3        a   a                   a   b
	// it's stored in two_thres field if it gives anything

	int res = 0;
	if (k == 1) {
		res = d[1];
	} else if (k == 2) {
		res = m_add(m_bin(d[1], 2), d[2]);
	} else if (k == 3) {
		res = m_add(m_bin(d[1], 3), d[3]);
		for(int i = 1; i <= 8; i++) {
			if (p[2][i]) {
				for (int j = 1; j<=i; j++) {
					res = m_add(res, m_mul(p[2][i], M(d[1] - j)));
				}
			}
		}
	} else if (k == 4) {

	}
	return res;
}


int main() {
	if(TEST) {
		assert(m_bin(5, 3) == 10);
		assert(m_bin(2, 3) == 1);
		assert(m_bin(-4, -5) == 0);
		assert(m_bin(9000000, 4) == 142856102);
		printf("Tests passed.\n");
		return 0;
	}
	read_input();
	int res = process_fields();
	printf("%d\n", res);
	return 0;
}