#include <stdio.h>

void islandin (int island[2500][2],int i,int j,int island_count){
    island[island_count][0] = i;
    island[island_count][1] = j;
}





int main(int argc, const char * argv[]) {
    
    
    int w,h,i=0,j=0,k=0,l=0,y;
    int table[52][52]={0};
    int island[2500][2],island_count=0,island_count1=0,x,island_count2 = 0,count=0;
    while (1) {
        
        
        scanf("%d %d",&w,&h);
        
        
        if (!w && !h) {
            break;
        }
        
        for (i=1; i<=h; i++) {
            for (j=1; j<=w; j++) {
                scanf("%d",&table[i][j]);
                
            }
        }
        
        for (i=1;i<=h;i++) {
            for (j=1; j<=w; j++) {
                if (table[i][j]==1) {
                    islandin(island,i,j,island_count);
                    island_count++;
                    table[i][j] = 0;
                    while (1){
                        island_count1=island_count;
                        for (x=0; x<island_count1; x++) {
                            for (k=-1; k <=1; k++) {
                                for (l=-1; l<=1; l++) {
                                    if (table[k+island[x][0]][l+island[x][1]] == 1) {
                                        islandin(island,(island[x][0]+k),(island[x][1]+l),island_count);
                                        island_count++;
                                        table[island[x][0]+k][island[x][1]+l]=0;
                                    }
                                }
                            }
                        }
                        if (island_count==island_count1) {
                            island_count2++;
                            island_count=0;
                            for (y=0; y<island_count; y++) {
                                island[y][0]=0;
                                island[y][1]=0;
                            }
                            break;
                        }
                    }
                }
            }
        }
        printf("%d\n",island_count2);
        island_count=0;
        island_count2=0;
    }
    return 0;
}