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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include <iostream>
#include <queue>
#include <list>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

struct Point {
    int x, y;
    Point(int x = -1, int y = -1) : x(x), y(y) {}
};
bool operator==(const Point &p1, const Point &p2) {
    return p1.x == p2.x && p1.y == p2.y;
}
bool operator!=(const Point &p1, const Point &p2) {
    return p1.x != p2.x || p1.y != p2.y;
}
bool operator<(const Point &p1, const Point &p2) {
    if (p1.y == p2.y) {
        return p1.x < p2.x;
    }
    return p1.y < p2.y;
};

struct Value {
    int dist = 10000;
    long long paths = 0;
};

int n, k;
Value** board;
queue<Point> q;
vector<Point> one;
vector<Point> two;
vector<Point> three;
vector<Point> four;
long long result = 0;
long long MAX = 1000000007;
long long twoPaths = 0;

long long mod(long long number) {
   return number % MAX;
}

void add(long long number) {
    result += number;
    result = mod(result);
}

long long multiply(long long number1, long long number2) {
   long long number = number1 * number2;
   return mod(number);
}

void loadBoard() {
    board = new Value*[n];
    for(int i = 0; i < n; ++i) {
        board[i] = new Value[n];
    }

    char c;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> c;
            if (c == '#') {
                board[i][j].dist = 0;
                q.push(Point{i,j}); 
            }
        }
    }
}

void neighbors(const Point &p, Point* array, int &size, int d = -1) {
    size = 0;
    if (p.y > 0 && (board[p.x][p.y-1].dist == d || d < 0)) {
        array[size++]=(Point{p.x,p.y-1});
    }
    if (p.x > 0 && (board[p.x-1][p.y].dist == d || d < 0)) {
        array[size++]=(Point{p.x-1,p.y});
    }
    if (p.x < n-1 && (board[p.x+1][p.y].dist == d || d < 0)) {
        array[size++]=(Point{p.x+1,p.y});
    }
    if (p.y < n-1 && (board[p.x][p.y+1].dist == d || d < 0)) {
        array[size++]=(Point{p.x,p.y+1});
    }
}

int neighborsWithDistSum(const Point &p, int d, Point* ps) {
    int size;
    neighbors(p, ps, size, d);
    return size;
}

int intersectionSize(const Point* a1, int size1, const Point* a2, int size2) {
    vector<Point> v;
    set_intersection(a1,a1+size1,a2,a2+size2,back_inserter(v));
    return v.size();
}

int sumSize(const Point* a1, int size1, const Point* a2, int size2) {
    set<Point> sum;
    sum.insert(a1, a1+size1);
    sum.insert(a2, a2+size2);
    return sum.size();
}

void fillBoard() {
    Point ns[4];
    int nsSize;
    while(!q.empty()) {
        Point p = q.front();
        q.pop(); 
        if (board[p.x][p.y].dist < k) {
            neighbors(p, ns, nsSize);
            for (int i = 0; i < nsSize; i++) {
                Point n = ns[i];
                if (board[n.x][n.y].dist == 10000) {
                    q.push(n);
                }
                if (board[n.x][n.y].dist > board[p.x][p.y].dist) {
                    board[n.x][n.y].dist = board[p.x][p.y].dist+1;
                    if (board[n.x][n.y].dist == 1) {
                        board[n.x][n.y].paths = 1;
                    } else {
                        board[n.x][n.y].paths+= board[p.x][p.y].paths;
                    }
                }
            }
        }
    } 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            switch (board[i][j].dist) {
                case 1: one.push_back(Point{i,j}); break;
                case 2: two.push_back(Point{i,j}); break;
                case 3: three.push_back(Point{i,j}); break;
                case 4: four.push_back(Point{i,j}); break;
            }
        }
    }
    for (Point p : two) {
        twoPaths += board[p.x][p.y].paths;
    }
}

long long binomial(long long up, long long down) {
    if (up < down) {
        return 0;
    }
    long long number = 1;
    vector<bool> dividers;
    dividers.resize(down-1, true);
    for (long long i = up-down+1; i <= up; i++) {
        long long factor = i;
        for (int j = down; j > 1; j--) {
            if (dividers[j-2] && factor % j == 0) {
                dividers[j-2] = false;
                factor /= j;
            }
        }
        number = multiply(number, factor);
    }
    return number;
}

void variant1() {
    long long result1 = one.size();
    add(result1);
}

void variant11() {
    long long result11 = binomial(one.size(), 2);
    add(result11);
}

void variant12() {
    long long result12 = twoPaths;
    add(result12);
}

void variant111() {
    long long result111 = binomial(one.size(), 3);
    add(result111);
}

