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
#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef set<int> SI;

const int INF = 1000000007;
const int MAXN = 3001;
const LL MOD = 1000000007;

#define FOR(i, b, e) for(int i = b; i <= e; ++i)
#define FORD(i, b, e) for(int i = b; i >= e; --i)
#define REP(i, n) FOR(i, 0, n-1)
#define REV(i, n) FORD(i, n-1, 0)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(c) (int)(c).size()
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define PB push_back
#define PP pop_back
#define MP make_pair
#define ST first
#define ND second

const int mv = 4;
const int xx[mv] = {0, 0, 1, -1};
const int yy[mv] = {1, -1, 0, 0};

int tab[MAXN][MAXN], dist[MAXN][MAXN], odw[MAXN][MAXN];
int n, k;
char c;
LL res;

inline bool inside(int a, int b)
{
	return (1 <= a) && (a <= n) && (1 <= b) && (b <= n);
}

vector<PII> kol;

void BFS()
{
	FOR(i, 1, n)
	FOR(j, 1, n)
	{
		if(tab[i][j] == 1)
		{
			odw[i][j] = true;
			kol.PB(MP(i, j));
		}
	}
	
	REP(i, SIZE(kol))
	{
		PII v = kol[i];
		
		REP(j, mv)
		{
			int a1 = xx[j] + v.ST, b1 = yy[j] + v.ND;
			
			if(!odw[a1][b1] && inside(a1, b1))
			{
				odw[a1][b1] = true;
				dist[a1][b1] = dist[v.ST][v.ND] + 1;
				kol.PB(MP(a1, b1));
			}
		}
	}
}

set<PII> sciezka;

void Search(int aa, int bb)
{
	if(SIZE(sciezka) == k)
	{
		res++;
		res%=MOD;
		return;
	}
	
	FOR(i, aa, n)
	FOR(j, (i == aa ? bb : 1), n)
	{
		if(dist[i][j] > 0 && (sciezka.find(MP(i, j)) == sciezka.end()))
		{
			bool connected = false;
			
			if(dist[i][j] == 1) 
				connected = true;
			else
			{
				REP(x1, mv)
				{
					int a1 = i+xx[x1], b1 = j+yy[x1];
					if(inside(a1, b1) && sciezka.find(MP(a1, b1)) != sciezka.end()) connected = true;
				}
			}
			
			if(connected) 
			{
				sciezka.insert(MP(i, j));
				//~ cout<<"("<<i<<", "<<j<<", "<<SIZE(sciezka)<<")\n";
				Search(i, j);
				sciezka.erase(MP(i, j));
			}
		}
	}
}

int main()
{
	ios::sync_with_stdio(0);
	cin>>n>>k;
	FOR(i, 1, n)
	FOR(j, 1, n)
	{
		cin>>c;
		tab[i][j] = (c == '#' ? 1 : 0);
	}	
	
	//wypisywanie
	//~ FOR(i, 1, n)
	//~ {
		//~ FOR(j, 1, n)
		//~ {
			//~ cout<<tab[i][j]<<" ";
		//~ }	
		//~ cout<<"\n";
	//~ }
	//~ cout<<endl;
	
	BFS();
	
	//~ FOR(i, 1, n)
	//~ {
		//~ FOR(j, 1, n)
		//~ {
			//~ cout<<dist[i][j]<<" ";
		//~ }	
		//~ cout<<"\n";
	//~ }
	
	Search(1, 1);
	
	cout<<res<<"\n";
	return 0;
}

/*
Test przykladowy:

3  2
.#.
##.
#..
* 
3  4
.#.
##.
#..

*/

/*
Testy moje



4 4
....
####
#.#.
#.#.

12 1
............
............
............
............
..##.#......
.#.###......
.#.###......
######......
######......
.#####......
..##.#......
..####......

12 2
............
............
............
............
..##.#......
.#.###......
.#.###......
######......
######......
.#####......
..##.#......
..####......

12 3
............
............
............
............
..##.#......
.#.###......
.#.###......
######......
######......
.#####......
..##.#......
..####......

12 4
............
............
............
............
..##.#......
.#.###......
.#.###......
######......
######......
.#####......
..##.#......
..####......














*/