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

using u64 = uint64_t;
using i64 = int64_t;
using i32 = int32_t;
const i64 inf = (1ll << 62);
template <typename C> i64 isize(const C& c) { return static_cast<i64>(c.size()); }

// print tuples {{{
template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& t) {
    return os << '[' << t.first << ',' << t.second << ']';
}
// }}}
// print containers {{{
template <typename It>
void print(ostream& os, It begin, It end, u64 len, u64 limit = 30) {
    u64 count = 0;
    os << "{";
    while (begin != end && count < limit) {
        os << "(" << *begin << ")";
        count++;
        begin++;
    }
    if (begin != end)
        os << "... " << len << " total";
    os << "}";
}
#define MAKE_PRINTER_1(container) \
template <typename T> ostream& operator<<(ostream& os, const container<T>& t) { print(os, t.begin(), t.end(), t.size()); return os; }
#define MAKE_PRINTER_2(container) \
template <typename T1, typename T2> \
ostream& operator<<(ostream& os, const container<T1, T2>& t) { \
    print(os, t.begin(), t.end(), t.size()); \
    return os; \
}
MAKE_PRINTER_1(vector)
MAKE_PRINTER_2(map)
MAKE_PRINTER_1(set)
MAKE_PRINTER_2(unordered_map)
MAKE_PRINTER_1(unordered_set)
#undef MAKE_PRINTER_1
#undef MAKE_PRINTER_2
// }}}
// read/write {{{
template <typename T> T read() { T e; cin >> e; return e; }
void read() {}
template <typename T, typename ...Ts> void read(T& v, Ts& ...ts) { v = read<T>(); read(ts...); }
template <typename T> vector<T> readv(u64 n) { vector<T> v; for (u64 i = 0; i < n; i++) v.push_back(read<T>()); return v; }
template <typename T> struct identity { const T& operator()(const T& t) const { return t; } };
#define PRINTERS(FNAME, OUTP) \
    template <typename T> void FNAME(const T& t) { OUTP << t << ' '; } \
    void FNAME##ln() { OUTP << '\n'; } \
    template <typename T> void FNAME##ln(const T& t) { OUTP << t << '\n'; } \
    template <typename T, typename F = identity<typename T::value_type>> \
    void FNAME##v(const T& t, F f = F()) { for (const auto& e : t) FNAME(f(e)); FNAME##ln(); }
PRINTERS(print, cout)
#ifdef DEBUG_PRINTS
    PRINTERS(dprint, cerr)
#else
# define dprint(...)
# define dprintv(...)
# define dprintln(...)
#endif
/// }}}

const i64 p = 1000000007;
i64 num_of_subsets(i64 set_size, i64 subset_size) {
    assert(subset_size >= 0 && subset_size <= 4);
    if (subset_size > set_size)
        return 0;
    if (subset_size == 0 || subset_size == set_size)
        return 1;
    if (subset_size == 0)
        return 0;
    if (subset_size == 1)
        return set_size;
    if (subset_size == 2)
        return set_size * (set_size - 1ll) / 2ll % p;
    i64 res1, res2;
    if (subset_size == 3) {
        res1 = set_size * (set_size - 2ll), res2 = (set_size - 1ll);
        if (res1 % 3 == 0)
            res1 /= 3;
        if (res2 % 3 == 0)
            res2 /= 3;
        if (res1 % 2 == 0)
            res1 /= 2;
        if (res2 % 2 == 0)
            res2 /= 2;
    }
    if (subset_size == 4) {
        res1 = set_size * (set_size - 2ll), res2 = (set_size - 1ll) * (set_size - 3ll);
        dprint("res1, res2: "); dprint(res1); dprint(res2); dprintln();
        if (res1 % 3 == 0)
            res1 /= 3;
        else if (res2 % 3 == 0)
            res2 /= 3;
        if (res1 % 8 == 0)
            res1 /= 8;
        else if (res2 % 8 == 0)
            res2 /= 8;
    }
    return (res1 % p) * (res2 % p) % p;
}

i64 n;

struct Node {
    Node(i32 id0, i32 layer0, i32 x0 = 0) : id(id0), layer(layer0), x(x0) {}
    i32 id;
    i32 layer;
    i32 x;
    vector<i32> v;
    array<vector<i32>, 5> layers;
    vector<i32> far_nodes;
};
vector<Node> nodes;
array<vector<i32>, 5> layers;
array<vector<i32>, 5> paths; // numbers of paths

