#include<stdio.h>
#define rep(i,n) for(int i=0;(i)<(n);(i)++)
typedef struct{
    int land;
    int used;
}mesh;
int w, h;
int count;
mesh c[50][50];
void dfs(int i,int j){
    c[i][j].used=1;
    for(int x=-1;x<=1;x++){
        for(int y=-1;y<=1;y++){
            if(x==0&&y==0) continue;
            int ny=i+y,nx=j+x;
            if(nx<0||nx>=w||ny<0||ny>=h) continue;
            else if(c[ny][nx].used==1) continue;
            else if(c[ny][nx].land==1) dfs(ny,nx);
        }
    }
}
int main(void){
    while(1){
        scanf("%d%d",&w,&h);
        if(w+h==0) break; count=0;
        rep(i,h) rep(j,w) scanf("%d",&c[i][j].land),c[i][j].used=0;
        rep(i,h)rep(j,w){
            if(c[i][j].used==0&&c[i][j].land==1)
                dfs(i,j),count++;
        }
        printf("%d\n",count);
    }
}

