#include <stdio.h>
#define Wsize 50
#define Hsize 50

int map[Hsize+2][Wsize+2]={};//テヲツオツキ0テ」ツ??ゥツ卍クテッツシツ?

void search(int H, int W){
    //printf("テァツ渉セテ・ツ慊ィテ・ツ慊ーW%d H%d\n",W, H);
    int needlH[8]={-1,-1,0,1,1,1,0,-1};
    int needlW[8]={0,1,1,1,0,-1,-1,-1};
    int i;
    map[H][W] = 0;
    for(i=0;i<8;i++){
        //printf("%d %dテ」ツつ津ィツヲツ凝」ツつ欺n",H + needlH[i],W + needlW[i]);
        if(map[H + needlH[i]][W + needlW[i]]){
            //printf("ifin\n");
            search((H + needlH[i]), (W + needlW[i]));
        }
    }
    return;
}

    
int main (void){
    int w,h;
    int i,j;
    int count;
    
    while(1){
        count = 0;
        
        //テ・ツ?・テ・ツ?コテ・ツ環?
        scanf("%d %d",&w ,&h);
        if(!w && !h)
            break;
        
        for(i=1; i<h+1; i++)
            for(j=1; j<w+1; j++)
                scanf("%d",&map[i][j]);
        /*
        for(i=0; i<=h+1; i++){
            for(j=0; j<=w+1; j++){
                printf("%d ",map[i][j]);
            }
            printf("\n");
        }
        */
        
        //テ・ツ?ヲテァツ青?
        for(i=1; i<h+1; i++){
            for(j=1; j<w+1; j++){
                if(map[i][j]){
                    count++;
                    search(i,j);
                }
            }
        }
        printf("%d\n",count);
        
    }
    
    return 0;
}