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
#include <bits/stdc++.h>
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge {c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#undef assert
#define assert

static const int dx[4] = {-1, 1, 0, 0};
static const int dy[4] = {0, 0, 1, -1};
typedef pair<int,int> P;
#define row first
#define col second
set< set<P> > sety[5];
vector<pair<long long, long long>> pre[5];
short duper[9][9];

vector<P> getAdjacent(P p) {
	vector<P> they(4);
	for(int i = 0; i < 4; ++i)
		they[i] = P{p.first + dx[i], p.second + dy[i]};
	return they;
}
set<P> getAdjacent(set<P> secik) {
	set<P> all;
	for(P p : secik)
		for(P nowy : getAdjacent(p))
			if(!secik.count(nowy))
				all.insert(nowy);
	return all;
}

int hasz(int row, int col) {
	return duper[row+4][col+4];
	//~ return 9 * (row + 4) + (col + 4);
}

void rec(set<P> they) {
	
	if(!sety[they.size()].insert(they).second) return;
	
	long long B = 0, C = 0;
	for(P p : they) {
		B |= 1LL << hasz(p.first, p.second);
		C |= 1LL << hasz(p.first, p.second);
	}
	for(P p : getAdjacent(they))
		C |= 1LL << hasz(p.first, p.second);
	
	pre[they.size()].push_back(make_pair(B, C));
	
	if((int) they.size() == 4) return;
	for(P p : they)
		for(P nowy : getAdjacent(p))
			if(!they.count(nowy)) {
				set<P> t2 = they;
				t2.insert(nowy);
				rec(t2);
			}
}

void preprocessing() {
	int it = 0;
	for(int i = -4; i <= 4; ++i)
		for(int j = -4; j <= 4; ++j)
			if(abs(i) + abs(j) <= 4)
				duper[i+4][j+4] = it++;
	cerr << "it = " << it << "\n";
	rec(set<P>{P{0, 0}});
	for(int i = 0; i <= 4; ++i) debug() << imie(i) imie(pre[i].size());
}

const int nax = 3005;
int n, k;
bool yes[nax][nax], almost[nax][nax], forbidden[nax][nax];
int cnt_almost;
const int mod = 1000 * 1000 * 1000 + 7;

bool inside(const P & p) {
	return 0 <= min(p.row, p.col) && max(p.row, p.col) < n;
}

void read() {
	scanf("%d%d", &n, &k);
	for(int row = 0; row < n; ++row) {
		static char sl[3005];
		scanf("%s", sl);
		for(int col = 0; col < n; ++col) {
			yes[row][col] = sl[col] == '#';
		}
	}
	for(int row = 0; row < n; ++row)
		for(int col = 0; col < n; ++col)
			if(yes[row][col])
				for(P nowy : getAdjacent(P{row, col}))
					if(inside(nowy) && !yes[nowy.row][nowy.col] && !almost[nowy.row][nowy.col]) {
						almost[nowy.row][nowy.col] = true;
						++cnt_almost;
					}
}

int memo[200][5];
long long binom(long long x, int b) {
	if(x < b) return 0;
	if(b == 0) return 1;
	assert(b <= 4);
	assert(0 <= cnt_almost - x && cnt_almost - x < 200);
	int & r = memo[cnt_almost-x][b];
	if(r) return r;
	
	vector<long long> tmp(b);
	for(int i = 0; i < b; ++i) tmp[i] = x - i;
	for(int i = b; i >= 2; --i) {
		bool done = false;
		for(long long & val : tmp)
			if(val % i == 0) {
				val /= i;
				done = true;
				break;
			}
		assert(done);
	}
	r = 1;
	for(long long val : tmp) r = (long long) r * val % mod;
	return r;
}

long long ple[100123];

int main() {
	
	preprocessing();
	read();
	int answer = binom(cnt_almost, k);
	debug() << imie(cnt_almost) imie(k) imie(answer);
	
	for(int row = 0; row < n; ++row)
		for(int col = 0; col < n; ++col)
			forbidden[row][col] = yes[row][col];
			
	long long cnt_spec = 0;
	
	long long sub = 0;
	
	for(int row = 0; row < n; ++row)
		for(int col = 0; col < n; ++col)
			if(almost[row][col]) {
				debug();
				int ip = 0;
				long long allowed = 0, almosty = 0;
				for(int r2 = max(0, row - 4); r2 <= min(n - 1, row + 4); ++r2) {
					const int tmp = 4 - abs(r2 - row);
					for(int c2 = max(0, col -   tmp); c2 <= min(n - 1, col + tmp); ++c2) {
						if(!forbidden[r2][c2])
							allowed |= 1LL << hasz(r2 - row, c2 - col);
						if(almost[r2][c2]) {
							almosty |= 1LL << hasz(r2 - row, c2 - col);
							if(k == 4 && abs(r2 - row) + abs(c2 - col) <= 3) {
						debug() << "maybe" imie(r2) imie(c2);
								long long tu = 1LL << hasz(r2 - row, c2 - col);
								for(int i = 0; i < 4; ++i) {
									int r3 = r2 + dx[i];
									int c3 = c2 + dy[i];
									if(0 <= r3 && 0 <= c3 && c3 < n && r3 < n && !yes[r3][c3] && !almost[r3][c3]) {
										debug() << imie(r2) imie(c2) imie(r3) imie(c3);
										ple[ip++] = (tu | (1LL << hasz(r3 - row, c3 - col)));
									}
								}
							}
						}
					}
				}
				//~ debug() << imie(row) imie(col) imie(ple.size());
				for(int here = 2; here <= k; ++here)
					for(const pair<long long, long long> & need : pre[here]) {
						if((need.first & allowed) == need.first) {
							if((need.first & almosty) == need.first) continue;
							if(here == 2 && k == 4) {
								++cnt_spec;
								for(int j = 0; j < ip; ++j) {
									if(ple[j] & (need.first | need.second)) {
										debug() << imie(row) imie(col) "++";
										++sub;
									}
								}
								--sub;
								debug() << imie(row) imie(col) "--";
								//~ ++answer;
							}
							const int tmp = binom(cnt_almost - __builtin_popcountll(need.second & almosty), k - here);
							debug() << imie(here) imie(row) imie(col) imie(tmp);
							answer += tmp;
							if(answer >= mod) answer -= mod;
						}
					}
				
				forbidden[row][col] = true;
			}
	
	cerr << "sub = " << sub << "\n";
	assert(sub % 2 == 0);
	if(k == 4) {
		answer = (answer + cnt_spec * (cnt_spec - 1) / 2) % mod;
		answer = (((answer - sub / 2) % mod) + mod) % mod;
	}
	printf("%d\n", answer);
	
}