#include <stdio.h>

int h, w;
int c[51][51];
int memo[51][51];
int count = 1;

void calc_r(int y, int x)
{
  if ( y < 0 || y >= h || x < 0 || x >= w ) { return; }
  if ( memo[y][x] ) { return; }
  if ( !c[y][x] ) { return; }
  memo[y][x] = count;

  calc_r(y-1, x-1);
  calc_r(y-1, x);
  calc_r(y-1, x+1);
  calc_r(y, x-1);
  calc_r(y, x+1);
  calc_r(y+1, x-1);
  calc_r(y+1, x);
  calc_r(y+1, x+1);
}

void calc()
{
  int k1, k2;

  for ( k1 = 0; k1 < h; k1++ ) {
    for ( k2 = 0; k2 < w; k2++ ) {
      if ( c[k1][k2] && !memo[k1][k2] ) { calc_r(k1, k2); count++; }
    }
  }
}



int main(void)
{
  int k1, k2;

  while ( 1 ) {
    scanf("%d %d", &w, &h);
    if ( (w|h) == 0 ) { break; }
    for ( k1 = 0; k1 < h; k1++ ) {
      for ( k2 = 0; k2 < w; k2++ ) { scanf("%d", &c[k1][k2]); }
    }
    count = 1;


    for ( k1 = 0; k1 < 51; k1++ ) {
      for ( k2 = 0; k2 < 51; k2++ ) { memo[k1][k2] = 0; }
    }

    calc();
    printf("%d\n", count-1);

  }




  return 0;
}