#include <stdio.h>

void d_islands(int[64][64], int, int);//inc counter, call islands()

int main(void){

  char scan[128], give[4];
  int w, h, loop1, loop2, loop3, map[64][64];


  while(1){

    for(loop1 = 0; loop1 < 128; loop1++){
      scan[loop1] = '\0';
    }
    for(loop1 = 0; loop1 < 64; loop1++){
      for(loop2 = 0; loop2 < 64; loop2++){
	map[loop1][loop2] = 0;
      }
    }
    
    fgets(scan, 8, stdin);
    for(loop1 = 0; scan[loop1] != ' '; loop1++){
      give[loop1] = scan[loop1];
    }
    w = atoi(give);
    loop1++;    
    for(loop2 = 0; loop2 < 4; loop2++){
      give[loop2] = '\0';
    }
    for(loop2 = 0; scan[loop1] != '\n'; loop1++, loop2++){
      give[loop2] = scan[loop1];
    }
    h = atoi(give);   // get w h
    
    if((w == 0) && (h == 0)) break;

    for(loop1 = 0; loop1 < 4; loop1++){
      give[loop2] = '\0';
    }
    for(loop1 = 0; loop1 < 128; loop1++){
      scan[loop1] = '\0';
    }
    
    
    for(loop1 = 1; loop1 <= h; loop1++){
      fgets(scan, 128, stdin);
      for(loop2 = 0, loop3 = 1; scan[loop2] != '\n'; loop2++){
	if(scan[loop2] == '0'){
	  loop3++;
	}
	if(scan[loop2] == '1'){
	  map[loop1][loop3] = 1;
	  loop3++;
	}
      }
    }  // load the map

    /*    for(loop1 = 0; loop1 < 10; loop1++){
      for(loop2 = 0; loop2 < 10; loop2++){
	printf("%d ", map[loop1][loop2]);
      }
      printf("\n");
      }*/

    loop3 = 0;
    for(loop1 = 1; loop1 <= h; loop1++){
      for(loop2 = 1; loop2 <= w; loop2++){
	if(map[loop1][loop2] != 0){
	  d_islands(map, loop1, loop2);
	  loop3++;
	}
      }
    }
    printf("%d\n", loop3);

    
  }
  return 0;
    
}


void d_islands(int map[64][64], int h, int w){
  int loop1, loop2;
  map[h][w] = 0;
  /*    for(loop1 = 0; loop1 < 10; loop1++){
      for(loop2 = 0; loop2 < 10; loop2++){
	printf("%d ", map[loop1][loop2]);
      }
      printf("\n");
      }*/
  //  printf("checking %d %d\n", h, w);
  for(loop1 = h - 1; loop1 < h + 2; loop1++){
    for(loop2 = w - 1; loop2 < w + 2; loop2++){ 
      if(map[loop1][loop2] == 1) d_islands(map, loop1, loop2);//printf("1 ");
      //      else printf("0 ");

    }    
  }
}