void calc_nodes(vector<vector<i32>>& m) { // {{{
    i32 last_node_id = 0;
    nodes.push_back(Node(0, 0));
    for (i64 k = 1; k < 5; k++) {
        for (i64 i = 0; i < n; i++) {
            for (i64 j = 0; j < n; j++) {
                if (m[i][j] >= 0)
                    continue;
                if ((j > 0 && m[i][j - 1] >= 0 && nodes[m[i][j - 1]].layer < k) ||
                        (j < n - 1 && m[i][j + 1] >= 0 && nodes[m[i][j + 1]].layer < k) ||
                        (i > 0 && m[i - 1][j] >= 0 && nodes[m[i - 1][j]].layer < k) ||
                        (i < n - 1 && m[i + 1][j] >= 0 && nodes[m[i + 1][j]].layer < k)) {
                    m[i][j] = ++last_node_id;
                    nodes.emplace_back(m[i][j], k, i);
                    layers[k].push_back(m[i][j]);
                    if (j > 0 && m[i][j - 1] > 0) {
                        nodes.back().v.push_back(m[i][j - 1]);
                        nodes[m[i][j - 1]].v.push_back(m[i][j]);
                    }
                    if (j < n - 1 && m[i][j + 1] > 0) {
                        nodes.back().v.push_back(m[i][j + 1]);
                        nodes[m[i][j + 1]].v.push_back(m[i][j]);
                    }
                    if (i > 0 && m[i - 1][j] > 0) {
                        nodes.back().v.push_back(m[i - 1][j]);
                        nodes[m[i - 1][j]].v.push_back(m[i][j]);
                    }
                    if (i < n - 1 && m[i + 1][j] > 0) {
                        nodes.back().v.push_back(m[i + 1][j]);
                        nodes[m[i + 1][j]].v.push_back(m[i][j]);
                    }
                }
            }
        }
    }
    assert(last_node_id + 1 == isize(nodes));
    for (auto& node : nodes)
        for (auto vid : node.v) {
            node.layers[nodes[vid].layer].push_back(vid);
            if (nodes[vid].layer > 1)
                node.far_nodes.push_back(vid);
        }
} // }}}

struct Val {
    Val(i64 v = 0) : res_(v) {}
    void operator+=(i64 v) { assert(v >= 0); res_ = (res_ + v) % p; }
    void operator*=(i64 v) { assert(v >= 0); res_ = (res_ * v) % p; }
    void operator-=(i64 v) { assert(v >= 0); res_ = (res_ + p - v) % p; }
    i64 get() const { return res_; }
    friend ostream& operator<<(ostream& os, const Val& v) { return os << v.res_; }
private:
    i64 res_;
};

Val count_211;
Val count_221;
Val count_21;

i64 lsize(i64 node, i64 l) { return isize(nodes[node].layers[l]); }
i64 lsize(i64 l) { return isize(layers[l]); }


i64 calc_221s() {
    Val res;
    for (const auto nd : layers[1]) {
        res += num_of_subsets(lsize(nd, 2), 2);
    }
    dprint("221s:"); dprintln(res);
    return res.get();
}

