-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaoj1160.cpp
More file actions
65 lines (60 loc) · 1.17 KB
/
aoj1160.cpp
File metadata and controls
65 lines (60 loc) · 1.17 KB
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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <cstring>
#include <cctype>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <complex>
using namespace std;
#define REP(i,n) for(int i = 0; i < (int)n; i++)
#define FOR(i,a,b) for(int i = a; i < (int)b; i++)
const int INF = 1<<28;
int m[50][50];
int w, h;
int cnt;
void dfs(int x, int y, int z) {
if(m[y][x]) {
if(!z)
cnt++;
m[y][x] = false;
if(y > 0 && m[y-1][x])
dfs(x, y-1, 1);
if(y > 0 && x > 0 && m[y-1][x-1])
dfs(x-1, y-1, 1);
if(x > 0 && m[y][x-1])
dfs(x-1, y, 1);
if(x > 0 && y < h-1 && m[y+1][x-1])
dfs(x-1, y+1, 1);
if(x < w-1 && m[y][x+1])
dfs(x+1, y, 1);
if(x < w-1 && 0 < y && m[y-1][x+1])
dfs(x+1, y-1, 1);
if(y < h-1 && m[y+1][x])
dfs(x, y+1, 1);
if(y < h-1 && x < w-1 && m[y+1][x+1])
dfs(x+1, y+1, 1);
}
return;
}
int main() {
while(cin >> w >> h, w|h) {
REP(i, h) REP(j, w)
cin >> m[i][j];
cnt = 0;
REP(i, h) REP(j, w)
dfs(j, i, 0);
cout << cnt << endl;
}
return 0;
}