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

#define BUFFER_SIZE 1000000

struct queue {
    int buffer[BUFFER_SIZE];
    int head;
    int tail;
};

void init(struct queue *q)
{
    q->head = 0;
    q->tail = -1;
}

void enqueue(struct queue *q, int x)
{
    if (q->head == q->tail) {
        fprintf(stderr, "queue is full\n");
        exit(1);
    }

    q->buffer[q->head] = x;
    q->head = (q->head + 1) % BUFFER_SIZE;
}

int dequeue(struct queue *q)
{
    int t = (q->tail + 1) % BUFFER_SIZE;
    if (q->head == t) {
        fprintf(stderr, "queue is empty\n");
        exit(1);
    }

    q->tail = t;
    return q->buffer[q->tail];
}
int w, h;
int c[50][50];

const int dx[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
void fill(int sx, int sy)
{
    struct queue q;
    init(&q);
    enqueue(&q, sx);
    enqueue(&q, sy);
    c[sx][sy] = 0;

    while (q.head != (q.tail + 1) % BUFFER_SIZE) {
        int i;
        int x, y;
        x = dequeue(&q);
        y = dequeue(&q);

        for (i = 0; i < 8; i++) {
            int nx = x + dx[i], ny = y + dy[i];
            if (nx < 0 || w <= nx || ny < 0 || h <= ny)
                continue;
            if (c[ny][nx] == 0)
                continue;

            c[ny][nx] = 0;
            enqueue(&q, nx);
            enqueue(&q, ny);
        }
    }
}

int main(void)
{
    for (;;) {
        int i, j, ans = 0;

        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", &c[i][j]);

        for (i = 0; i < h; ++i) {
            for (j = 0; j < w; ++j) {
                if (c[i][j] == 1) {
                    fill(j, i);
                    ++ans;
                }
            }
        }
        printf("%d\n", ans);
    }

    return 0;
}