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
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;

const int MOD = 1000000007;
auto neightbours = {make_pair(0, 1), make_pair(0, -1), make_pair(1, 0), make_pair(-1, 0)};
int n, k;

int to_coord(int row, int col) {
    return n * row + col;
}

int to_row(int x) {
    return x / n;
}

int to_col(int x) {
    return x % n;
}

long long newt(long long x, int comb) {
    long long num = 1, den = 1;
    if(x < comb) return 0;
    x %= MOD;
    while(comb) {
        num *= x--;
        num %= MOD;
        den *= comb--;
    }
    return num / den;
}

struct Field {
    bool occupied = false;
    bool visited = false;
    int dist = 0;
    short from = 0;
};

int main() {
    scanf("%d %d", &n, &k);
    char row[3001];
    vector<Field> board;
    board.resize(n * n);
    int start, coord;
    for(int i = 0; i < n; ++i) {
        scanf("%s", row);
        for(int j = 0; j < n; ++j) {
            coord = to_coord(i, j);
            if (row[j] == '#') {
                board[coord].occupied = true;
                start = coord;
            }
        }
    }
    int x, y, xx, yy, cur, next;
    long long dist[] = {0, 0, 0, 0, 0, 0}; // indeksem jest dist
    queue<int> starters, bfs;
    starters.push(start);
    board[start].visited = true;
    // znajdz miejsca startowe i bfs dla dist = 1
    while(!starters.empty()) {
        cur = starters.front();
        starters.pop();
        x = to_row(cur);
        y = to_col(cur);
        for(auto diff : neightbours) {
            xx = x + diff.first, yy = y + diff.second;
            if(xx >= 0 && xx < n && yy >= 0 && yy < n) {
                next = to_coord(xx, yy);
                if (!board[next].visited) {
                    board[next].visited = true;
                    if (board[next].occupied) {
                        starters.push(next);
                    } else {
                        board[next].dist = 1;
                        board[next].from = 1; // because 
                        bfs.push(next);
                        dist[1] += 1;
                    }
                }
            }
        }
    }
    long long fork3 = 0, reps3 = 0;;
    int adj2, adj3;
    int cur_dist, next_dist;
    // wlasciwy bfs dla dist <= k
    while(!bfs.empty()) {
        cur = bfs.front();
        bfs.pop();
        cur_dist = board[cur].dist;
        if (cur_dist > k) {
            break;
        }
        x = to_row(cur);
        y = to_col(cur);
        //printf("(%d, %d) dist=%d from=%d", x, y, board[cur].dist, board[cur].from);
        adj2 = adj3 = 0;
        for(auto diff : neightbours) {
            xx = x + diff.first, yy = y + diff.second;
            if(xx >= 0 && xx < n && yy >= 0 && yy < n) {
                next = to_coord(xx, yy);
                if(!board[next].visited) {
                    board[next].visited = true;
                    board[next].dist = cur_dist + 1;
                    bfs.push(next);
                }
                next_dist = board[next].dist;
                if(next_dist > cur_dist) {
                    board[next].from += board[cur].from;
                    dist[next_dist] += board[cur].from;
                    dist[next_dist] %= MOD;
                }
                if (next_dist == 2) adj2++;
                if (next_dist == 3) adj3++;
            }
        }
        if(k == 3) {
            if(cur_dist == 1) {
                if(adj2 == 2) fork3 += 1;
                if(adj2 == 3) fork3 += 3;
                fork3 %= MOD;
            }
            if(cur_dist == 2) {
                reps3 += (board[cur].from - 1) * board[cur].from / 2;
                reps3 %= MOD;
                fork3 += board[cur].from * adj2;
                fork3 %= MOD;
            }
        }
        //printf("\n");
    }
    long long result = 0;
    //for(int i = 1; i <= k; ++i) {printf("k=%d: %lld\n", i, dist[i]);}
    if (k == 1) {
        result = dist[1]; // d == 1
    } else if (k == 2) {
        result = dist[2]; // d == 2
        //printf("%lld\n", result);
        result += newt(dist[1], 2); // 2 x d == 1
        //printf("%lld\n", result);
    } else if (k == 3) {
        result = dist[3]; // d == 3
        //printf("%lld\n", result);
        result += dist[2] * (dist[1] - 1); 
        result %= MOD;
        result += MOD - reps3;
        result %= MOD;
        //printf("%lld\n", result);
        result += newt(dist[1], 3); // 3 * d == 1
        result %= MOD;
        //printf("%lld\n", result);
        result += fork3;
    } else if (k == 4) {
        result = dist[4]; // d == 4
        //printf("%lld\n", result);
        result += dist[3] * (dist[1] - 1); // d == 3 * d == 1
        result %= MOD;
        //printf("%lld\n", result);
        result += dist[2] * newt(dist[1] - 1, 2); // d == 2 i 2 * d == 1
        result %= MOD;
        //printf("%lld\n", result);
        result += newt(dist[1], 4); // 4 * d == 1
        result %= MOD;
        //printf("%lld\n", result);
    }
    result %= MOD;
    printf("%lld\n", result);
    return 0;
}