#include <stdio.h>

int dx[8] = {1,1,0,-1,-1,-1,0,1};
int dy[8] = {0,-1,-1,-1,0,1,1,1};
int map[52][52];

void foo(int y,int x,int n){
  int i;

  if(map[y][x] == -1){
    map[y][x] = n;
    for(i = 0;i < 8;i++){
      foo(y+dy[i],x+dx[i],n);
    }
  }
}

int main(){
  int w,h,i,j,ans;

  while(1){
    scanf("%d %d",&w,&h);
    if(w == 0 && h == 0){
      break;
    }
    ans = 0;

    for(i = 0;i < h;i++){
      map[i][0] = 0;
      map[i][h+1] = 0;
    }
    for(j = 0;j < w;j++){
      map[0][j] = 0;
      map[w+1][j] = 0;
    }

    for(i = 1;i <= h;i++){
      for(j = 1;j <= w;j++){
	scanf("%d",&map[i][j]);
	map[i][j] *= -1;
      }
    }

    for(i = 1;i <= h;i++){
      for(j = 1;j <= w;j++){
	if(map[i][j] == -1){
	  ans++;
	  foo(i,j,ans);
	}
      }
    }

    printf("%d\n",ans);
  }

  return 0;
}