/*
	?§£????????????:Graph I - Breadth First Search
	(http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C)

	Status:Accepted

	??????:
		??¨??£???????°??????§???????????????????????????????????¢?´¢??????????????§???????????£????????????????????¢?´¢????????????????????????????????±???????????°???????????°????????¨?°????????????????

*/
var log = console.log;
if (typeof process != "undefined") {
    var input = "";
    process.stdin.resume();
    process.stdin.setEncoding('utf8');
    process.stdin.on('data', function(chunk) {
        input += chunk;
    });
    process.stdin.on('end', function() {
        var lines = input.split("\n");
        main(lines);
    });
}


function main(lines) {
    lines = lines.map(function(line) {
        return line.split(" ").map(function(e) {
            return Number(e);
        });
    });
    while (lines[0][0] != 0 && lines[0][1] != 0) {
        var fl = lines.shift();
        var W = fl[0];
        var H = fl[1];
        var set = lines.slice(0, H);
        lines = lines.slice(H);
        processSet(W, H, set);
    }
}

function processSet(W, H, set) {
    //!!log(set);
    var visited = [];
    for (var h = 0; h < H; h++) {
        visited[h] = [];
        for (var w = 0; w < W; w++) {
            visited[h][w] = false;
        }
    }
    var stack = [];
    var islands = 0;
    for (var h = 0; h < H; h++) {
        for (var w = 0; w < W; w++) {
            //!!log("look at [" + h + "," + w + "]");
            if (
                set[h][w] == 1 &&
                !visited[h][w]
            ) {
                //???????¨??????????????????¶?????°??§??????
                //dfs???????????????
                //!!log("[" + h + "," + w + "] is new island!");
                //!!log("start dfs");
                stack.push([h, w]);
                visited[h][w] = true;
                while (stack.length > 0) {
                    //!!log("now stack is ->");
                    //!!log(stack);
                    //??????????????£??????????????§
                    //???????????¨???????????????
                    var now = stack.pop();
                    stack.push(now);
                    var count = 0;
                    var next = [];
                    for (var dw = -1; dw <= 1; dw++) {
                        for (var dh = -1; dh <= 1; dh++) {
                            //count???????????§?????????????£???°???
                            if (count !== 0) {
                                break;
                            }
                            var th = now[0] + dh;
                            var tw = now[1] + dw;
                            if (
                                0 <= th && th < H &&
                                0 <= tw && tw < W
                            ) {
                                //???????????´??????????????????
                                //??°??????
                                if (!visited[th][tw] &&
                                    set[th][tw] == 1
                                ) {
                                    //????????????????????????
                                    count++;
                                    //??????????????????????????????????????????????????????
                                    if (count == 1) {
                                        next = [th, tw];
                                    }
                                }
                            }
                        }
                    }
                    //????????¢?´¢?????????????????£??\??????????????????
                    if (count > 0) {
                        stack.push(next);
                        visited[th][tw] = true;
                    } else {
                        //??£??\???????????????????????¢?´¢???????????????
                        stack.pop();
                    }
                }
                islands++;
            } else {
                //????????????????????£?????´??????
                //?£???°???
                visited[h][w] = true;
            }
        }
    }
    log(islands);
}

Measured memory_bytes: 18184