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

typedef long long LL;
typedef long double LD;
typedef pair < int, int > PII;
typedef pair < LL, LL > PLL;
typedef pair < LD, LD > PDD;

#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
template < typename _T > inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; }
template < typename _T, typename... args > void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); }

#ifdef LOCAL
#define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__)
#else
#define DBG(...) (__VA_ARGS__)
#define cerr if(0) cout
#endif

// ********************** CODE ********************** //

const int N = 3e3 + 7;
const int M = 1e9 + 7;

int n, k, d[N][N], p[] = {1, 0, 0, 1, -1, 0, 0, -1};
char s[N][N];
vector < PII > war[5];
queue < PII > Q;

vector < vector < PII > > V;
vector < PII > Tmp;

void Go(int w)
{
    if(w == 0) { V.push_back(Tmp); return; }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if(s[i][j] == '.')
            {
                bool ok = 0;
                for(int kk = 0; kk < 8; kk += 2)
                {
                    int x = i + p[kk], y = j + p[kk + 1];
                    if(s[x][y] == '#') { ok = 1; break; }
                }
                if(ok)
                {
                    Tmp.push_back({i, j});
                    s[i][j] = '#';
                    Go(w - 1);
                    s[i][j] = '.';
                    Tmp.pop_back();
                }
            }
        }
    }
}

void bfs()
{
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if(s[i][j] == '#')
                d[i][j] = 0, Q.push({i, j});
            else
                d[i][j] = -1;
        }
    }
    while(!Q.empty())
    {
        int x = Q.front().first, y = Q.front().second;
        Q.pop();
        for(int i = 0; i < 8; i += 2)
        {
            int ux = x + p[i], uy = y + p[i + 1];
            if(d[ux][uy] == -1)
            {
                d[ux][uy] = d[x][y] + 1;
                if(d[ux][uy] <= 4) {
                    war[d[ux][uy]].push_back({ux, uy});
                    Q.push({ux, uy});
                }
            }
        }
    }
}

int main()
{
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++)
        scanf("\n%s", s[i] + 1);

    bfs();

    if(k == 1)
    {
        cout << sz(war[1]) % M << "\n";
    }

    if(k == 2)
    {
        LL ans = LL(sz(war[1])) * (sz(war[1]) - 1) / 2 % M;
        for(auto v: war[1])
        {
            for(int i = 0; i < 8; i += 2)
            {
                int x = v.first + p[i], y = v.second + p[i + 1];
                if(d[x][y] == 2) ans++;
            }
        }
        cout << ans % M << "\n";
    }

    if(k == 3)
    {
        LL w1 = sz(war[1]), ans = w1 * (w1 - 1) % M * 500000004LL % M * (w1 - 2) % M * 333333336LL % M;
        LL ans2 = 0;
        for(auto v: war[1])
        {
            int cnt3 = 0;
            for(int i = 0; i < 8; i += 2)
            {
                int x = v.first + p[i], y = v.second + p[i + 1];
                if(d[x][y] == 2)
                {
                    cnt3++;
                    int cnt1 = 0, cnt2 = 0;
                    for(int j = 0; j < 8; j += 2)
                    {
                        int ux = x + p[j], uy = y + p[j + 1];
                        if(d[ux][uy] == 1) cnt1++;
                        else if(d[ux][uy] == 2 || d[ux][uy] == 3) cnt2++;
                    }
                    ans += cnt2; if(ans >= M) ans -= M;
                    ans += w1 - cnt1; if(ans >= M) ans -= M;
                    if(cnt1 > 1) ans2 += cnt1 - 1;
                }
            }
            ans += cnt3 * (cnt3 - 1) / 2; if(ans >= M) ans -= M;
        }
        assert(ans2 % 2 == 0);
        ans2 /= 2;
        cout << (ans + ans2) % M << "\n";
    }

    if(k == 4)
    {
        Go(4);
        for(auto &vec: V)
            sort(all(vec));
        sort(all(V));
        V.erase(unique(all(V)), V.end());
        cout << sz(V) % M << "\n";
    }
	return 0;
}