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

using namespace std;

#define fru(j,n) for(int j=0; j<(n); ++j)
#define tr(it,v) for(auto it=(v).begin(); it!=(v).end(); ++it)
#define fi first
#define se second
#define pb push_back
#define ALL(G) (G).begin(),(G).end()

//#define DEBUG
#ifdef DEBUG
#define DEB printf
#else
#define DEB(...)
#endif

typedef double D;
typedef long long LL;
typedef pair<short,short> pss;
typedef pair<char,char> pcc;
typedef vector<int> vi;

const int mod = 1000000007;
const int MAXN = 3010;

int odwr[5];

int CC(int n,int k){
	 int ret=odwr[k];
	 fru(i,k) ret=1LL*ret*(n-i)%mod;
	 assert(ret>=0);
	 return ret;
}

int n,kk,d;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};

int dx6[2][6]={{-1,-1, 0,0,1,1},{-1,0,0,1,1,2}};
int dy6[2][6]={{ 0, 1,-1,2,0,1},{0,-1,1,-1,1,0}};

pss Q[MAXN*MAXN];
char W[MAXN][MAXN];
char spos[MAXN][MAXN];
char R[MAXN][MAXN];
#define x (xx+dx[k])
#define y (yy+dy[k])

vector<vector<pcc>> MV3;

vector<vector<pcc>> MV2;
vector<vector<pcc>> BD2;

int go(int xx, int yy, int a1, int a2, int a3){
	 int w[6],ret=0;
	 tr(ve,MV3){
		  w[1]=w[2]=w[3]=0;
		  tr(it,*ve) w[W[xx+it->fi][yy+it->se]]++;
		  if(w[1]==a1 && w[2]==a2 && w[3]==a3) {
				tr(it,*ve) DEB("%d %d\n",it->fi,it->se);
				++ret;
		  }
	 }
	 return ret;
}

int go2(int xx,int yy, int a1, int a2,bool spec=0){
	 int w[6];
	 LL ret=0;
	 fru(i,MV2.size()){
		  w[1]=w[2]=0;
		  tr(it,MV2[i]) w[W[xx+it->fi][yy+it->se]]++;
		  if(w[1]==a1 && w[2]==a2){
				if(spec){
					 ret++;
					 continue;
				}
				int t=0;
		  		tr(it,BD2[i]) if(W[xx+it->fi][yy+it->se]==1) ++t;
				int add=d-t-a1-(W[xx][yy]==1);
				ret+=add;
		  }
	 }
	 return ret%mod;
}

int solve(){
	 int sum2=0;
	 fru(i,n) fru(j,n) 
		  if(W[i][j]==2) sum2+=spos[i][j];
	 	  else if(W[i][j]==1) ++d;

	 if(kk==1) return d;
	 if(kk==2)
		  return (CC(d,2)+sum2)%mod;
	 if(kk==3){
		  LL ret=CC(d,3);
		  fru(i,n) fru(j,n) if(W[i][j]==2){
				ret+=1LL*spos[i][j]*(d-spos[i][j])%mod+CC(spos[i][j],2);
		  }
		  else if(W[i][j]==3){
				ret+=spos[i][j];
		  }
		  else if(W[i][j]==1) ret+=go2(i,j,0,2,1);
		  return ret%mod;
	 }
	 LL ret=CC(d,4),ret2=0;
	 int d3=CC(d,3);
	 fru(i,n) fru(j,n) {
		   if(W[i][j]==4){
		  ret+=spos[i][j];//1234
	 }
	 else if(W[i][j]==3){
		  ret+=go(i,j,1,2,0);//1223
		  ret+=go(i,j,2,1,0);//1123 (B1)
		  ret+=go2(i,j,1,1); //1123 (B2)
	 }
	 else if(W[i][j]==2){
		  ret+=go(i,j,1,0,2); //1233
		  ret=(ret+mod+d3-CC(d-spos[i][j],3))%mod; //1112
		  ret2+=go(i,j,2,1,0);// 1122 (A1)
	 }
	 else if(W[i][j]==1){
		  ret+=go(i,j,0,3,0); //1222
		  ret+=go2(i,j,0,2); //1122 (A2)
	 }
	 }
				assert(ret>=0);
	 if(1) fru(xx,n) fru(yy,n) if(W[xx][yy]==2) fru(k,4) if(W[x][y]==1){
		  int r=sum2-1;
		  int a=x,b=y;
		  if(k%2==1) a=min(x,xx);
		  else b=min(y,yy);
		  fru(e,6) r-=R[dx6[k&1][e]+a][dy6[k&1][e]+b];
		   if(k%2==0) {
				 if(x>0) if(W[x-1][y]+W[x-1][yy]==3) ++r;
				 if(x+1<n) if(W[x+1][y]+W[x+1][yy]==3) ++r;
			}
			else {
				 if(y>0) if(W[x][y-1]+W[xx][y-1]==3) ++r;
				 if(y+1<n) if(W[x][y+1]+W[xx][y+1]==3) ++r;
			}
			ret2+=r; //1122 (A3)
	 }
	 fru(i,n) fru(j,n) if(W[i][j]==2) assert(spos[i][j]==R[i][j]);
	 ret2%=mod;
	 ret2=1LL*ret2*odwr[2]%mod;
	 DEB("ret = %lld ret2 = %lld\n",ret,ret2);
	 return (ret+ret2)%mod;
}
int qp(int a,int b){
	 if(b==0) return 1;
	 int yy=qp(a,b/2);
	 yy=1LL*yy*yy%mod;
	 if(b%2) yy=1LL*yy*a%mod;
	 return yy;
}

