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
// for k <= 2
#include <iostream>
#include <cstdio>
#include <string>
#include <sstream> 
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <ctime>
#include <cassert>
using namespace std;
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define vi vector<int>
#define vpii vector<pii>
#define SZ(x) ((int)(x.size()))
#define DBG cerr << "debug here" << endl;
#define info(vari) cerr << #vari<< " = "<< (vari) <<endl;

typedef long long ll;
const int INF = 1e9;

const int N = 3000+10;
const int M = 1e9+7;

const char EMPTY = '.';
const char USED = '#';
const char TYPE1 = '1';
const char TYPE2 = '2';
const char TYPE3 = '3';

string a[N];

ll npo2(ll n) {
    ll res = n*(n-1)/2;
    return res%M;
}

ll rev6 = 166666668;

ll npo3(ll n) {
    ll res = (n*(n-1)) % M;
    res *= (n-2);
    res %= M;
    res *= rev6;
    res %= M;
    return res;
}

const int R = 4;
int dx[R] = {-1, 0, 1, 0};
int dy[R] = {0, 1, 0, -1};

ll n;

ll type1 = 0; // directly adjacent to taken cells

int c[N][N][5]; // c[x][y][t] num of adjacent of type t to (x,y)

int main()
{
    ios_base::sync_with_stdio(0);
    int k;
    cin >> n >> k;
    assert(k <= 3);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    // computing type1 cells 
    for (int x = 0; x < n; ++x) {
        for (int y = 0; y < n; ++y) {
            if (a[x][y] != USED) continue;
            for (int i = 0; i < R; ++i) {
                int nx = x+dx[i];
                int ny = y+dy[i];
                if (nx >= 0 and nx < n and ny >= 0 and ny < n) {
                    if (a[nx][ny] == EMPTY) {
                        a[nx][ny] = TYPE1;
                        ++type1;
                    }
                }
            }
        }
    }

    // computing type2 cells - adjacent to at least 1 type1 cell and not adjancent to any USED cells
    for (int x = 0; x < n; ++x) {
        for (int y = 0; y < n; ++y) {
            if (a[x][y] != EMPTY) continue;
            int cntType1 = 0;
            int cntUsed = 0;
            for (int i = 0; i < R; ++i) {
                int nx = x+dx[i];
                int ny = y+dy[i];
                if (nx >= 0 and nx < n and ny >= 0 and ny < n) {
                    if (a[nx][ny] == USED) {
                        cntUsed++;
                    } else if (a[nx][ny] == TYPE1) {
                        cntType1++;
                    }
                }
            }
            if (cntType1 > 0 and cntUsed == 0) {
                a[x][y] = TYPE2;
            }
        }
    }

    // computing type3 cells - adjacent to at least 1 type2 cell and not adjancent to any USED cells nor type1 cells
    for (int x = 0; x < n; ++x) {
        for (int y = 0; y < n; ++y) {
            if (a[x][y] != EMPTY) continue;
            int cntType1 = 0;
            int cntType2 = 0;
            int cntUsed = 0;
            for (int i = 0; i < R; ++i) {
                int nx = x+dx[i];
                int ny = y+dy[i];
                if (nx >= 0 and nx < n and ny >= 0 and ny < n) {
                    if (a[nx][ny] == USED) {
                        cntUsed++;
                    } else if (a[nx][ny] == TYPE1) {
                        cntType1++;
                    } else if (a[nx][ny] == TYPE2) {
                        cntType2++;
                    }
                }
            }
            if (cntType2 > 0 and cntUsed == 0 and cntType1 == 0) {
                a[x][y] = TYPE3;
            }
        }
    }

    for (int x = 0; x < n; ++x) {
        for (int y = 0; y < n; ++y) {
            for (int i = 0; i < R; ++i) {
                int nx = x+dx[i];
                int ny = y+dy[i];
                if (nx >= 0 and nx < n and ny >= 0 and ny < n) {
                    if (a[nx][ny] == TYPE1) {
                        c[x][y][1]++;
                    } else if (a[nx][ny] == TYPE2) {
                        c[x][y][2]++;
                    } else if (a[nx][ny] == TYPE3) {
                        c[x][y][3]++;
                    }
                }
            }
        }
    }

    ll res = 0;
    if (k == 1) {
        res = type1;
    } else if (k == 2) {
        res = npo2(type1);
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (a[i][j] == TYPE2) {
                    res += c[i][j][1];
                }
            }
        }
        res %= M;
    } else if (k == 3) {
        // a)
        res = npo3(type1);
        //info(res);

        // b)
        for (int x = 0; x < n; ++x) {
            for (int y = 0; y < n; ++y) {
                if (a[x][y] != TYPE2) continue;
                res += npo2(c[x][y][1]);
                //cerr << "! " << x+1 << " " << y+1 << endl;
                //info(c[x][y][1]);
                res %= M;
            }
        }
        //info(res);

        // c)
        for (int x = 0; x < n; ++x) {
            for (int y = 0; y < n; ++y) {
                if (a[x][y] != TYPE1) continue;
                res += npo2(c[x][y][2]);
                res %= M;
            }
        }
        //info(res);

        // d)
        for (int x = 0; x < n; ++x) {
            for (int y = 0; y < n; ++y) {
                if (a[x][y] != TYPE2) continue;
                /*
                cerr << "! " << x+1 << " " << y+1 << endl;
                info(c[x][y][1]);
                info(c[x][y][2]);
                info(c[x][y][3]);
                */
                res += c[x][y][1] * (c[x][y][2] + c[x][y][3]);
                res %= M;
            }
        }
        //info(res);

        // e)
        for (int x = 0; x < n; ++x) {
            for (int y = 0; y < n; ++y) {
                if (a[x][y] != TYPE2) continue;
                res += c[x][y][1] * (type1 - c[x][y][1]);
                res %= M;
            }
        }
        //info(res);

    }
    cout << res << endl;

    /*
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cerr << a[i][j];
        }
        cerr << endl;
    }
    */

    return 0;
}