#include<iostream>
#include<vector>
using namespace std;
#define rep(i,n) for(int i = 0; i < n;i++)
int w, h;
int map[50][50];
int dx[8] = { -1,0,1,-1,1,-1,0,1 }, dy[8] = { -1,-1,-1,0,0,1,1,1 };
void dfs(int y, int x) {
	map[y][x] = 0;
	for (int i = 0; i < 8;i++) {
		int nx = x + dx[i];
		int ny = y + dy[i];
		if (nx < 0 || ny >= h || ny < 0 || nx >= w) continue; 
		if(map[ny][nx] == 1) {
			dfs(ny, nx);
		}
	}
}
void dfss() {
	int count = 0;
	rep(i, h) {
		rep(j, w) {
			if (map[i][j] == 1) {
				count++; 
				dfs(i, j);
			}
		}
	}
	cout << count << endl;
}
int main() {
	for (;;) {
		cin >> w >> h;
		if (w == 0 && h == 0)
			break;
		rep(i, h) {
			rep(j, w) {
				cin >> map[i][j];
			}
		}
		dfss();
	}
}
