#include<stdio.h>
#include<stdbool.h>

int map[50][50];
bool FindIsland(int, int, int, int);

void main()
{
	int h, w, i, j;
	int count;
	
	while(1)
	{
		count = 0;
		
		scanf("%d%d", &w, &h);
		
		if(w == 0 && h == 0) break;
		
		for(i = 0; i < h; i++)
		{
			for(j = 0; j < w; j++)
			{
				scanf("%d", &map[i][j]);
			}
		}
		
		for(i = 0; i < h; i++)
		{
			for(j = 0; j < w; j++)
			{
				if(FindIsland(i, j, h, w))
				{
					count++;
				}
			}
		}
		
		printf("%d\n", count);
	}
}

bool FindIsland(int cH, int cW, int maxH, int maxW)
{
	int i, j;
	int ccH, ccW;
	bool result = map[cH][cW] == 1;
	map[cH][cW] = -1;
	
	if(result)
	{
		for(i = 0; i < 3; i++)
		{
			for(j = 0; j < 3; j++)
			{
				ccH = cH - 1 + i;
				ccW = cW - 1 + j;
				if((i != 1 || j != 1) && ccH > -1 && ccW > -1 && ccH < maxH && ccW < maxW)
				{
					FindIsland(ccH, ccW, maxH, maxW);
				}
			}
		}
	}
	
	return result;
}