#include <stdio.h>
#define SIZE 50

/* ---------------------------------------------------------------------------- */

typedef unsigned short point;
#define point(y, x) (((y)<<8) | (x))
#define x(pt) ((pt) & 255)
#define y(pt) (((pt)>>8) & 255)

/* ---------------------------------------------------------------------------- */

point queue[SIZE*SIZE], pp, gp;
#define put(pt) (queue[pp++] = pt)
#define get() (queue[gp++])
#define empty() (gp == pp)
#define init() (pp = gp = 0)

/* ---------------------------------------------------------------------------- */

#define search(y, x) (map[y][x] = 0, put(point(y, x)))

int w, h;
int map[SIZE][SIZE+1];

void fill(int y, int x)
{
    point p;

    init(); search(y, x);

    while(!empty())
    {
        p = get(); x = x(p), y = y(p);

        if(x-1 >= 0){
            if(y-1 >= 0 && map[y-1][x-1]) search(y-1, x-1);
            if(            map[y  ][x-1]) search(y  , x-1);
            if(y+1 <  h && map[y+1][x-1]) search(y+1, x-1);
        }

        if(y-1 >= 0 && map[y-1][x]) search(y-1, x);
        if(y+1 < h  && map[y+1][x]) search(y+1, x);

        if(x+1 < w)
        {
            if(y-1 >= 0 && map[y-1][x+1]) search(y-1, x+1);
            if(            map[y  ][x+1]) search(y  , x+1);
            if(y+1 <  h && map[y+1][x+1]) search(y+1, x+1);
        }
    }
}

int main(void)
{
    int x, y, count;

    while(scanf("%d %d", &w, &h), w+h)
    {
        /* init, input */

        count = 0;

        for(y=0; y<h; y++)
            for(x=0; x<w; x++)
                scanf("%d", &(map[y][x]));

        /* search */

        for(y=0; y<h; y++)
            for(x=0; x<w; x++)
                if(map[y][x]) fill(y, x), count++;

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

    return 0;
}