set<set<pcc>> VIS;

void ruchy(set<pcc> S){
	 if(VIS.find(S)!=VIS.end()) return;
	 VIS.insert(S);
	 if(S.size()==3){
		  vector<pcc> RR;
		  tr(it,S) if(*it!=pcc(0,0)) RR.pb(*it);
		  MV2.pb(RR);
		  BD2.pb({});
		  set<pcc> sas;
	 	  tr(it,S) fru(k,4) sas.insert(pcc(it->fi+dx[k],it->se+dy[k]));
		  tr(it,sas) if(S.find(*it)==S.end()) BD2.back().pb(*it);
	 }
	 if(S.size()==4){
		  vector<pcc> RR;
		  tr(it,S) if(*it!=pcc(0,0)) RR.pb(*it);
		  MV3.pb(RR);
		  return;
	 }
	 tr(it,S)
		  fru(k,4){
		  pcc q(it->fi+dx[k],it->se+dy[k]);
		  if(S.find(q)!=S.end()) continue;
		  auto S2=S;
		  S2.insert(q);
		  ruchy(S2);
	 }
}

char S[MAXN][MAXN];
int main(){
	 ruchy(set<pcc>({{0,0}}));
	 sort(ALL(MV3));
	 scanf("%d%d",&n,&kk);
	 fru(i,MAXN) fru(j,MAXN) S[i][j]='.';
	 fru(i,n) scanf("%s",S[i+5]+5);
	 fru(i,n) S[i+5][n+5]='.';
	 int qs=0;
	 int sil=1;
	 for(int i=1;i<5;++i) {
		  sil*=i;
		  odwr[i]=qp(sil,mod-2);
		  assert(1LL*sil*odwr[i]%mod==1);
	 }
	 fru(i,n+10) fru(j,n+10) W[i][j]=5;
	 fru(i,n) fru(j,n) W[i+5][j+5]=-1;
	 fru(i,n) fru(j,n) if(S[i+5][j+5]=='#') {
		  Q[qs++]=pss(i+5,j+5);
		  W[i+5][j+5]=0;
	 }
	 n+=10;
	 fru(o,qs){
		  int xx=Q[o].fi,yy=Q[o].se;
		  if(W[xx][yy]==1) spos[xx][yy]=1;
		  fru(k,4) {
				if(W[x][y]==-1){
					 W[x][y]=min(5,W[xx][yy]+1);
					 Q[qs++]=pss(x,y);
				}
				if(W[x][y]==W[xx][yy]+1){
					 spos[x][y]+=spos[xx][yy];
				}
		  }
	 }
	 fru(xx,n) fru(yy,n) if(W[xx][yy]==2) fru(k,4) if(W[x][y]==1){
		  ++R[xx][yy];
		  ++R[x][y];
	 }
	 printf("%d\n",solve());
	 return 0;
}