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
#include <cstdlib>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <array>

using namespace std;

int n, k;
int** ar;
vector<int> moves;
vector<int> reservedMoves(9000002);
map< set<int>, short> solutions;
set<int> sol;

void printBoard()
{
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
			std::cout << ar[j][i];
		cout << "\n";
	}
}

bool inField(int r, int c)
{
	if (r < 0 || r >= n) return false;
	if (c < 0 || c >= n) return false;
	return true;
}
const int yy[] = {  -1,  1,  0, 0 };	
const int xx[] = {   0,  0, -1, 1 };

bool hasNeigbour(int x, int y)
{
	for (int i = 0; i < 4; i++)
		if (inField(x + xx[i], y + yy[i]) && ar[x + xx[i]][y + yy[i]] == 1)
			return true;
	return false;
}

bool isPossible(int x, int y)
{
	if (ar[x][y] == 0 && hasNeigbour(x, y))
		return true;
	return false;
}
int createMoves(int fieldId)
{
	int howMany = 0;
	int x = fieldId % n;
	int y = fieldId / n;
	ar[x][y]++;

	for (int j = 0; j < 4; j++)
	{
		int newMove = (y + yy[j])*n + (x + xx[j]);
		if (inField(x + xx[j], y + yy[j]) && ar[x + xx[j]][y + yy[j]] == 0 && reservedMoves[newMove] == 0)
		{
			howMany++;
			reservedMoves[newMove] = 1;
			moves.push_back(newMove);
		}
	}

	sol.insert(fieldId);

	return howMany;
}

void revertMoves(int fieldId, int howMany)
{
	for (int i = 0;i < howMany;i++)
	{
		reservedMoves[moves.back()] = 0;
		moves.pop_back();
	}

	int x = fieldId % n;
	int y = fieldId / n;
	ar[x][y]--;
	sol.erase(fieldId);
}

int solve2()
{
	long long total = 0;
	int x, y;

	for (int i = 0; i < moves.size(); i++)
	{
		total+=moves.size()-i-1;
		x = moves[i] % n; 
		y = moves[i] / n;

		for (int j = 0; j < 4; j++)
		{
			if (inField(x + xx[j], y + yy[j]) && ar[x + xx[j]][y + yy[j]]==0 && reservedMoves[ (y+yy[j])*n+ x + xx[j]]==0)
				total++;
		}
	}

	return total % 1000000007;
}


int solve3()
{
	for (int i = 0; i < moves.size(); i++) //first move
	{
		int howMany1 = createMoves(moves[i]);
		for (int j = i + 1; j < moves.size(); j++) //second move
		{
			int howMany2 = createMoves(moves[j]);
			for (int k = j + 1; k < moves.size(); k++) //third move
			{
				sol.insert(moves[k]);
				solutions.insert(std::pair< set<int>, short>(sol, 1));
				sol.erase(moves[k]);
			}
			revertMoves(moves[j], howMany2);
		}
		revertMoves(moves[i], howMany1);
	}
	return solutions.size() % 1000000007;
}

int solve4()
{
	for (int i = 0; i < moves.size(); i++) //first move
	{
		int howMany1 = createMoves(moves[i]);
		for (int j = i + 1; j < moves.size(); j++) //second move
		{
			int howMany2 = createMoves(moves[j]);
			for (int k = j + 1; k < moves.size(); k++) //second move
			{
				int howMany3 = createMoves(moves[k]);
				for (int l = k + 1; l < moves.size(); l++) //second move
				{
					sol.insert(moves[l]);
					solutions.insert(std::pair< set<int>, short>(sol, 1));
					sol.erase(moves[l]);
				}
				revertMoves(moves[k], howMany3);
			}
			revertMoves(moves[j], howMany2);
		}
		revertMoves(moves[i], howMany1);
	}
	return solutions.size() % 1000000007;
}

int main()
{
	cin >> n >> k;
	ar = new int*[n]; for (int i = 0; i < n; ++i) ar[i] = new int[n];
	char c; for (int i = 0; i < n; i++)for (int j = 0; j < n; j++) { cin >> c; if (c=='.')ar[j][i] = 0;else ar[j][i] = 1;}

	for (int i = 0; i < n*n; i++){int x = i%n; int y = i / n;if (isPossible(x, y)){moves.push_back(i);reservedMoves[i] = 1;}}

	if (k == 1)	cout << moves.size() << endl;
	if (k == 2) cout << solve2() << endl;
	if (k == 3) cout << solve3() << endl;
	if (k == 4) cout << solve4() << endl;

    return 0;
}