#include<stdio.h>

#define wsize 50
#define hsize 50

int c[hsize+2][wsize+2]={};//テヲツオツキ(テ」ツ?セテ」ツ?淌」ツ?ッテヲツ篠「テァツエツ「テヲツクツ暗」ツ?ソ)0,テゥツ卍ク1

void search(int y, int x){
    int needlY[8]={-1,-1,0,1,1,1,0,-1};
    int needlX[8]={0,1,1,1,0,-1,-1,-1};
    int i;
    c[y][x] = 0;
    
    for(i=0;i<8;i++){
        if(c[y + needlY[i]][x + needlX[i]])
            search((y + needlY[i]), (x + needlX[i]));
    }
    return;
}

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

/*
    for(i=0;i<=h+1;i++){
        for(j=0;j<=w+1;j++){
            printf("%d ",c[i][j]);
        }
        printf("\n");
    }
*/

        for(i=1;i<=h;i++){
            for(j=1;j<=w;j++){
                if(c[i][j]){
                    //printf("%d %d\n",i,j);
                    count++;
                    search(i,j);
                }
            }
        }
        //printf("4-1  %d\n",c[4][1]);
        printf("%d\n",count);
    }
    
    return 0;
}