import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

int[] di = [1,0,-1,0,1,1,-1,-1], dj = [0,1,0,-1,1,-1,1,-1];
int ans;

void main() {
    while (1) {
        int w, h;
        scan(w, h);

        if (!w) return;

        auto c = iota(h).map!(i => readln.split.to!(int[])).array;

        int ans;

        foreach (i ; 0 .. h) {
            foreach (j ; 0 .. w) {
                if (c[i][j]) {
                    dfs(h, w, c, i, j);
                    ans++;
                }
            }
        }

        writeln(ans);
    }
}

void dfs(int h, int w, int[][] c, int i, int j) {
    c[i][j] = 0;

    foreach (k ; 0 .. 8) {
        int ni = i + di[k];
        int nj = j + dj[k];

        if (ni < 0 || nj < 0 || ni >= h || nj >= w) continue;

        if (c[ni][nj]) {
            dfs(h, w, c, ni, nj);
        }
    }
}



void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}


void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}