#include <stdio.h>

const int di[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dj[8] = {-1, 0, 1, -1, 1, -1, 0, 1};

int w, h;
int c[50][50];

void delete(int i, int j) {
	int k;

	c[i][j] = 0;

	for (k = 0; k < 8; k++) {
		int ni = i + di[k], nj = j + dj[k];
		if (ni >= 0 && ni < h && nj >= 0 && nj < w && c[ni][nj])
			delete(ni, nj);
	}
}

int main(void) {
	int i, j;

	while (scanf("%d %d", &w, &h), w) {
		int cnt;

		for (i = 0; i < h; i++)
			for (j = 0; j < w; j++)
				scanf("%d", c[i] + j);

		cnt = 0;
		for (i = 0; i < h; i++)
			for (j = 0; j < w; j++)
				if (c[i][j]) {
					delete(i, j);
					cnt++;
				}

		printf("%d\n", cnt);
	}

	return 0;
}