import java.lang.Exception
import java.lang.IllegalArgumentException
import java.util.*

val dx = listOf(1, 1, 1, 0, 0, -1, -1, -1)
val dy = listOf(1, 0, -1, 1, -1, 1, 0, -1)

var map: Array<IntArray> = emptyArray()
var seen: Array<BooleanArray> = emptyArray()
val SEA = 0
val LAND = 1
var height = -1;
var width = -1;


var sc = Scanner(System.`in`)
var count = 0
fun main(args: Array<String>) {

    try {
        while (true) {
            solve()
        }
    } catch (e: Exception) {
        return
    }
}

private fun solve() {

    width = sc.nextInt()
    height = sc.nextInt()

    if(width == 0 && height == 0){
        throw IllegalArgumentException()
    }

    map = Array(height) { IntArray(width) }
    seen = Array(height) { BooleanArray(width) }
    count = 0
    for (h in 0 until height) {
        for (w in 0 until width) {
            map[h][w] = sc.nextInt()
            if (map[h][w] == SEA) seen[h][w] = true
        }
    }


    for (w in 0 until width) {
        for (h in 0 until height) {
            if (seen[h][w]) {
                continue
            }
            if (map[h][w] == LAND) {
                count++
                dfs(h, w)
            }
        }
    }
    println(count)

}

private fun dfs(h: Int, w: Int) {
    if (seen[h][w]) {
        return
    }
    seen[h][w] = true
    for ((x, y) in dx.zip(dy)) {
        val ny = h + y
        val nx = w + x
        if (ny < 0 || nx < 0 || ny >= height || nx >= width) continue
        dfs(ny, nx)
    }
}

