#include <stdio.h>

int w, h;
int island[50][50], map[50][50], ans;

//flg:ツ親ツ・ツ子ツづ個フツδ可グツ　0ツづ?親
//ツ親ツづ債督?づーツ板ュツ個ゥツつキツづゥ
//ツ子ツづ債督?つェツづ?つアツづ慊づ?連ツ堕アツつオツづ?つ「ツづゥツつゥツづーツ探ツ催オツつキツづゥ
void dfs(int x, int y, int flg) {
		int i, j;
	//ツ氾債暗債外,ツ探ツ催オツ催渉づ敖湘ェツ渉環づ個個淞催オツづ康G
	if(x < 0 || w < x || y < 0 || h < y || map[x][y] == 1) {
		return;
	}
	//ツ鳴「ツ探ツ催オツ篠?
	else if(map[x][y] == 0) {
		//ツつサツつアツつェツ海ツづ按づァ
		if(island[y][x] == 0) {
			//ツ親ツづ按づァツづ?
			if(flg == 0) {
			 	map[x][y] = 1;  //ツつサツつアツづーツ探ツ催オツ催渉づ敖づ?
		
				//ツ偲シツ暗?ツマツスツづーツ探ツ催オツ催?開
				for(i = -1; i <= 1; i ++) {
					for(j = -1; j <= 1; j ++) {
						dfs(x + i, y + j, 0);
					}
				}
			}
			//ツ子ツづ按づァツづ篠、ツ可スツづ?つケツつクツ帰ツづゥ
			else if(flg == 1) {
				return;
			}
		}
		//ツつサツつアツつェツ督?づ按づァツづ?
		else if(island[y][x] == 1) {
			map[x][y] = 1;  //ツつサツつアツづーツ探ツ催オツ催渉づ敖づ?
					
			if(flg == 0) {
				ans ++; //ツ親ツつェツ個ゥツづつつッツつスツづ按づァツづ篠督?青?1
			}
	//		printf("island[%d][%d] = %d, flg = %d\n", x, y, island[y][x], flg);
			
			for(i = -1; i <= 1; i ++) {
				for(j = -1; j <= 1; j ++) {
					dfs(x + i, y + j, 1); //ツ偲シツ暗債づ可督?つェツづ按つ「ツつゥツ子ツづ可探ツ催オツつウツつケツづゥ
				}
			}
		}
	}
	return;
}

int main() {
	
	int i, j;
	while(scanf("%d %d", &w, &h)) {
		if(w == 0 && h == 0) { break; }
		
		//mapツ渉可甘コツ可サツ閉板閉ェ
		ans = 0;
		for(i = 0; i < 50; i ++) {
			for(j = 0; j < 50; j ++) {
				map[i][j] = -1;
				island[i][j] = 0;
			}
		}
	
		for(i = 0; i < h; i ++) {
			for(j = 0; j < w; j ++) {
				scanf("%d", &island[i][j]);
				map[i][j] = 0;
			}
		}
		
		for(i = 0; i < h; i ++) {
			for(j = 0; j < w; j ++) {
				dfs(i, j, 0);
			}
		}
		printf("%d\n", ans);
	}

	return 0;
}