-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoj1160.cpp
50 lines (41 loc) · 925 Bytes
/
aoj1160.cpp
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
#include <iostream>
#include <cstdio>
using namespace std;
#define N 50
const int dx[]={-1,-1,-1,0,0,1,1,1};
const int dy[]={-1,0,1,-1,1,-1,0,1};
int w,h;
int map[N][N];
void rec(int y,int x){
map[y][x]=0;
for(int i=0;i<8;i++){
int nextx = x + dx[i];
int nexty = y + dy[i];
if(nextx>=0&&nexty>=0&&nextx<w&&nexty<h){
if(map[nexty][nextx]==1){
rec(nexty,nextx);
}
}
}
}
int main(){
while(cin >> w >> h){
if(w==0&&h==0) break;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
scanf("%d",&map[i][j]);
}
}
int count=0;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(map[i][j]==1){
rec(i,j);
count++;
}
}
}
cout<<count<<endl;
}
return 0;
}