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

enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;

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

        if (h == 0 && w == 0) return;

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

        auto ans = solve(h, w, g);

        writeln(ans);
    }
}


int solve(int h, int w, int[][] g) {
    auto v = new bool[][](h, w);

    int ans;

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

    void dfs(int i, int j) {
        v[i][j] = true;

        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 (v[ni][nj] || !g[ni][nj]) continue;
            dfs(ni, nj);
        }
    }

    foreach (i ; 0 .. h) {
        foreach (j ; 0 .. w) {
            if (g[i][j] && !v[i][j]) {
                dfs(i, j);
                ans++;
            }

            debug {
                writefln("%(%s\n%)", v);
            }
        }
    }

    return ans;
}







void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    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);
        }
    }
}

bool chmin(T, U...)(ref T x, U args) {
    bool isChanged;

    foreach (arg; args) {
        if (x > arg) {
            x = arg;
            isChanged = true;
        }
    }

    return isChanged;
}

bool chmax(T, U...)(ref T x, U args) {
    bool isChanged;

    foreach (arg; args) {
        if (x < arg) {
            x = arg;
            isChanged = true;
        }
    }

    return isChanged;
}

