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
#include <bits/stdc++.h>
#define int long long
using namespace std;
const long long MOD = 1e9+7;

int32_t main()
{
    ios::sync_with_stdio(false);
    int n,k;
    cin >> n >> k;
    vector<string> plansza(n);
    vector<vector<bool> > one(n,vector<bool>(n)),two(n,vector<bool>(n)),three(n,vector<bool>(n));
    queue<pair<int,int> > que;
    for(int i=0;i<n;i++){
        cin >> plansza[i];
        for(int j=0;j<n;j++)
            if(plansza[i][j] == '#'){
                que.push(make_pair(i,j));
            }
    }
    while(!que.empty()){
        pair<int,int> cur = que.front();
        que.pop();
        char nx = '1';
        if(plansza[cur.first][cur.second] == '1')
            nx = '2';
        if(cur.first-1 >= 0){
            if(plansza[cur.first-1][cur.second] == '.'){
                plansza[cur.first-1][cur.second] = nx;
                if(nx != '2')
                    que.push(make_pair(cur.first-1,cur.second));
            }
        }
        if(cur.first+1 < n){
            if(plansza[cur.first+1][cur.second] == '.'){
                plansza[cur.first+1][cur.second] = nx;
                 if(nx != '2')
                    que.push(make_pair(cur.first+1,cur.second));
            }
        }
        if(cur.second-1 >= 0){
            if(plansza[cur.first][cur.second-1] == '.'){
                plansza[cur.first][cur.second-1] = nx;
                if(nx != '2')
                    que.push(make_pair(cur.first,cur.second-1));
            }
        }
        if(cur.second+1 < n){
            if(plansza[cur.first][cur.second+1] == '.'){
                plansza[cur.first][cur.second+1] = nx;
                if(nx != '2')
                    que.push(make_pair(cur.first,cur.second+1));
            }
        }
    }
    long long res = 0;
    long long ile1 = 0;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            int ct = 0;
            if(plansza[i][j] == '2' && k >= 2){
                if(i > 0 && plansza[i-1][j] == '1')
                    ct++;
                if(j > 0 && plansza[i][j-1] == '1')
                    ct++;
                if(i+1 < n && plansza[i+1][j] == '1')
                    ct++;
                if(j+1 < n && plansza[i][j+1] == '1')
                    ct++;
            }
            if(plansza[i][j] == '1')
                ile1++;
            res += ct;
        }
    }
    if(k == 2)
        res += (ile1*(ile1-1))/2;
    if(k == 1)
        res += ile1;
    res %= MOD;
    cout<<res<<endl;
    return 0;
}