#define MAX 52

#include <stdio.h>

int map[MAX][MAX] = {};

void island_del(int i,int j){
    if(map[i][j]==1){
        map[i][j] = 0;
        island_del(i,j-1);
        island_del(i,j+1);
        island_del(i-1,j);
        island_del(i-1,j-1);
        island_del(i-1,j+1);
        island_del(i+1,j);
        island_del(i+1,j-1);
        island_del(i+1,j+1);
    }
    
}

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