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
#include <cstdio>
#include <cstdlib>
#include <vector>

using namespace std;

typedef long long int int64;

typedef struct {
	int neighs[4];
	int ways;
	char nei_count;
	unsigned char dist;
} field_t;

const int64 MOD = 1000000007LL;
int n, nn, k;

int64 kofn(int n, int k) {
	if (k == 1) {
		return (int64)n % MOD;
	}
	int64 res = 1;
	unsigned char rem_2s = k == 4 ? 3 : 1;
	unsigned char rem_3s = k == 2 ? 0 : 1;
	for (int i = 0; i < k; ++i) {
		int64 mul = (int64)n - i;
		while (rem_2s > 0 && mul % 2 == 0) {
			mul /= 2;
			--rem_2s;
		}
		while (rem_3s > 0 && mul % 3 == 0) {
			mul /= 3;
			--rem_3s;
		}
		res = (res * mul) % MOD;
	}
	return res;
}

int main () {
	scanf("%d %d", &n, &k);
	nn = n * n;
	field_t* board = (field_t*)malloc((nn + 8) * sizeof(field_t));
	for (int i = 0; i < nn; ++i) {
		board[i].nei_count = 0;
		if (i % n) {
			board[i].neighs[board[i].nei_count] = i - 1;
			++board[i].nei_count;
		}
		if (i % n != n - 1) {
			board[i].neighs[board[i].nei_count] = i + 1;
			++board[i].nei_count;
		}
		if (i - n >= 0) {
			board[i].neighs[board[i].nei_count] = i - n;
			++board[i].nei_count;
		}
		if (i + n < nn) {
			board[i].neighs[board[i].nei_count] = i + n;
			++board[i].nei_count;
		}
		char c = 0;
		while (c != '#' && c != '.') {
			scanf("%c", &c);
		}
		if (c == '#') {
			board[i].dist = 0;
		}
		else {
			board[i].dist = 8;
			board[i].ways = 0;
		}
	}
	int xs = 0;
	for (int i = 1; i <= k; ++i) {
		for (int j = 0; j < nn; ++j) {
			if (board[j].dist != 8) {
				continue;
			}
			for (char nei = 0; nei < board[j].nei_count; ++nei) {
				if (board[board[j].neighs[nei]].dist == i - 1) {
					board[j].dist = i;
					if (i == 1) {
						board[j].ways = 1;
						++xs;
						break;
					}
					board[j].ways += board[board[j].neighs[nei]].ways;
				}
			}
		}
	}
	int64 sum = kofn(xs, k);
	if (k == 2) {
		for (int i = 0; i < nn; ++i) {
			if (board[i].dist == 2) {
				sum += (int64)board[i].ways;
			}
		}
		sum %= MOD;
	}
	else if (k == 3) {
		for (int i = 0; i < nn; ++i) {
			if (board[i].dist == 2) {
				sum += kofn(xs, 2) - kofn(xs - board[i].ways, 2);
				while (sum < 0) {
					sum += MOD;
				}
			}
			else if (board[i].dist == 1) {
				int twos = 0;
				int extra = 0;
				for (char nei = 0; nei < board[i].nei_count; ++nei) {
					if (board[board[i].neighs[nei]].dist == 2) {
						++twos;
						for (char nei2 = 0; nei2 < board[board[i].neighs[nei]].nei_count; ++nei2) {
							if (board[board[board[i].neighs[nei]].neighs[nei2]].dist == 2) {
								++extra;
							}
						}
					}
				}
				sum += extra + (int64)twos * (twos - 1) / 2;
			}
			else if (board[i].dist == 3) {
				sum += (int64)board[i].ways;
				sum %= MOD;
			}
			sum %= MOD;
		}
	}
	printf("%lld\n", sum);
	return 0;
}