#include<stdio.h>

void Island1(int, int, int, int);
void Island2(int, int, int, int);

int data[50][50];

int main(){
  int i, j, w, h, x, 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", &data[i][j]);
      }
    }

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

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

  return 0;
}

void Island1(int h, int w, int i, int j){
  data[i][j] = 2;
  Island2(h, w, i-1, j-1);
  Island2(h, w, i-1, j);
  Island2(h, w, i-1, j+1);
  Island2(h, w, i, j-1);
  Island2(h, w, i, j+1);
  Island2(h, w, i+1, j-1);
  Island2(h, w, i+1, j);
  Island2(h, w, i+1, j+1);
}

void Island2(int h, int w, int i, int j){
  if(i > -1 && i < h && j > -1 && j < w && data[i][j] == 1)
    Island1(h, w, i, j);
}