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
#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define ll long long
char p[3002][3002];
int t[3002][3002];
queue<pair<int,int> > Q,Q1;
int dx[4]={1,0,-1,0}, dy[4]={0,1,0,-1};
int t2[10];

int main() {
	int n,k;
	scanf("%d%d",&n,&k);
	for (int i=1;i<=n;i++) {
		scanf("%s",p[i]+1);
		for (int j=1;j<=n;j++)
			if (p[i][j]=='#')
				Q.push(make_pair(i,j));
	}
	while (!Q.empty()) {
		pair<int,int> s=Q.front(); Q.pop();
		//printf("%d%d\n",s.first,s.second);
		int i=s.first, j=s.second;
		for (int d=0;d<4;d++){
			if (p[i+dx[d]][j+dy[d]]=='.') {
				p[i+dx[d]][j+dy[d]]='1';
				Q1.push(make_pair(i+dx[d],j+dy[d]));
			}
		}
	}
	ll a1=Q1.size();
	if (k!=2 || a1==0) {
		printf("%lld\n",a1);
		return 0;
	}
	while (!Q1.empty()) {
		pair<int,int> s=Q1.front(); Q1.pop();
		//printf("%d%d\n",s.first,s.second);
		int i=s.first, j=s.second;
		for (int d=0;d<4;d++){
			if (p[i+dx[d]][j+dy[d]]=='.' || p[i+dx[d]][j+dy[d]]=='2') {
				p[i+dx[d]][j+dy[d]]='2';
				t[i+dx[d]][j+dy[d]]++;
			}
		}
	}
	//for (int i=1;i<=n;i++) puts(p[i]+1);
	for (int i=1;i<=n;i++) {
		for (int j=1;j<=n;j++)
			if (p[i][j]=='2')
				t2[t[i][j]]++;
	}
	
	ll a2=a1*(a1-1)/2%M;
	for (int i=0;i<9;i++)
		a2 = (a2+t2[i]*i)%M;
	printf("%lld\n",a2);
	return 0;
}