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
#include <iostream>
#include <vector>

using namespace std;

vector < vector <int> > startery;
vector < vector <int> > combo;
vector < vector <int> > plansza;

void wyrzucIdentyko(int g)
{
    for(int i = 0; i < combo.size(); i++)
    {
        for(int j = i + 1; j < combo.size(); j++)
        {
            int same = 0;
            for(int k = 0; k < combo[i].size(); k = k + 2)
            {
                for(int l = 0; l < combo[j].size(); l = l + 2)
                {
                    if(combo[i][k] == combo[j][l] && combo[i][k + 1] == combo[j][l + 1])
                    {
                        same++;
                        //cout << combo[i][k] << "," << combo[j][l] << "   " << combo[i][k + 1] << "," << combo[j][l + 1] << endl;
                    }
                }
            }
            if(same == g)
            {
                for(int k = 0; k < combo[i].size(); k = k + 2)
                {
                    for(int l = 0; l < combo[j].size(); l = l + 2)
                    {
                        combo[j][l] = 0;
                        combo[j][l + 1] = 0;
                    }
                }
            }
        }
    }
}

void idziem(int x, int y, int dl)
{
    vector< vector <int> > dro0;
    dro0.push_back(vector <int>());
    vector< vector <int> > dro1;
    dro1.push_back(vector <int>());
    vector< vector <int> > dro2;
    dro2.push_back(vector <int>());
    vector< vector <int> > dro3;
    dro3.push_back(vector <int>());

    if(plansza[x + 1][y] >= 0)
    {
        dro0[dro0.size() - 1].push_back(x);
        dro0[dro0.size() - 1].push_back(y);
        dro0[dro0.size() - 1].push_back(x + 1);
        dro0[dro0.size() - 1].push_back(y);
        if(dl == 2)
        {
            combo.push_back(vector <int>());
            for(int i = 0; i < dro0[dro0.size() - 1].size(); i++)
            {
                combo[combo.size() - 1].push_back(dro0[dro0.size() - 1][i]);
            }
        }
        else if(dl > 2)
        {
            combo.push_back(vector <int>());
            for(int i = 0; i < dro0[dro0.size() - 1].size(); i++)
            {
                combo[combo.size() - 1].push_back(dro0[dro0.size() - 1][i]);
            }
            //idziem(dro0[dro0.size() - 1][dro0[dro0.size() - 1].size() - 2], dro0[dro0.size() - 1][dro0[dro0.size() - 1].size() - 1], x, y, dl);
        }
    }
    if(plansza[x - 1][y] >= 0)
    {
        dro1[dro1.size() - 1].push_back(x);
        dro1[dro1.size() - 1].push_back(y);
        dro1[dro1.size() - 1].push_back(x - 1);
        dro1[dro1.size() - 1].push_back(y);
        if(dl == 2)
        {
            combo.push_back(vector <int>());
            for(int i = 0; i < dro1[dro1.size() - 1].size(); i++)
            {
                combo[combo.size() - 1].push_back(dro1[dro1.size() - 1][i]);
            }
        }
    }
    if(plansza[x][y + 1] >= 0)
    {

        dro2[dro2.size() - 1].push_back(x);
        dro2[dro2.size() - 1].push_back(y);
        dro2[dro2.size() - 1].push_back(x);
        dro2[dro2.size() - 1].push_back(y + 1);
        if(dl == 2)
        {
            combo.push_back(vector <int>());
            for(int i = 0; i < dro2[dro2.size() - 1].size(); i++)
            {
                combo[combo.size() - 1].push_back(dro2[dro2.size() - 1][i]);
            }
        }
    }
    if(plansza[x][y - 1] >= 0)
    {
        dro3[dro3.size() - 1].push_back(x);
        dro3[dro3.size() - 1].push_back(y);
        dro3[dro3.size() - 1].push_back(x);
        dro3[dro3.size() - 1].push_back(y - 1);
        if(dl == 2)
        {
            combo.push_back(vector <int>());
            for(int i = 0; i < dro3[dro3.size() - 1].size(); i++)
            {
                combo[combo.size() - 1].push_back(dro3[dro3.size() - 1][i]);
            }
        }
    }
    if(dl == 2)
    {
        for(int i = 0; i < startery.size(); i++)
        {
            if(x != startery[i][0] || y != startery[i][1])
            {
                combo.push_back(vector <int>());
                combo[combo.size() - 1].push_back(x);
                combo[combo.size() - 1].push_back(y);
                combo[combo.size() - 1].push_back(startery[i][0]);
                combo[combo.size() - 1].push_back(startery[i][1]);
            }
        }
    }
}

int main()
{
    int wymiary;
    int gracze;
    cin >> wymiary;
    cin >> gracze;
    for(int i = 0; i < wymiary + 2; i++)
    {
        plansza.push_back(vector <int>());
        for(int j = 0; j < wymiary + 2; j++)
        {
            plansza[i].push_back(0);
        }
    }

    for(int i = 0; i < wymiary + 2; i++)
    {
        plansza[i][0] = -40;
        plansza[i][wymiary + 1] = -40;
    }
    for(int i = 0; i < wymiary + 2; i++)
    {
        plansza[0][i] = -40;
        plansza[wymiary + 1][i] = -40;
    }

    char c;
    for(int i = 1; i < wymiary + 1; i++)
    {
        for(int j = 1; j < wymiary + 1; j++)
        {
            cin >> c;
            if(c == '#')
            {
                plansza[i][j] = -40;
                plansza[i + 1][j]++;
                plansza[i - 1][j]++;
                plansza[i][j + 1]++;
                plansza[i][j - 1]++;
            }
        }
    }

    /*for(int i = 0; i < wymiary + 2; i++)
    {
        for(int j = 0; j < wymiary + 2; j++)
        {
            cout << plansza[i][j] << " ";
        }
        cout << endl;
    }*/

    for(int i = 1; i < wymiary + 2; i++)
    {
        for(int j = 1; j < wymiary + 2; j++)
        {
            if(plansza[i][j] > 0)
            {
                startery.push_back(vector <int>());
                startery[startery.size() - 1].push_back(i);
                startery[startery.size() - 1].push_back(j);
            }
        }
    }

    /*for(int i = 0; i < startery.size(); i++)
    {
        cout << startery[i][0] << "," << startery[i][1] << endl;
    }*/

    if(gracze == 1)
    {
        cout << startery.size() << endl;
    }
    else
    {
        long long int counter = 0;
        for(int i = 0; i < startery.size(); i++)
        {
            idziem(startery[i][0], startery[i][1], gracze);
        }
        wyrzucIdentyko(gracze);
        for(int i = 0; i < combo.size(); i++)
        {
            if(combo[i][0] != 0)
            {
                counter++;
            }
        }
        cout << counter << endl;
    }

    return 0;
}