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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include <cstdio>
#include <vector>
using namespace std;
typedef long long LL;

const int mod   = 1000000007;
const int inv2  = 500000004;
const int inv6  = 166666668;
const int inv24 = 41666667;

int n;
int t[3005][3005];

int dr[4] = {0, 1, 0, -1};
int dc[4] = {1, 0, -1, 0};

int squared(int x) {
    return x*x;
}

int C2(int x) {
    return LL(LL(x)*(x-1)%mod)*inv2%mod;
}

int C3(int x) {
    return LL(LL(LL(x)*(x-1)%mod)*(x-2)%mod)*inv6%mod;
}

int C4(int x) {
    return LL(LL(LL(LL(x)*(x-1)%mod)*(x-2)%mod)*(x-3)%mod)*inv24%mod;
}

template<int N>
inline int neigh(int x, int y) {
    return (t[x+1][y] == N) + (t[x-1][y] == N) + (t[x][y+1] == N) + (t[x][y-1] == N);
}

bool inside(int r, int c) {
    return r > 0 && r <= n && c > 0 && c <= n;
}




vector<pair<int, int>> d[5];
int d_cnt[5];

void bfs() {
    vector<pair<int, int>> v;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            if(t[i][j] == 0)
                v.emplace_back(i, j);

    for(int i = 0; i < int(v.size()); i++) {
        for(int k = 0; k < 4; k++) {
            int r = v[i].first  + dr[k];
            int c = v[i].second + dc[k];

            if(t[r][c] == -1) {
                t[r][c] = t[v[i].first][v[i].second] + 1;
                v.emplace_back(r, c);
                if(t[r][c] <= 4) {
                    d[t[r][c]].emplace_back(r, c);
                    d_cnt[t[r][c]]++;
                }
            }
        }
    }
}




int c1111() {
    return C4(d_cnt[1]);
}


int c1112() {
    int ret = 0;
    for(auto i : d[2]) {
        int cnt = neigh<1>(i.first, i.second);
        if(cnt == 1) ret = (ret + C2(d_cnt[1]-1)) % mod;
        if(cnt == 2) ret = (ret + LL(C2(d_cnt[1]-2))*2 + d_cnt[1]-2) % mod;
        if(cnt == 3) ret = (ret + LL(C2(d_cnt[1]-3))*3 + LL(d_cnt[1]-3)*3 + 1) % mod;
        if(cnt == 4) ret = (ret + LL(C2(d_cnt[1]-4))*4 + LL(d_cnt[1]-4)*6 + 4) % mod;
    }
    return ret;
}


int c1122() {
    int ret = 0;
    for(auto i : d[2]) ret += neigh<1>(i.first, i.second);
    ret = (LL(ret)*ret) % mod;
    for(auto i : d[2]) ret = (ret - squared(neigh<1>(i.first, i.second)) + mod) % mod;
    ret = (LL(ret)*inv2) % mod;

    for(auto i : d[2]) {
        for(int k = 0; k < 2; k++) {
            int r1 = i.first  + dr[k];
            int c1 = i.second + dc[k];
            int r2 = i.first  + dr[k]*2;
            int c2 = i.second + dc[k]*2;
            int r3 = i.first  + dr[k] + dr[k+1];
            int c3 = i.second + dc[k] + dc[k+1];
            int r4 = i.first  + dr[k+1];
            int c4 = i.second + dc[k+1];

            if(t[r1][c1] == 2) {
                ret = (ret - neigh<1>(i.first, i.second)*neigh<1>(r1, c1) + mod) % mod;
                int cnt = neigh<1>(i.first, i.second) + neigh<1>(r1, c1);
                ret = (ret + cnt*(cnt-1)/2 + LL(d_cnt[1]-cnt)*cnt) % mod;
            }

            if(inside(r2, c2) && t[r2][c2] == 2 && t[r1][c1] == 1) {
                ret = (ret - neigh<1>(i.first, i.second)*neigh<1>(r2, c2) + mod) % mod;
                ret = (ret + d_cnt[1]-1 + (neigh<1>(i.first, i.second)-1)*(neigh<1>(r2, c2)-1)) % mod;
            }

            int cnt_ = (t[r1][c1] == 1) + (t[r4][c4] == 1);

            if(t[r3][c3] == 2 && cnt_ > 0) {
                ret = (ret - neigh<1>(i.first, i.second)*neigh<1>(r3, c3) + mod) % mod;
                ret = (ret + (cnt_ == 2) + (d_cnt[1]-cnt_)*cnt_ + (neigh<1>(i.first, i.second)-cnt_)*(neigh<1>(r3, c3)-cnt_)) % mod;
            }
        }
    }
    return ret;
}


int c1123() {
    int ret = 0;
    for(auto i : d[3]) {
        for(int k = 0; k < 4; k++) {
            int r = i.first  + dr[k];
            int c = i.second + dc[k];
            if(t[r][c] == 2) {
                int cnt = neigh<1>(r, c);
                if(cnt == 1) ret = (ret + d_cnt[1]-1) % mod;
                if(cnt == 2) ret = (ret + LL(d_cnt[1]-2)*2 + 1) % mod;
                if(cnt == 3) ret = (ret + LL(d_cnt[1]-3)*3 + 3) % mod;
            }
        }
    }
    return ret;
}

