"use strict";

let isSeen = []
const moveX = [-1,0,1,-1,1,-1,0,1]
const moveY = [-1,-1,-1,0,0,1,1,1]

function dfs(c,x,y,w,h) {
    isSeen[y][x] = 1
    //console.log(`x=${x}, y=${y}`)
    for(let i=0; i<moveX.length; i++) {// assume moveX.length is the same with moveY.length
        const newX = x+moveX[i]
        const newY = y+moveY[i]
        if(newX <0 || newX > w-1) continue;
        if(newY <0 || newY > h-1) continue;
        if(isSeen[newY][newX]===1) continue;
        if(c[newY][newX]===0) continue;
        
        //console.log(`  i=${i}, newX=${newX}, newY=${newY}`)
        //break
        dfs(c,newX,newY,w,h)
    }
    
}

function main(arg) {

    let input = arg.trim().split("\n");
    //console.log(input)
    while(true) {
        const w = parseInt(input[0].split(" ")[0]);
        const h = parseInt(input[0].split(" ")[1]);
        if(w===0 && h===0)break
        const inp = input.slice(1,h+1)
        const c = inp.map(i=>i.split(" ").map(n=>parseInt(n)));
        
        //console.log(inp)
        //console.log(`w=${w}, h=${h}`)
        for(let i=0; i<=h; i++) {
            input.shift()
        }
        //console.log(input)
        //console.log(input[0].split(" "))
        //return
        //continue
        isSeen = []
        c.forEach(cc=> {
            let arr = []
            cc.forEach(ccc=> {
                arr.push(0)
            })
            isSeen.push(arr)
        })
        
        let count = 0
        for(let x=0; x<w; x++) {
            for(let y=0; y<h; y++) {
                if(isSeen[y][x]===1) continue;
                if(c[y][x]===0) continue;
                dfs(c,x,y,w,h);
                ++count;
                //console.log(`***** count up to ${++count} *****`)
            }
        }
        
        console.log(count)
    }

}

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