import math._
import scala.collection.mutable.Queue

object Main {

  def calc(g: Array[Array[Int]]): Int = {
    val rows = g.length
    val cols = g(0).length

    var ans = 0
    val used = Array.fill(rows, cols)(false)
    for (i <- 0 to rows-1) {
      for (j <- 0 to cols-1) {
        if (g(i)(j) == 1 && !used(i)(j)) {
          ans += 1

          var q = new Queue[(Int, Int)]
          q.enqueue((i, j))
          while (!q.isEmpty) {
            val (r, c) = q.dequeue

            if (!used(r)(c)) {
              used(r)(c) = true
              for (dr <- -1 to 1; dc <- -1 to 1) {
                val row = r + dr
                val col = c + dc
                if (0 <= row && row < rows && 0 <= col && col < cols) {
                  if (g(row)(col) == 1 && !used(row)(col)) {
                    q.enqueue((row, col))
                  }
                }
              }
            }
          }
        }
      }
    }
    ans
  }

  def main(args: Array[String]) {
    val sc = new java.util.Scanner(System.in)

    var b = true
    while (b) {
      val w, h = sc.nextInt
      if (w == 0 && h == 0) {
        b = false
      }
      else {
        var g = Array.fill(h, w)(0)
        for (i <- 0 to h-1) {
          for (j <- 0 to w-1) {
            g(i)(j) = sc.nextInt
          }
        }

        println(calc(g))
      }
    }
  }
}