#include <stdio.h>

void island(int s[][50],int w, int h,int i, int j){
  s[i][j] = -1;
  if(s[i][j-1] == 1 && 0 <= j-1) island(s,w,h,i,j-1);
  if(s[i][j+1] == 1 && j+1 <= w-1) island(s,w,h,i,j+1);
  if(s[i-1][j] == 1 && 0 <= i-1) island(s,w,h,i-1,j);
  if(s[i+1][j] == 1 && i+1 <= h-1) island(s,w,h,i+1,j);
  if(s[i+1][j+1] == 1 && i+1 <= h-1 && j+1 <= w-1) island(s,w,h,i+1,j+1);
  if(s[i+1][j-1] == 1 && i+1 <= h-1 && 0 <= j-1) island(s,w,h,i+1,j-1);
  if(s[i-1][j+1] == 1 && 0 <= i-1 && j+1 <= w-1) island(s,w,h,i-1,j+1);
  if(s[i-1][j-1] == 1 && 0 <= i-1 && 0 <= j-1) island(s,w,h,i-1,j-1);
}
    
  
 

int main(){

  int s[50][50],i,j,w,h;
  int count;
  
  while(1){
    scanf("%d %d", &w, &h);
    if(w == 0&&h ==0)
      break;

    for(i = 0; i < h; i++){
      for(j = 0; j < w ; j++){
	scanf("%d",&s[i][j]);
      }
    }

    count = 0;
    for(i = 0; i < h ; i++){
      for(j = 0; j < w ; j++){
	if(s[i][j] == 1){
	  island(s,w,h,i,j);
	  count++;
	}
      }
    }

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

  return 0;
}

