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

#define ROW_MAX 50
#define COL_MAX 50
#define BUF_SIZE 130

int get_int2(int *a1, int *a2) {
#ifdef BUF_SIZE
  char line[BUF_SIZE];
  if(!fgets(line, BUF_SIZE, stdin)) return -1;
  sscanf(line, "%d %d", a1, a2);
#else
#error
#endif
  return 0;
}

// <arr[0]> <arr[1]> .. <arr[size-1]>
int fget_array(char *arr, int size) {
#ifdef BUF_SIZE
    char line[BUF_SIZE];
    char *tmpbuf = line;
    int i;
    if(!fgets(line, BUF_SIZE, stdin)) return -1;
    for(i = 0; i < size; i++) {
        char *tmp = strtok(tmpbuf, " ");
        arr[i] = strtol(tmp, NULL, 10);
        tmpbuf = NULL;
    }
#else
#error
#endif
    return 0;
}

struct point2d {
    int row;
    int col;
};

struct point2d dist8[8] = {
    {-1,-1},
    {-1,0},
    {-1,1},
    {0, -1},
    {0, 1},
    {1,-1},
    {1,0},
    {1,1},
};

void dfs(char map[][COL_MAX+2], int row, int col) {
    int i;
    map[row][col] = 0;
    for(i = 0; i < 8; i++) {
        struct point2d np = {
            row + dist8[i].row,
            col + dist8[i].col
        };
        if(map[np.row][np.col]) {
            dfs(map, np.row, np.col);
        }
    }
    return;
}

#define SET_MAX 100

int main(void) {
    int row, col;
    int ans[SET_MAX] = {0};
    int ans_idx = 0;
    int i;
    while(1) {
        get_int2(&col, &row);
        char map[ROW_MAX+2][COL_MAX+2] = {0};
        if(row + col == 0) break;
        int i, j;
        // map is 1-indexed
        for(i = 1; i <= row; i++) {
            fget_array(&map[i][1], col);
        }

        int cnt = 0;
        for(i = 1; i <= row; i++) {
            for(j = 1; j <= col; j++) {
                if(map[i][j]) {
                    dfs(map, i, j);
                    cnt++;
                }
           }
        }
        ans[ans_idx++] = cnt;
    }
    for(i = 0; i < ans_idx; i++) {
        printf("%d\n", ans[i]);
    }
    return 0;
}
