링크 : https://www.acmicpc.net/problem/4963
/*
문제 : 섬의 개수
링크 : https://www.acmicpc.net/problem/4963
*/
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
int map[50][50];
bool visited[50][50];
//우, 우상, 상, 좌상, 좌, 좌하, 하, 우하
int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int w, h;
void dfs(int y, int x){
for(int i = 0; i < 8; i ++){
int nx = x + dx[i];
int ny = y + dy[i];
if(nx < 0 || ny < 0 || nx >= w || ny >= h) continue;
if(!visited[ny][nx] && map[ny][nx]){
visited[ny][nx] = true;
dfs(ny, nx);
}
}
}
int main(){
while(1){
int cnt = 0;
cin >> w >> h;
if(w == 0 && h == 0) break;
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
cin >> map[i][j];
}
}
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
if(!visited[i][j] && map[i][j]){
visited[i][j] = true;
dfs(i, j);
cnt ++;
}
}
}
cout << cnt << "\n";
memset(visited,0,sizeof(visited));
}
return 0;
}