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
| #include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int h, w;
int dy[8] = { 1, 1, 1, 0, 0,-1,-1,-1};
int dx[8] = { 1, 0,-1, 1,-1, 1, 0,-1};
bool can(int y, int x) {
if(0 <= y && y < h && 0 <= x && x < w) return true;
return false;
}
int cnt[1005][1005];
bool inQ[1005][1005], used[1005][1005];
int main() {
cin >> h >> w;
vector<string> s(h);
rep(i, h) cin >> s[i];
memset(cnt, 0, sizeof(cnt));
memset(used, 0, sizeof(used));
memset(inQ, 0, sizeof(inQ));
queue<P> que;
rep(i, h) {
rep(j, w) {
if(s[i][j] != '.') {
rep(k, 8) {
int y = i + dy[k];
int x = j + dx[k];
if(can(y, x) && s[y][x] == '.') {
cnt[i][j]++;
}
}
if(s[i][j] - '0' <= cnt[i][j]) {
que.push(mp(i, j));
used[i][j] = true;
}
}
}
}
int ans = 0;
while(que.size()) {
queue<P> tmp;
while(que.size()) {
P p = que.front();
que.pop();
rep(k, 8) {
int y = p.first + dy[k];
int x = p.second + dx[k];
if(can(y, x)) {
cnt[y][x]++;
if(s[y][x] != '.' && (s[y][x] - '0') <= cnt[y][x] && !used[y][x]) {
used[y][x] = true;
tmp.push(mp(y, x));
}
}
}
}
while(tmp.size()) {
P p = tmp.front();
tmp.pop();
que.push(p);
}
ans++;
}
cout << ans << endl;
return 0;
}
|