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
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <set>
#define MAX_N 3003

using namespace std;
const char EMPTY = '.';
const long long int MOD = 1e9+7;
char tab[MAX_N][MAX_N];
long long int ways[MAX_N][MAX_N];
int dis[MAX_N][MAX_N];
long long int numberOnes , numberTwos , numberThrees , numberFours;
//numberOnes = 0;//numberTwos = numberThrees = numberFours = 0;
pair<int,int> que[MAX_N*MAX_N];
int que_size = 0;
int n,k;
int dir[][2] = {{1,0},{0,1},{-1,0},{0,-1}};
void bfs()
{
	for(int i=1;i<=n;++i)
		for(int j=1;j<=n;++j)
			if(tab[i][j] != EMPTY){
				que[que_size++] = make_pair(i,j);
				
			}
	int poz=0;
	while(poz<que_size)
	{
		pair<int,int> cur = que[poz++];
		for(int i=0;i<4;++i)
		{
			int x,dx,y,dy;
			x = cur.first;
			y = cur.second;
			dx = x+dir[i][0];
			dy = y+dir[i][1];
			if(tab[dx][dy] == EMPTY && dis[dx][dy] == 0)
			{
				dis[dx][dy] = dis[x][y]+1;
				if(dis[dx][dy] == 1)ways[dx][dy] = 1;
				que[que_size++] = make_pair(dx,dy);
			}
			if(tab[dx][dy] == EMPTY && dis[dx][dy]  == dis[x][y]+1)
			{
				
				ways[dx][dy] +=ways[x][y];
			}
			
		}
	}
	/*
	cout << "bfs\n";
	for(int i=1;i<=n;++i){
		for(int j=1;j<=n;++j)
			cout << dis[i][j] << " ";
		cout << "\n";
	}
	cout << "\n";
	for(int i=1;i<=n;++i){
		for(int j=1;j<=n;++j)
			cout << ways[i][j] << " ";
		cout << "\n";
	}
	cout << "\n";
	for(int i=1;i<=n;++i){
		for(int j=1;j<=n;++j)
			cout << tab[i][j];
		cout << "\n";
	}
	*/
	for(int i=1;i<=n;++i)
		for(int j=1;j<=n;++j)
			if(tab[i][j] ==	EMPTY)
			{
				//if(dis[i][j] == 2)cout << i << " " << j << " " << ways[i][j] << "\n";
				if(dis[i][j] == 1)numberOnes++;
				if(dis[i][j] == 2)numberTwos+=ways[i][j];
				if(dis[i][j] == 3)numberThrees+=ways[i][j];
				if(dis[i][j] == 4)numberFours+=ways[i][j];
			}
	
}
long long int rev(long long int a,long long int b)
{
	long long int u,w,x,z,q;
	u = 1; w = a;
	x = 0; z = b;
	while(w)
	{
		if(w < z)
		{
			q = u; u = x; x = q;
			q = w; w = z; z = q;
		}
		q = w / z;
		u -= q * x;
		w -= q * z;
  }
  if(x<0)x+=b;
  return x;
}
long long int factorial(long long int a,long long int MOD)
{
	long long int res = 1;
	for(int i=2;i<=a;++i)
		res = (res*i)%MOD;
	return res;
}
long long int newton(long long int a,long long int b)
{
	if(a<b)return 0;
	long long int res = 1;
	for(int i=a-b+1;i<=a;++i)
		res=(res*i)%MOD;
	res*=rev(factorial(b,MOD),MOD);
	res%=MOD;
	return res;
}
long long int solve()
{
	long long int res =0;
	if(k==1)return numberOnes%MOD;
	if(k==2)return (numberTwos + newton(numberOnes,2))%MOD;
	if(k==3){
		res = numberThrees + newton(numberOnes,3);
		res = res%MOD;
		res = (res + numberTwos*(numberOnes-1))%MOD;
		long long int maks = 0;
		for(int i=1;i<=n;++i)
			for(int j=1;j<=n;++j)
				if(dis[i][j] == 2)
				{
					maks = max(maks,ways[i][j]);
					if(ways[i][j] == 2)res--;
					if(ways[i][j] == 3)res -=3;
					if(ways[i][j] == 4)res -=6;
				}
		if(res < 0)res+=MOD;
		return res;
	}
	res = numberFours;
	res = (res+newton(numberOnes,4))%MOD;
	res = (res+numberThrees*(numberOnes-1))%MOD;
	// 2 1 1
	if(numberOnes>=2)
	{
			res = (res+numberTwos*newton(numberOnes-1,2))%MOD;
			for(int i=1;i<=n;++i)
			for(int j=1;j<=n;++j)
				if(dis[i][j] == 2)
				{
					if(ways[i][j] == 2)res-= numberOnes-2;
					if(ways[i][j] == 3)res -= (numberOnes-3)*3+2;
					if(ways[i][j] == 4)res -= 6*(numberOnes-4)+8;
					while(res < 0)res+=MOD;
				}
	}
	res=res%MOD;
	// 2 2
	if(numberTwos>=2)
	{
		res = (res+newton(numberTwos,2))%MOD;
			for(int i=1;i<=n;++i)
			for(int j=1;j<=n;++j)
				if(dis[i][j] == 2)
				{
					if(ways[i][j] == 2)res--;
					if(ways[i][j] == 3)res -=3;
					if(ways[i][j] == 4)res -=6;
				
				}
	}
	while(res<0)res+=MOD;
	
	return res%MOD;
}
int main()
{
	std::ios::sync_with_stdio(false);
	numberOnes = numberTwos = numberThrees = numberFours = 0;
	cin >> n >> k;
	for(int i=0;i<MAX_N;++i)
		tab[i][0] = tab[0][i] = tab[i][MAX_N-1] = tab[MAX_N-1][i] = '#';
	for(int i=1;i<=n;++i)
		for(int j=1;j<=n;++j)
		{
			cin >> tab[i][j];
		}
	bfs();
	cout << solve();
		
}