int c1222() {
    int ret = 0;
    for(auto i : d[1]) {
        if(neigh<2>(i.first, i.second) == 3) ret++;

        for(int k = 0; k < 4; k++) {
            int r1 = i.first  + dr[k];
            int c1 = i.second + dc[k];
            int r2 = i.first  + dr[(k+1)&3];
            int c2 = i.second + dc[(k+1)&3];
            if(t[r1][c1] == 2 && t[r2][c2] == 2) {
                ret += neigh<2>(r1, c1) + neigh<2>(r2, c2);
                int r3 = i.first  + dr[k] + dr[(k+1)&3];
                int c3 = i.second + dc[k] + dc[(k+1)&3];
                if(t[r3][c3] == 2) ret--;
            }

        }

        for(int k = 0; k < 2; k++) {
            int r1 = i.first  + dr[k];
            int c1 = i.second + dc[k];
            int r2 = i.first  + dr[(k+2)&3];
            int c2 = i.second + dc[(k+2)&3];
            if(t[r1][c1] == 2 && t[r2][c2] == 2)
                ret += neigh<2>(r1, c1) + neigh<2>(r2, c2);
        }

        for(int k = 0; k < 4; k++) {
            int r1 = i.first  + dr[k];
            int c1 = i.second + dc[k];
            if(t[r1][c1] == 2) {
                if(neigh<2>(r1, c1) == 2) ret++;
                if(neigh<2>(r1, c1) == 3) ret += 3;
                for(int k_ = 0; k_ < 4; k_++) {
                    int r2 = r1 + dr[k_];
                    int c2 = c1 + dc[k_];
                    if(t[r2][c2] == 2)
                        ret += neigh<2>(r2, c2)-1;
                }
            }
        }
    }
    return ret;
}

int c1223() {
    int ret = 0, sq = 0;
    for(auto i : d[2]) {

        for(int k = 0; k < 4; k++) {
            int r1 = i.first  + dr[k];
            int c1 = i.second + dc[k];
            int r2 = i.first  + dr[(k+2)&3];
            int c2 = i.second + dc[(k+2)&3];
            if(t[r1][c1] == 1 && t[r2][c2] == 3)
                ret += neigh<2>(r1, c1) + neigh<2>(r2, c2) + neigh<2>(i.first, i.second) - 2;
        }

        for(int k = 0; k < 4; k++) {
            int r1 = i.first  + dr[k];
            int c1 = i.second + dc[k];
            int r2 = i.first  + dr[(k+1)&3];
            int c2 = i.second + dc[(k+1)&3];
            if(t[r1][c1] * t[r2][c2] == 3) {
                ret += neigh<2>(r1, c1) + neigh<2>(r2, c2) + neigh<2>(i.first, i.second) - 2;
                int r3 = i.first  + dr[k] + dr[(k+1)&3];
                int c3 = i.second + dc[k] + dc[(k+1)&3];
                if(t[r3][c3] == 2) ret -= 2, sq++;
            }
        }

    }
    return ret + sq/2;
}

int c1233() {
    int ret = 0;
    for(auto i : d[2]) {
        if(neigh<3>(i.first, i.second) == 2) ret += neigh<1>(i.first, i.second);
        if(neigh<3>(i.first, i.second) == 3) ret += neigh<1>(i.first, i.second)*3;

        for(int k = 0; k < 4; k++) {
            int r = i.first  + dr[k];
            int c = i.second + dc[k];
            if(t[r][c] == 3)
                ret += neigh<3>(r, c);
        }
    }
    return ret;
}

int c1234() {
    int ret = 0;
    for(auto i : d[2])
        for(int k = 0; k < 4; k++) {
            int r = i.first  + dr[k];
            int c = i.second + dc[k];
            if(t[r][c] == 3)
                ret += neigh<1>(i.first, i.second) * neigh<4>(r, c);
        }
    return ret;
}

int c111() {
    return C3(d_cnt[1]);
}

int c112() {
    int ret = 0;
    for(auto i : d[2]) {
        int cnt = neigh<1>(i.first, i.second);
        if(cnt == 1) ret = (ret + d_cnt[1]-1) % mod;
        if(cnt == 2) ret = (ret + LL(d_cnt[1]-2)*2 + 1) % mod;
        if(cnt == 3) ret = (ret + LL(d_cnt[1]-3)*3 + 3) % mod;
        if(cnt == 4) ret = (ret + LL(d_cnt[1]-4)*4 + 6) % mod;
    }
    return ret;
}

int c122() {
    int ret = 0;
    for(auto i : d[1]) {
        if(neigh<2>(i.first, i.second) == 2) ret++;
        if(neigh<2>(i.first, i.second) == 3) ret += 3;

        for(int k = 0; k < 4; k++) {
            int r = i.first  + dr[k];
            int c = i.second + dc[k];
            if(t[r][c] == 2)
                ret += neigh<2>(r, c);
        }
    }
    return ret;
}

int c123() {
    int ret = 0;
    for(auto i : d[2])
        ret += neigh<1>(i.first, i.second) * neigh<3>(i.first, i.second);
    return ret;
}

int c11() {
    return C2(d_cnt[1]);
}

int c12() {
    int ret = 0;
    for(auto i : d[2])
        ret += neigh<1>(i.first, i.second);
    return ret;
}

int c1() {
    return d_cnt[1];
}

int main() {

    int k;
    scanf("%i%i", &n, &k);
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++) {
            char c;
            scanf(" %c", &c);
            t[i][j] = (c == '#' ? 0 : -1);
        }
    bfs();

    if(k == 1) printf("%i\n", c1());
    if(k == 2) printf("%i\n", (LL(c11())+c12()) % mod);
    if(k == 3) printf("%i\n", (LL(c111())+c112()+c122()+c123()) % mod);
    if(k == 4) printf("%i\n", (LL(c1111())+LL(c1112())+LL(c1122())+LL(c1123())+LL(c1222())+LL(c1223())+LL(c1233())+LL(c1234())) % mod);
}
/*
4 4
....
....
..##
....
*/