fun main(args: Array<String>) = p25()

fun p25() {
    data class Yx(val y: Int, val x: Int)

    val around = (-1..1).map { i1 -> (-1..1).map { i2 -> Yx(i1, i2) } }
        .flatten().filterNot { it == Yx(0, 0) }
    var (w, h) = readLine()!!.split(' ').map { it.toInt() }


    val answer = StringBuilder()

    while (w != 0 && h != 0) {
        val grounds = Array(h) {
            readLine()!!.split(' ').map { it == "1" }.toBooleanArray()
        }
        val visited = mutableSetOf<Yx>()

        fun dfs(yx: Yx): Boolean {
            if (!visited.add(yx)) return false

            val (y, x) = yx

            around.map { Yx(y + it.y, x + it.x) }.filter {
                it.y in grounds.indices && it.x in grounds.first().indices && grounds[it.y][it.x]
            }.forEach { dfs(it) }

            return true
        }

        val counts = (0 until h).map { y ->
            (0 until w).filter { x -> grounds[y][x] }.count { x -> dfs(Yx(y, x)) }
        }.sum()

        answer.appendln(counts)

        val (nw, nh) = readLine()!!.split(' ').map { it.toInt() }
        w = nw
        h = nh
    }

    print(answer)
}

