import java.util.ArrayDeque

fun main(args: Array<String>) {
    while (true) {
        val (W, H) = readLine()!!.split(" ").map(String::toInt)

        if (W == 0 && H == 0) {
            return
        }
        val graph = Array(H) { readLine()!!.split(" ") }

        val seen = Array(H) { BooleanArray(W) }

        var count = 0
        for (i in 0 until H) {
            for (j in 0 until W) {
                if (graph[i][j] == "0" || seen[i][j]) {
                    continue
                }
                val stack = ArrayDeque<Pair<Int, Int>>()
                stack.push(i to j)

                while (stack.isNotEmpty()) {
                    val (h, w) = stack.pop()
                    seen[h][w] = true
                    for (th in -1..1) {
                        for (tw in -1..1) {
                            val ch = h + th
                            val cw = w + tw
                            if (ch in 0 until H && cw in 0 until W && !seen[ch][cw] && graph[ch][cw] == "1") {
                                stack.push(ch to cw)
                            }
                        }
                    }
                }
                count++
            }
        }
        println(count)
    }
}