void variant112() {
    long long result112 = 0;

    for (Point p : two) {
        long long paths = board[p.x][p.y].paths;
        result112 += multiply(paths, one.size() - paths) + binomial(paths, 2);
    }

    add(result112);
}

void variant122() {
    long long result122 = 0;

    Point ns[4];

    for (Point p : one) {
        int twos = neighborsWithDistSum(p, 2, ns);
        result122 += binomial(twos, 2);
    } 

    add(result122);
}

void variant123() {
    long long result123 = 0;

    for (Point p : three) {
        result123 += board[p.x][p.y].paths;
    }

    add(result123);
}

void variant1111() {
    long long result1111 = binomial(one.size(), k);
    add(result1111);
}

void variant1112() {
    long long result1112 = 0;
    for (Point p : two) {
        long long paths = board[p.x][p.y].paths;
        result1112 += multiply(paths, binomial(one.size() - paths, 2));
        result1112 += multiply(one.size() - paths, binomial(paths, 2));
        result1112 += binomial(paths, 3);
        result1112 = mod(result1112);
    }
    add(result1112);
}

void variant1122() {
    long long result1122 = 0;

    for (Point p : two) {
        long long paths = board[p.x][p.y].paths;
        result1122 += paths * (twoPaths - paths);
    }
    result1122 = mod(result1122 / 2);

    Point ns1[4];
    Point ns2[4];
    int nsSize1, nsSize2;

    for (Point p : two) {
        set<Point> twosByOne;
        neighbors(p, ns1, nsSize1, 1);
        for (int i = 0; i < nsSize1; i++) {
            Point n = ns1[i];
            neighbors(n, ns2, nsSize2, 2);
            twosByOne.insert(ns2, ns2 + nsSize2);
        }
        for (Point n : twosByOne) {
            if (n < p) {
                neighbors(n, ns2, nsSize2, 1);
                int inter = intersectionSize(ns1, nsSize1, ns2, nsSize2);
                int sum = sumSize(ns1, nsSize1, ns2, nsSize2);
                if (inter == 1) {
                    result1122 += one.size() - sum - 1;
                } else if (inter == 2) {
                    result1122 += 2 * (one.size() - sum) - 3;
                }
            }
            result1122 = mod(result1122);
        }
    }

    add(result1122);
}

void variant1123() {
    long long result1123 = 0;

    Point ns[4];

    for (Point p : two) {
        long long paths = board[p.x][p.y].paths;
        result1123 += multiply(neighborsWithDistSum(p,3,ns), multiply(paths, one.size() - paths) + binomial(paths, 2));
        result1123 = mod(result1123);
    }

    add(result1123);
}


void variant1222() {
    long long result1222 = 0;
    Point ns[4];
    for (Point p : one) {
        if (neighborsWithDistSum(p, 2, ns) == 3) {
            result1222 ++;
        }
    }

    add(result1222);
}


void variant1223() {
    long long result1223 = 0;

    Point twos[4];
    Point ns1[4];
    Point ns2[4];
    int twosSize, nsSize1, nsSize2;

    for (Point p : one) {
        neighbors(p, twos, twosSize, 2);
        if (twosSize > 1) {
            for (int i = 0; i < twosSize; i++) {
                Point p1 = twos[i];
                for (int j = 0; j < twosSize; j++) {
                    Point p2 = twos[j];
                    if (p1 < p2) {
                        neighbors(p1, ns1, nsSize1, 3);
                        neighbors(p2, ns2, nsSize2, 3);
                        result1223 += sumSize(ns1, nsSize1, ns2, nsSize2);
                    }
                }
            }
        }
    }

    add(result1223);
}


void variant1233() {
    long long result1233 = 0;
    Point ns[4];
    for (Point p : two) {
        long long paths = board[p.x][p.y].paths;
        result1233 += multiply(paths, binomial(neighborsWithDistSum(p,3,ns), 2));
    }

    add(result1233);
}


void variant1234() {
    long long result1234 = 0;

    for (Point p : four) {
        result1234 += board[p.x][p.y].paths;
    }

    add(result1234);
}

int main() {
    cin.sync_with_stdio(false);
    cin.tie(nullptr);
    
    cin >> n >> k;

    loadBoard();
    fillBoard();
    
    switch(k) {
        case 1:
            variant1();
            break;
        case 2:
            variant11();
            variant12();
            break;
        case 3:
            variant111();
            variant112();
            variant122();
            variant123();
            break;
        case 4:
            variant1111();
            variant1112();
            variant1122();
            variant1123();
            variant1222();
            variant1223();
            variant1233();
            variant1234();
            break;
    }
    
    cout << result << endl;
}