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
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <assert.h>

using namespace std;
typedef long long int llint;

/*

3 2
##.
...
..#

12

3 2
.#.
##.
#..

8

*/

#define MOD (1000*1000*1000+7)
#define MAX 3000
#define MAXK 4
int n,k;
char tab[MAX][MAX];
int cnt[MAXK];
int neighbor_cnt[MAXK];
bool neighbor_mask[MAX][MAX];

bool has_neighbor_check(int i, int j, char c)
{
	if( 0<=i&&i<n&&
		0<=j&&j<n&&
		tab[i][j]==c)
	{
		neighbor_mask[i][j]=true;
		return true;
	}
	return false;
}

bool has_neighbor(int i, int j, char c)
{
	const bool n0=has_neighbor_check(i-1,j,c);
	const bool n1=has_neighbor_check(i+1,j,c);
	const bool n2=has_neighbor_check(i,j-1,c);
	const bool n3=has_neighbor_check(i,j+1,c);
	return n0||n1||n2||n3;
}

bool k2_check(int i, int j)
{
	if( 0<=i&&i<n&&
		0<=j&&j<n&&
		tab[i][j]=='1')
	{
		return true;
	}
	return false;
}

int k2(int i, int j)
{
	if(tab[i][j]!='0')return 0;
	const bool n0=k2_check(i-1,j);
	const bool n1=k2_check(i+1,j);
	const bool n2=k2_check(i,j-1);
	const bool n3=k2_check(i,j+1);
	return n0+n1+n2+n3;
}

extern llint kombinacje(int _k, int _n);
llint kombinacje(int _k, int _n)
{
	if(_k>_n)
		return 0;

	llint res=1;
	for(int i=_n;i>=_n-_k+1;--i)
	{
		res*=i;
		res%=MOD;
	}
	for(int i=1;i<=_k;++i)
	{
		res/=i;
		res%=MOD;
	}
	return res;
}

int main()
{
	cin>>n>>k;
	for(int i=0;i<n;++i)
		for(int j=0;j<n;++j)
			cin>>tab[i][j];

	for(int ki=0;ki<4;++ki)
	{
		for(int i=0;i<n;++i)
			for(int j=0;j<n;++j)
				neighbor_mask[i][j]=false;
		cnt[ki]=0;

		const char k_neighbor=ki?('0'+ki-1):'#';
		const char k_value='0'+ki;
		for(int i=0;i<n;++i)
			for(int j=0;j<n;++j)
				if(tab[i][j]=='.'&&has_neighbor(i,j,k_neighbor))
				{
					tab[i][j]=k_value;
					assert(0<=ki&&ki<4);
					++cnt[ki];
				}

		neighbor_cnt[ki]=0;
		for(int i=0;i<n;++i)
			for(int j=0;j<n;++j)
				neighbor_cnt[ki]+=(neighbor_mask[i][j]==true);
	}
	
	llint res=0;
	if(k==1)
		res=cnt[0];
	else //if (k==2)
	{
		res=kombinacje(k,cnt[0]);
		for(int i=0;i<n;++i)
			for(int j=0;j<n;++j)
	       	res+=k2(i,j);
		res%=MOD;
	}

	cout<<res;
	
	return 0;
}