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
/*
 *  Copyright (C) 2017  Paweł Widera
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details:
 *  http://www.gnu.org/licenses/gpl.html
 */
#include <vector>
#include <unordered_set>
#include <iostream>
using namespace std;

int MOD = 1000000007;

int n, k;
vector<vector<int>> board;
vector<vector<int>> cache;
vector<pair<int, int>> neighbours = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

vector<unordered_set<pair<int, int>>> positions(5);

// pair hasher (based on boost implementation)
namespace std {
	template<typename S, typename T> struct hash<pair<S, T> > {
		inline size_t operator()(const pair<S, T>& p) const {
			hash<S> hasher1;
			hash<T> hasher2;

			size_t seed = 0;
			seed ^= hasher1(p.first) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
			seed ^= hasher2(p.second) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
			return seed;
		}
	};
}


// check if tile has a neighbour with a given value
bool is_next(int i, int j, int value) {
	for (auto move: neighbours) {
		if (value == board[i + move.first][j + move.second]) {
			return true;
		}
	}
	return false;
}


// mark a tile if it has a "value" tile next to it, return total number of marks
int annotate(int value, int mark) {
	int count = 0;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			if (board[i][j] == 0 && is_next(i, j, value)) {
				board[i][j] = mark;
				++count;
			}
		}
	}
	return count;
}


// variant for k > 2, also stores coordinates of all 3s and 4s
int annotate_and_store(int value, int mark) {
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			if (board[i][j] == 0 && is_next(i, j, value)) {
				board[i][j] = mark;
				positions[mark].insert(make_pair(i, j));
			}
		}
	}
	return positions[mark].size();
}


// count neighbours with a given value
int count_next(int i, int j, int value) {
	int count = 0;
	for (auto move: neighbours) {
		if (value == board[i + move.first][j + move.second]) {
			++count;
		}
	}
	return count;
}


// find number of ways to pick a "start" tiles and its "end" neighbour
unsigned long long int count_neighbours(int start, int end) {
	unsigned long long int count = 0;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			if (board[i][j] == start) {
				count += count_next(i, j, end);
			}
		}
	}
	return count;
}


// variant for k > 2, only the "12" moves, building cache to be used by 3s and 4s
unsigned long long int count_neighbours_cache() {
	unsigned long long int count = 0;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			if (board[i][j] == 2) {
				int number = count_next(i, j, 1);
				count += number;
				cache[i][j] = number;
			}
		}
	}
	return count;
}


// variant for k > 2, uses cached values for speed up
int count_next_cached(int i, int j, int value) {
	int count = 0;
	for (auto move: neighbours) {
		int rows = i + move.first;
		int columns = j + move.second;
		if (value == board[rows][columns]) {
			count += cache[rows][columns];
		}
	}
	return count;
}


// variant for k > 2, looping over stored coordinates for speed up
unsigned long long int count_neighbours_fast(int start, int end) {
	unsigned long long int count = 0;
	for (auto p: positions[start]) {
		int number = count_next_cached(p.first, p.second, end);
		count += number;
		cache[p.first][p.second] = number;
	}
	return count;
}


// how many "12" moves have "2" as a neighbour
unsigned long long int count_122() {
	float count = 0;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			if (board[i][j] == 2) {
				int count22 = count_next(i, j, 2);
				for (auto move: neighbours) {
					int rows = i + move.first;
					int columns = j + move.second;
					if (1 == board[rows][columns]) {
						count += 0.5 * (count_next(rows, columns, 2) - 1) + count22;
					}
				}
			}
		}
	}
	return static_cast<unsigned long long int>(count);
}


int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> n >> k;
	board.reserve(n + 2);

	// set top and bottom guards
	board[0].resize(n + 2, 0);
	board[n + 1].resize(n + 2, 0);

	char symbol;
	for (int i = 1; i <= n; ++i) {
		board[i].reserve(n + 2);
		board[i].push_back(0);  // set left side guard
		for (int j = 0; j < n; ++j) {
			cin >> symbol;
			board[i].push_back(symbol == '#' ? 9 : 0);
		}
		board[i].push_back(0);  // set right side guard
	}

	// preprocessing
	vector<unsigned long long int> counter(k + 1, 0);
	for (int i = 1; i <= min(k, 2); ++i) {
		int value = i == 1 ? 9 : i - 1;
		counter[i] = annotate(value, i);
	}
	for (int i = 3; i <= k; ++i) {
		counter[i] = annotate_and_store(i - 1, i);
	}

	unsigned long long int count = counter[1];

	// number of ways to choose k 1s
	for (int i = 2; i <= k; ++i) {
		count = count * (counter[1] - i + 1) / i; // % MOD;
	}

	if (k == 2) {
		// number of distinct "12" moves
		count = (count + count_neighbours(2, 1)) % MOD;
	}
	else {
		// init cache array (not needed for k < 3)
		cache.reserve(n + 2);
		for (int i = 1; i <= n; ++i) {
			cache[i].resize(n + 2, 0);
		}

		if (k == 3) {
			// number of distinct "1" + "12" moves
			count += (counter[1] - 1) * count_neighbours_cache() % MOD;
			// number of distinct "122" moves
			count = (count + count_122()) % MOD;
			// number of distinct "123" moves
			count = (count + count_neighbours_fast(3, 2)) % MOD;
		}
		else if (k == 4) {
			unsigned long long int count12 = count_neighbours_cache();
			// number of distinct "12" + "12" moves
			count += count12 * count12 % MOD;
			// number of distinct "1" + "1" + "12" moves
			count += (counter[1] - 1) * (counter[1] - 2) % MOD * count12 % MOD;
			// number of distinct "1" + "122" moves
			count += (counter[1] - 1) * count_122() % MOD;
			// number of distinct "1" + "123" moves
			count += (counter[1] - 1) * count_neighbours_fast(3, 2) % MOD;
			// number of distinct "1234" moves
			count = (count + count_neighbours_fast(4, 3)) % MOD;
		}
	}

	cout << count << endl;
	return 0;
}