function Main(input) {
    input = input.trim().split("\n").map(function(x) { return x.split(" ")});
    var l = 0;
    while (true){
        var w = parseInt(input[l][0], 10);
        var h = parseInt(input[l][1], 10);
        if (w === 0 && h === 0) break;
        var line;
        var arr_c = [];
        for (var i = 0; i < h; i++){
            line = input[i + l + 1].map(e => parseInt(e, 10));
            arr_c.push(line);
        }
        arr_v = [];
        var countGround = 0;
        for (var i = 0; i < h; i++){
            for (var j = 0; j < w; j++){
                if (arr_c[i][j] === 1) countGround ++;
                arr_v.push([]);
                if (i - 1 >= 0 && j - 1 >= 0 && arr_c[i - 1][j - 1] === 1) arr_v[arr_v.length - 1].push(calcIndex(i - 1, j - 1, w));
                if (i - 1 >= 0 && arr_c[i - 1][j] === 1) arr_v[arr_v.length - 1].push(calcIndex(i - 1, j, w));
                if (i - 1 >= 0 && j + 1 < w && arr_c[i - 1][j + 1] === 1) arr_v[arr_v.length - 1].push(calcIndex(i - 1, j + 1, w));
                if (j - 1 >= 0 && arr_c[i][j - 1] === 1) arr_v[arr_v.length - 1].push(calcIndex(i, j - 1, w));
                if (j + 1 < w && arr_c[i][j + 1] === 1) arr_v[arr_v.length - 1].push(calcIndex(i, j + 1, w));
                if (i + 1 < h && j - 1 >= 0 && arr_c[i + 1][j - 1] === 1) arr_v[arr_v.length - 1].push(calcIndex(i + 1, j - 1, w));
                if (i + 1 < h && arr_c[i + 1][j] === 1) arr_v[arr_v.length - 1].push(calcIndex(i + 1, j, w));
                if (i + 1 < h && j + 1 < w && arr_c[i + 1][j + 1] === 1) arr_v[arr_v.length - 1].push(calcIndex(i + 1, j + 1, w));
            }
        }
        var tmp = 0;
        arr_time = Array.from({length: h * w}, () => [0, 0, 0]);
        time = 1;
        var countIsland = 0;
        history = [];
        whole_loop:
        while (time < countGround * 2){
            for (var i = tmp; i < h * w; i++){
                if (arr_c[calcIndex2(i, w)[0]][calcIndex2(i, w)[1]] === 1 && arr_time[i][1] === 0) {
                    arr_time[i] = [i, time, 0];
                    history = [i];
                    time ++;
                    finished = DFS(i);
                    tmp = i;
                    countIsland ++;
                    continue whole_loop;
                }
            }
        }
        // for (var x of arr_time){
        //     console.log(x.join(" "))
        // }
        console.log(countIsland);
        l += h + 1;
    }
}
function calcIndex(i, j, w){
    return i * w + j;
}

function calcIndex2(n, w){ // n >= 0
    return [(n / w | 0), n % w]
}

function DFS(tmpNode){
    for (let x of arr_v[tmpNode]){
        if (arr_time[x][1] === 0) {
            arr_time[x] = [x, time, 0];
            time ++;
            history.push(x);
            DFS(x);
        } 
    }
    arr_time[tmpNode][2] = time;
    time ++;
    history.pop();
    return true;
}

Main(require("fs").readFileSync("/dev/stdin", "utf8")); 


