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

#define FOR(i,a,n) for (decltype(n) i = (a), i##__ = (n); i <= i##__; ++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define FORD(i,a,n) for (decltype(a) i = (a), i##__ = (n); i >= i##__; --i)
#define REPD(i,n) FORD(i,(n)-1,0)
#define ALL(x) x.begin(), x.end()
#define EB emplace_back
#define ST first
#define ND second
#define SZ(x) ((int)x.size())
#define RS resize
#define V vector
constexpr char nl = '\n';

using LL = long long;
using PII = pair<int, int>;
using VI = V<int>;
using VVI = V<VI>;
using VPII = V<PII>;
using VVPII = V<VPII>;
using VB = V<bool>;
using VVB = V<VB>;

template<class T, class B> void mini(T &&a, B &&b) { if (b < a) a = b; }
template<class T, class B> void maxi(T &&a, B &&b) { if (b > a) a = b; }
int pow2(int x) { return x < 2 ? 0 : sizeof(x) * 8 - __builtin_clz(x - 1); }
 
template<class T> struct Off { T a, b; };
template<class T> auto O(T &x) -> Off<decltype(typename enable_if<!is_same<string, T>::value, T>::type(), x.begin())> { return {x.begin(), x.end()}; }
template<class T> auto O(T &x) -> decltype(cerr << x, x) { return x; }
template<class T> auto O(T &x) -> decltype(x.first, x) { return x; }
struct Debug {
    ~Debug() { cerr << nl; }
    template<class T> auto operator<<(T x) -> decltype(cerr << x, *this) { cerr << O(x); return *this; }
    template<class T> auto operator<<(T x) -> decltype(x.begin(), typename enable_if<!is_same<string, T>::value, T>::type(), *this) {
        cerr << "{\n";
        for (auto a = x.begin(); a != x.end(); ++a)
            *this << "  " << distance(x.begin(), a) << ": " << O(*a) << '\n';
        return *this << "}";
    }
    template<class T, class B> Debug& operator<<(pair<T, B> p) { 
        return *this << "(" << O(p.first) << ", " << O(p.second) << ")"; 
    }
    template<class T> Debug& operator<<(Off<T> d) {
        cerr << "{";
        for (auto a = d.a, b = d.b; a != b; ++a)
            *this << O(*a) << (next(a) == b ? "" : ", ");
        return *this << "}";
    }
};
struct DebugOff { template<class T> DebugOff& operator<<(T) { return *this; } };
 
#ifdef DEBUG
# define D Debug()
#else
# define D DebugOff()
#endif
#define I(x...) #x ": " << x << "   "
#define TD(X, ...) ostream& operator<<(ostream &os, X &x) { __VA_ARGS__; return os; }

//end of templates

int n;
VVI in;
array<int, 4> x_diff = {{1, -1, 0, 0}}, y_diff = {{0, 0, 1, -1}};

inline int nr(int x, int y) {
    return x + y * n;
}

inline bool valid(int x, int y) {
    return 0 <= x && x < n && 0 <= y && y < n;
}

VVI BFS() {
    VVI dist(n, VI(n, 5));
    deque<PII> q;

    REP(x, n)
        REP(y, n)
            if(in[x][y])
                dist[x][y] = 0, q.EB(x, y);

    while(SZ(q)) {
        int x, y;
        tie(x, y) = q.front();
        q.pop_front();

        REP(side, 4) {
            int i = x_diff[side], j = y_diff[side];
            if(valid(x + i, y + j) && dist[x][y] < dist[x + i][y + j]) {
                dist[x + i][y + j] = dist[x][y] + 1;
                q.EB(x + i, y + j);
            }
        }
    }
    return dist;
}


struct ModInt {
    static constexpr int M = 1000000007;
    int a;
    ModInt(LL b) : a(b % M) {}
    ModInt(int b) : a(b) { if(a >= M) a %= M; }
    operator int() { return a; }
    ModInt  operator+ (ModInt b) { return a + b.a - (a + b.a >= M ? M : 0); }
    ModInt& operator+=(ModInt b) {    a = a + b.a - (a + b.a >= M ? M : 0); return *this; }
    ModInt  operator- (ModInt b) { return a - b.a + (a - b.a <  0 ? M : 0); }
    ModInt  operator* (ModInt b) { return (a * LL(b.a)) % M; }
};

ModInt newton(ModInt m, int k) {
    LL x = m;
    if(k == 2)
        return x * (x-1) / 2;
    else if(k == 3)
        return x * (x-1) * (x-2) / 6;
    else if(k == 4)
        return x * (x-1) * (x-2) * (x-3) / 24;
    else
        return 0;
}

ModInt s(VI &v) {
    return SZ(v);
}

ModInt answ = 0;
V<VVI> XD;
void broot(int k) {
    if(k == 0) {
        bool OK = 1;
        REP(i, SZ(XD))
            if(XD[i] == in) {
                OK = 0; 
                break;
            }
        if(OK) {
            answ += 1;
            XD.EB(in);
        }
    } else
        REP(i, n) 
            REP(j, n)
                if(in[i][j] == 0 && 
                        ((i != 0 && in[i - 1][j]) ||
                        (i != n - 1 && in[i + 1][j]) ||
                        (j != 0 && in[i][j-  1]) ||
                        (j != n - 1 && in[i][j + 1]))
                    ) {
                        in[i][j] = 1;
                        broot(k - 1);
                        in[i][j] = 0;
                    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int k;
    cin >> n >> k;
    in.RS(n, VI(n));
    REP(y, n)
        REP(x, n) {
            char c;
            cin >> c;
            in[x][y] = c == '#';
        }
    D << I(n) << I(k) << I(in);

    VVI dist = BFS();
    D << I(dist);

    array<VI, 5> all;
    V< array<VI, 5> > near(n*n);
    REP(x, n)
        REP(y, n) {
            int v = nr(x, y);
            all[dist[x][y]].EB(v);
            REP(side, 4) {
                int i = x_diff[side], j = y_diff[side];
                if(valid(x + i, y + j))
                    near[v][dist[x + i][y + j]].EB(nr(x + i, y + j));
            }
        }
    // D << I(all) << I(near); z pewnych oczywistych powodów nie może działać (operator int() oraz iteratory...)
    
    if(k == 1) {
        
        // 1
        answ += s(all[1]);
    
    } else if(k == 2) {

        // 1 1
        answ += newton(s(all[1]), 2);

        // 1 2
        for(int v : all[2])
            answ += s(near[v][1]);
    
    } else if(k == 3) {

        // 1 1 1
        answ += newton(s(all[1]), 3);

        // 1 1 2
        for(int v : all[2])
            answ += s(near[v][1]) * (s(all[1]) - s(near[v][1])) + newton(s(near[v][1]), 2);
        
        // 1 2 2
        for(int v : all[1]) {
            answ += newton(s(near[v][2]), 2);
            for(int u : near[v][2])
                answ += s(near[u][2]);
        }

        // 1 2 3
        for(int v : all[3])
            for(int u : near[v][2])
                answ += s(near[u][1]);
    
    } else {

        broot(4);

    }

    cout << answ;
}