i64 process_211(i64 k) {
    // case when there's only one 2 that touches some 1s
    dprintln("process 211");
    if (k < 2)
        return 0;
    Val res = 0;
    for (const auto nd : layers[2]) {
        dprint("node from layer 2: "); dprintln(nd);
        const i64 nb_1s = lsize(nd, 1);
        const i64 free_1s = lsize(1) - nb_1s;
        const i64 nb_23s = lsize(nd, 2) + lsize(nd, 3);
        dprint("nb_1s"); dprintln(nb_1s);
        dprint("free_1s"); dprintln(free_1s);
        dprint("nb_23s"); dprintln(nb_23s);
        // 1,1,1 near 2
        if (nb_1s >= k - 1)
            res += num_of_subsets(nb_1s, k - 1);
        dprint("1,1,1 near 2"); dprintln(res);
        // two 1s - nd and some free 1 or neighbour 2/3
        if (k < 3)
            continue;
        if (nb_1s >= k - 2)
            res += num_of_subsets(nb_1s, k - 2) * (nb_23s + free_1s);
        dprint("1,1, 2 and 1 or 2/3"); dprintln(res);
        assert(nb_1s >= 1);
        if (k < 4)
            continue;
        {
            dprintln("building result for cases '1, 2'");
            Val val;
            // 1 - nd - two 2/3s
            val += num_of_subsets(nb_23s, k - 2);
            dprint("1, 2, 2/3;2/3"); dprintln(val);
            // 1 - nd and two free 1s
            val += num_of_subsets(free_1s, k - 2);
            dprint("1, 2, 1;1"); dprintln(val);
             // 1 - nd - 2/3 and some free 1, that doesn't have edge with 2/3
            for (const auto other_nd : nodes[nd].far_nodes)
                val += free_1s - lsize(other_nd, 1);
            dprint("1, 2, 2/3;1"); dprintln(val);
   
            val *= num_of_subsets(nb_1s, k - 3);
            dprint("building result finished:"); dprintln(val);
            res += val.get();
            dprint("1, 2, *:"); dprintln(res);
        }
        dprint("1, 2, *:"); dprintln(res);
        // paths 1 - 2 - 2/3 - 2/3/4, such that 1 /- 2/3/4
        for (const auto node1 : nodes[nd].layers[1])
            for (const auto node3 : nodes[nd].far_nodes)
                for (const auto node4 : nodes[node3].far_nodes)
                    if (node4 != nd) {
                        bool bad = false;
                        for (const auto node5 : nodes[node4].layers[1])
                            if (node5 == node1)
                                bad = true;
                        if (!bad)
                            res += 1;
                    }
        dprint("path results:"); dprintln(res);
    }
    if (k == 4) {
        res += calc_221s();
    }

    return res.get();
}

i64 calc_21s() {
    Val res;
    for (const auto nd : layers[1])
        res += lsize(nd, 2);
    dprint("21s:"); dprintln(res);
    return res.get();
}

i64 calc_211s() {
    Val res;
    for (const auto nd : layers[2]) {
        res += num_of_subsets(lsize(nd, 1), 2);
    }
    dprint("211s:"); dprintln(res);
    return res.get();
}


i64 calc_2112s() {
    Val res;
    for (const auto nd : layers[1])
        for (const auto nd2 : nodes[nd].layers[2])
            if (nodes[nd2].x < nodes[nd].x)
                for (const auto nd3 : nodes[nd2].layers[1])
                    if (nodes[nd3].x == nodes[nd2].x)
                        for (const auto nd4 : nodes[nd3].layers[2])
                            if (nodes[nd4].x > nodes[nd3].x)
                                for (const auto nd5 : nodes[nd4].layers[1])
                                    res += (nd5 == nd);
    dprint("2112s:"); dprintln(res);
    return res.get();
}

i64 process_pairs(i64 k) {
    // 12 + 12
    dprint("pairs:");
    if (k < 4)
        return 0;
    Val res;
    const i64 all_21s = calc_21s(), all_211s = calc_211s(), all_221s = calc_221s(), all_2112s = calc_2112s();
    res += num_of_subsets(all_21s, 2);
    dprint("first, all posible pairs, with intersections:"); dprintln(res);
    res -= all_211s;
    res -= all_221s;
    res -= all_2112s;
    dprint("after decreasing by 211, 221 and 2112s:"); dprintln(res);
    return res.get();
}

i64 process_1111(i64 k) {
    dprint("1111 args:"); dprint(lsize(1)); dprintln(k);
    i64 res = num_of_subsets(lsize(1), k);
    dprint("1111s:"); dprintln(res);
    return res;
}

i64 calc_res(const i64 k) {
    Val res;
    res += process_pairs(k);
    res += process_211(k);
    res += process_1111(k);
    if (k == 3)
        res += calc_221s();
    dprint("complete:"); dprintln(res);
    return res.get();
}

void print_tree(const vector<vector<i32>>& m) {
    dprintln("ids");
    for (const auto& v : m) {
        for (auto e : v) {
            dprint("\t");
            dprint(e);
        }
        dprintln();
    }
    dprintln("layers");
    for (const auto& v : m) {
        for (auto e : v) {
            dprint(nodes[e].layer);
        }
        dprintln();
    }
}


void go() {
    i64 k;
    read(n, k);
    vector<vector<i32>> m(n, vector<i32>(n));
    for (i64 i = 0; i < n; i++) {
        string s;
        cin >> s;
        for (i64 j = 0; j < n; j++) {
            m[i][j] = (s[j] == '#') ? 0 : (1 << 31);
        }
    }
    calc_nodes(m);
    print_tree(m);
    println(calc_res(k));
}

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