#include <stdio.h>
#include <string.h>
int map[51][51];
void search(int i,int j)
{
  if(map[i+1][j] == 1){
    map[i+1][j] = map[i][j];
    search(i+1,j);
  }
  if(map[i][j+1] == 1){
    map[i][j+1] = map[i][j];
    search(i,j+1);
  }
  if(map[i+1][j+1] == 1){
    map[i+1][j+1] = map[i][j];
    search(i+1,j+1);
  }
  if(j > 0){
    if(map[i][j-1] == 1){
      map[i][j-1] = map[i][j];
      search(i,j-1);
    }
    if(map[i+1][j-1] == 1){
      map[i+1][j-1] = map[i][j];
      search(i+1,j-1);
    }
  }
  if(i > 0){
    if(map[i-1][j] == 1){
      map[i-1][j] = map[i][j];
      search(i-1,j);
    }
    if(map[i-1][j+1] == 1){
      map[i-1][j+1] = map[i][j];
      search(i-1,j+1);
    }
  }
  if(i > 0 && j > 0){
    if(map[i-1][j-1] == 1){
      map[i-1][j-1] = map[i][j];
      search(i-1,j-1);
    }
  }
}
int main()
{
  for(;;){
    int w,h,i,j,count = 1;
    scanf("%d %d",&w,&h);
    if(w == 0 || h == 0) break;
    memset(map, 0, sizeof(map));
    for(i=0; i<h; i++){
      for(j=0; j<w; j++){
        scanf("%d",&map[i][j]);
      }
    }
    for(i=0; i<h; i++){
      for(j=0; j<w; j++){
        if(map[i][j] == 1){
          map[i][j] += count++;
          search(i,j);
        }
      }
    }
    printf("%d\n",count-1);
  }
  return 0;
}