/* ***************************
   ??????????§????
   ???????????????
   ????????????????????¨????????±???????????¢?´¢
   *************************** */

#include<stdio.h>
#include<stdlib.h>

#define MAX 20

// ????????¨?????¨ ?§???????
typedef struct {
  int row;               // ?????????
  int col;               // ?????????
} Coord;

// ??????????????¨ ?§???????
typedef struct _stack {
  struct _stack *next;
  Coord point;
} *stack, stackelem;

int M[MAX+2][MAX+2];    // ?????¢?????°??????????????§???

// ????????????????????£?¨?
void input (int W, int H);
int solve (int W, int H);
void display_map (int W, int H);

// ??????????????¨
stack  new_stack (void);
int is_empty (stack);
int push (stack, Coord);
Coord top (stack);
Coord pop (stack);

// main ??¢??°
int main () {
  int W, H;

  while(1){
    scanf("%d%d", &W, &H);
    if(W==0 && H==0)break;
    /*if (W<1 || H<1 || W>MAX || H>MAX) {
      printf("?????¢??????????????? %d X %d ?????§??§???. \n", MAX, MAX);
      } else {*/
    input(W,H);
    printf("%d\n", solve(W,H));
    // }
  }

  return 0;
}

// ?????£?????¢?´¢ 
void input (int W, int H) {
  //Coord start;             // ??????????????°???
  int i, j;

  //  ?£?????????????????¨???? 
  for (i=0; i<=W+1; i++) { M[0][i] = 0; M[H+1][i] = 0; }
  for (i=1; i<=H; i++) { M[i][0] = 0; M[i][W+1] = 0; }
  // ??????????????\??? 
  for (i=1; i<=H; i++) {
    for (j=1; j<=W; j++) {
      scanf(" %d", &M[i][j]);
    }
  }

  // ??????????????°?????¨??? (??¬??\???????????????)
  //printf("??????????????°??? [%d, %d]\n\n", start.row, start.col);

  // ?????£?????°??????????????? 1, ????????§???????????? 0
}

int solve (int W, int H) {
  int i, j;
  int count = 0;
  Coord start;
  stack S = new_stack ();  // ?¨?????????????????????????????????????

  for(i = 0; i <= W; i++){
    for(j = 0; j <= H; j++){
      if(M[j][i] == 1){
	count++;
	M[j][i] = 0;
	start.row = j;
	start.col = i;
	push(S, start);
	// ??¢?´¢
	while (!is_empty(S)) {
	  Coord current = pop(S);                    // pop ???????????????
	  int d;                                     // ??????
	  int D1[]={1,1,0,-1,-1,-1,0,1}; int D2[]={0,1,1,1,0,-1,-1,-1};  // ????????¢?????¶?????¨
	  //printf("?????¨??°??? [%d, %d]\n", current.row, current.col);  // ??¬??\???????????????
	  //display_map(W, H); // ?????¨??????????????¨??? (??¬??\???????????????)
	  for (d=0; d<8; d++) {
	    Coord nc = current;                      // Next Cell
	    // ????????§???????????????
	    nc.row += D1[d]; nc.col += D2[d];

      
	    if(M[nc.row][nc.col] == 1){
	      push(S, nc);
	      M[nc.row][nc.col] = 0;
	    }

	  }
	}
      }
    }
  }
  return count;  
}

void display_map (int W, int H) {;
  int i, j;
  for (i=1; i<=H; i++) {
    for (j=1; j<=W; j++) {
      printf("%c", M[i][j]);
    }
    putchar('\n');
  }
  putchar('\n');
}

/* ??°??????????????????????¢?????????????????????? */
stack new_stack (void) {
  stack s;
  s = (stack) malloc (sizeof (stackelem));
  if (s == NULL) return NULL;
  s->next = NULL;
  return s;
}
    
/* ???????????????????????????????????? */
int is_empty (stack s) {
  return (s && s->next == NULL);
}

/* ??°??????????´???????????????\ */
int push (stack s, Coord v) {
  stack p;
  if (s == NULL) return 0;
  p = (stack) malloc (sizeof (stackelem));
  if (p == NULL) return 0;
  p->point = v;
  p->next = s->next;
  s->next =  p;
  return 1;
}

/* ?????????????´?????????? */
Coord top (stack s) {
  if (s && s->next) 
    return s->next->point;
  exit (1);
}

/* ????????? */
Coord pop (stack s) {
  stack p;
  Coord v = top (s);
  p = s->next;
  s->next = p->next;
  free (p);
  return v;
}