fun main(args:Array<String>?): Unit {
    while(true){
        val (w, h) = readLine()!!.trim().split(' ').map(String::toInt)
        if (w == 0 && h == 0) return
        val state = Array(h){ readLine()!!.trim().split(' ').map{it == "1"}.toBooleanArray()}
        val union = UnionFind(w * h)
        for (i in 0 until h) for (j in 0 until w) if (state[i][j]) {
            for (di in -1 .. 1) for (dj in -1 .. 1) if (i + di in 0 until h && j + dj in 0 until w && state[i + di][j + dj]){
                union.merge(i * w + j, i * w + j + di * w + dj)
            }
        }
        println((0 until h).sumBy{i -> (0 until w).count{j -> state[i][j] && union.findRoot(i * w + j) == i * w + j}})
    }
}
class UnionFind(val size: Int){
    private val mArray = IntArray(size){-1}
    fun merge(a: Int, b: Int): Unit {
        val ar = findRoot(a)
        val br = findRoot(b)
        if (ar != br){
            if (mArray[ar] < mArray[br]){
                mArray[ar] += mArray[br]
                mArray[br] = ar
            }else {
                mArray[br] += mArray[ar]
                mArray[ar] = br
            }
        }
    }
    fun findRoot(a: Int): Int {
        return if (mArray[a] >= 0){
            mArray[a] = findRoot(mArray[a])
            mArray[a]
        }else {
            a
        }
    }
    fun sizeOf(a: Int): Int {
        return -mArray[findRoot(a)]
    }
    fun isSame(a: Int, b: Int): Boolean {
        return findRoot(a) == findRoot(b)
    }
}

