#define R(i,n) for(i=0;i++<n;)
b[52][52],w,h,c;M(y,x){if(b[y][x]^1)return;b[y][x]++;M(y+1,x);M(y,x+1);M(y,x-1);M(y-1,x);M(y+1,x+1);M(y+1,x-1);M(y-1,x+1);M(y-1,x-1);}main(i,j){for(;scanf("%d%d",&w,&h),w;c=!printf("%d\n",c)){memset(b,-1,sizeof(b));R(i,h)R(j,w)scanf("%d",&b[i][j]);R(i,h)R(j,w)if(b[i][j]&1)c++,M(i,j);}}