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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Artur Kraska, II UWr

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>

#define forr(i, n)                  for(int i=0; i<(int)n; i++)
#define FOREACH(iter, coll)         for(typeof(coll.begin()) iter = coll.begin(); iter != coll.end(); ++iter)
#define FOREACHR(iter, coll)        for(typeof(coll.rbegin()) iter = coll.rbegin(); iter != coll.rend(); ++iter)
#define lbound(P,R,PRED)            ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;})
#define testy()                     int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests)
#define CLEAR(tab)                  memset(tab, 0, sizeof(tab))
#define CONTAIN(el, coll)           (coll.find(el) != coll.end())
#define FOR(i, a, b)                for(int i=(int)a; i<=(int)b; i++)
#define FORD(i, a, b)               for(int i=(int)a; i>=(int)b; i--)
#define MP                          make_pair
#define PB                          push_back
#define deb(X)                      X;

#define M 1000000007
#define INF 1000000007

#define cout if(0) cout
#define BRUT 0

using namespace std;

int n, k, a, tab[3050][3050];
int d[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int dd[8][2] = {{-2, 0}, {-1, 1}, {0, 2}, {1, 1}, {2, 0}, {1, -1}, {0, -2}, {-1, -1}};
long long res, ile_mapa, pi, pj, res2, ile[5], prev_res;
int mapa[3050][3050];
char slowo[3030][3030];
int numer, byl[3030][3030];
char r2[3005][3005], r1[3005][3005];

// DO BRUTA

int rrr, bwynik;

bool pom[303][303], bb[303][303];

int dfs(int i, int j)
{
    if(bb[i][j] || (tab[i][j] != -1 && pom[i][j] == 0))
        return 0;
    bb[i][j] = 1;
    int res = 1;
    forr(x, 4)
        res += dfs(i+d[x][0], j+d[x][1]);
    return res;
}

void brut(int ile, int i, int j)
{
    if(ile == 0)
    {
        CLEAR(bb);
        int rrrr = dfs(i, j-1);
        if(rrrr == rrr+k)
        {
            bwynik++;
            cout << "plansza " << bwynik << ": " << endl;
            FOR(i, 1, n)
            {
                FOR(j, 1, n)
                    cout << (pom[i][j] ? "#" : ".");
                cout << endl;
            }
        }

        return ;
    }
    if(i > n)
        return ;
    if(j > n)
    {
        brut(ile, i+1, 1);
        return ;
    }
    if(tab[i][j] >=0 && tab[i][j] <= 3 && pom[i][j] == 0)
    {
        pom[i][j] = 1;
        brut(ile-1, i, j+1);
        pom[i][j] = 0;
    }
    brut(ile, i, j+1);
}

long long odpal_bruta()
{
    bwynik = 0;
    FOR(i, 1, n)
        FOR(j, 1, n)
            if(tab[i][j] == -1)
            {
                CLEAR(bb);
                rrr = dfs(i, j);
            }
    cout << "rozmiar spojnej: " << rrr << endl;

    brut(k, 1, 1);
    return bwynik;
}

// PO BRUCIE

pair <long long, long long> euklid(long long a, long long b)
{
    if(b == 0)   return make_pair(1, 0);
    pair <long long, long long> p = euklid(b, a%b);
    return make_pair(p.second, p.first - p.second * (a/b));
}

long long odwr(long long a, long long mod)
{
    long long e = euklid(a, mod).first;
    return (e < 0 ? mod-((-e) % mod) : (e % mod));
}


long long n_po_k(int n, int k)
{
    if(k < 0 || k > n || n < 0)
        return 0;
    long long r=1;
    forr(i, k)
        r = r*(n-i)%M*odwr(i+1, M)%M;
    return r;
}

void generuj(int il, int i, int j)
{
    byl[i][j] = 1;
    forr(x, 4)
        if(tab[i+d[x][0]][j+d[x][1]] == 0)
        {
            mapa[i+d[x][0]][j+d[x][1]]++;
            cout << i+d[x][0] << ", " << j+d[x][1] << " do mapy  -> " << mapa[i+d[x][0]][j+d[x][1]] << endl;
            if(mapa[i+d[x][0]][j+d[x][1]] == 1)
                ile_mapa++;;
        }

    prev_res = res;
    if(((i < pi) || (i == pi && j < pj)))
    {
        //cout << "waz parzysty, nie liczymy" << endl;
        //FOR(a, 1, k-il)
        //    res2 = (res2 + n_po_k(ile_mapa, a)*n_po_k(ile[0]-ile_mapa, k-il-a))%M;
    }
    else
    {
        res2 = res;
        FOR(a, 1, k-il)
            res = (res + n_po_k(ile_mapa, a)*n_po_k(ile[0]-ile_mapa, k-il-a))%M;

    }
    cout << " dla " << il << "-elementowego weza w " << i << ", " << j << " mamy res += " << res-prev_res;
    cout << ", res2: " << res2 << ", ile_mapa: " << ile_mapa << endl;

    if(il+1 < k)
    {
        forr(x, 4)
        {
            int di = i+d[x][0];
            int dj = j+d[x][1];
            if(tab[di][dj] >= 1 && !byl[di][dj])
            {
                generuj(il+1, di, dj);
            }
        }
    }

    forr(x, 4)
        if(tab[i+d[x][0]][j+d[x][1]] == 0)
        {
            mapa[i+d[x][0]][j+d[x][1]]--;
            cout << i+d[x][0] << ", " << j+d[x][1] << " z mapy " << endl;
            if(mapa[i+d[x][0]][j+d[x][1]] == 0)
                ile_mapa--;
        }
    byl[i][j] = 0;
}

int main()
{
    scanf("%d %d", &n, &k);
    forr(i, n)
    {
        scanf("%s", slowo[i]);
    }
//    long long start = clock();
    // pocz testow

//    srand(time(0));
//    n = 4, k = 4;
    int res_b = 0;
//    while(1)
//    {

/*
    forr(i, n)
        forr(j, n)
            slowo[i][j] = (rand()%10 < 5 ? '#' : '.');
*/
//    forr(i, n)
//        printf("%s\n", slowo[i]);

    forr(i, n+2)
        forr(j, n+2)
        {
            if(i > 0 && j > 0 && slowo[i-1][j-1] == '#')
                tab[i][j] = -1;
            else
                tab[i][j] = -INF;
        }

    FOR(a, 0, k-1)
    {
        FOR(i, 1, n)
            FOR(j, 1, n)
            {
                if(tab[i][j] == -INF)
                    forr(x, 4)
                        if(tab[i+d[x][0]][j+d[x][1]] == a-1)
                           tab[i][j] = a;
                cout << " tab[" << i << "][" << j << "] = " << tab[i][j] << endl;
                if(tab[i][j] == a)
                    ile[a]++;
            }
    }
    long long b_res = 0;//odpal_bruta();

    if(BRUT)
        odpal_bruta();

    // podlicz 4 razy 0
    res = n_po_k(ile[0], k);
    cout << "ile[" << 0 << "] = " << ile[0] << endl;
    cout << "wszystkie na poziomie 0: " << res << endl;

    // dolicz takie, ktore maja tylko jedna spojna czesc poza 0
    if(k > 1)
        FOR(i, 1, n)
            FOR(j, 1, n)
            {
                if(tab[i][j] > 0)
                {
                    pi = i;
                    pj = j;
                    ile_mapa = 0;
                    generuj(1, i, j);
                }
            }
    cout << " po wygenerowaniu: " << res << endl;

                    vector<pair<int, int> > v1(100);
    // teraz takie krzyze 1110 i weze 1012
    if(k == 4)
        FOR(i, 1, n)
            FOR(j, 1, n)
            {
                if(tab[i][j] == 0)
                {
                    int dl = 0;
                    forr(x, 4)
                        if(tab[i+d[x][0]][j+d[x][1]] == 1)
                        {
                            v1[dl++] = MP(i+d[x][0], j+d[x][1]);
                        }
                    // krzyz 1110
                    if(dl == 3)
                    {
                        cout << "jest krzyz na " << i << " " << j << endl;
                        res++;
                    }
                    // waz 2101
                    forr(a, dl)
                        forr(b, dl)
                            if(a != b)
                            {
                                int i0 = v1[a].first;
                                int j0 = v1[a].second;
                                forr(x, 4)
                                {
                                    if(tab[i0+d[x][0]][j0+d[x][1]] == 2)
                                    {
                                        bool c=0;
                                        if(abs(i0+d[x][0] - v1[b].first) + abs(j0+d[x][1] - v1[b].second) > 1)
                                        {
                                            cout << " mamy weza 2101 na " << i << " " << j << endl;
                                            res++;
                                        }
                                    }
                                    if(tab[i0+d[x][0]][j0+d[x][1]] == 1)
                                    {
                                        bool c=0;
                                        if(abs(i0+d[x][0] - v1[b].first) + abs(j0+d[x][1] - v1[b].second) > 1)
                                        {
                                            cout << " mamy weza 1101 na " << i << " " << j << endl;
                                            res++;
                                        }
                                    }
                                }
                            }
                }
            }
    cout << " dodal weze - res: " << res << endl;
    res %= M;

    // dla k=3 podlicz 101
    if(k == 3)
    {

        FOR(i, 1, n)
            FOR(j, 1, n)
            {
                if(tab[i][j] == 0)
                {
                    int il = 0;
                    forr(x, 4)
                        if(tab[i+d[x][0]][j+d[x][1]] == 1)
                           il++;
                    res += n_po_k(il, 2);
                    res %= M;
                }
            }
            cout << " po podliczeniu 101: " << res << endl;

    }




    // i podlicz takie ktore maja 2 rozlaczne czesci z elementami 1
    if(k == 4)
    {
//    start = clock();
        cout << "przed: " << res << endl;
        long long ill = 0;
        FOR(i, 1, n)
            FOR(j, 1, n)
            {
                if(tab[i][j] == 1)
                {
                    int il = 0;
                    int obok = 0;
                    forr(x, 4)
                        if(tab[i+d[x][0]][j+d[x][1]] == 0)
                        {
                            il++;
                        }
                        else
                            if(tab[i+d[x][0]][j+d[x][1]] == 1)
                            {
                                // tu nie wiem...
                                obok += r2[i+d[x][0]][j+d[x][1]];
                            }
                    res = (res + il*(ill-obok))%M;
                    r2[i][j] = il;
                    cout << " dla " << i << ", " << j << " mamy " << il << " mozliwych wartosci 0, res += " << il*(ill-obok) << endl;
                    ill += il;
                }
            }
        cout << " po podliczniu wszystkich par: " << res << endl;
                    vector <pair<int, int>> v(100);

        // odlicz zduplikowane elementy 1-0-1, ale dodaj rozlaczne zestawy 1-0-1 i 0
        FOR(i, 1, n)
            FOR(j, 1, n)
            {
                if(tab[i][j] == 0)
                {
                    int dl = 0;
                    forr(x, 4)
                        if(tab[i+d[x][0]][j+d[x][1]] == 1)
                           v[dl++] = MP(i+d[x][0], j+d[x][1]);
                    FOR(a, 0, dl-1)
                        FOR(b, a+1, dl-1)
                        {
                            // odejmujemy to jedno niepelne wystapienie
                            res--;
                            int il = 0;
                            numer++;
                            forr(x, 4)
                                if(tab[v[a].first+d[x][0]][v[a].second+d[x][1]] == 0)
                                {
                                   il += byl[v[a].first+d[x][0]][v[a].second+d[x][1]] != numer;
                                   byl[v[a].first+d[x][0]][v[a].second+d[x][1]] = numer;
                                }
                            forr(x, 4)
                                if(tab[v[b].first+d[x][0]][v[b].second+d[x][1]] == 0)
                                {
                                   il += byl[v[b].first+d[x][0]][v[b].second+d[x][1]] != numer;
                                   byl[v[b].first+d[x][0]][v[b].second+d[x][1]] = numer;
                                }
                            // ale dodajemy zestawy 1-0-1, 0
                            res = (res + ile[0]-il + M)%M;
                            cout << " dla " << v[a].first << ", " << v[a].second << " i " <<
                                    v[b].first << ", " << v[b].second << " z wartosciami 1 mamy " << il << " sasiadow wartosci 0, res += " << ile[0]-il << "-1" << endl;
                        }
                }
            }
            cout << " po odliczeniu duplikatow: " << res << endl;

//    printf("%lld\n", clock() - start);

    }

    // odlicz kwadraty 10/01 i 01/10

    if(k == 4)
    {
        FOR(i, 1, n-1)
            FOR(j, 1, n-1)
            {
                if(tab[i][j] == 1 && tab[i+1][j] == 0 && tab[i][j+1] == 0 && tab[i+1][j+1] == 1)
                    res--;
                if(tab[i][j] == 0 && tab[i+1][j] == 1 && tab[i][j+1] == 1 && tab[i+1][j+1] == 0)
                    res--;
            }
    }

    cout << " res: " << res << ", res2: " << res2 << endl;


//    printf("moj: %lld, brut: %lld\n", res, b_res);

/*    if(res != b_res)
    {
        forr(i, n)
            printf("%s\n", slowo[i]);
        return 0;
    }
*//*    }
*/
    printf("%lld\n", res);

    return 0;
}