import scala.io.Source
import scala.collection.mutable.{ Set => MSet }

object Main {
  def main(args: Array[String]) = {
    val lines = Source.stdin.getLines.toStream
    loop(lines)
  }

  def loop(lines: Stream[String]): Unit = {
    val Array(numWidth, numHeight) = lines.head.split(" ").map(_.toInt)
    if (numWidth == 0) return

    val elems = lines.drop(1).take(numHeight).map(_.split(" ").map(_.toInt))
    val map = (for {
      (_elem, y) <- elems.zipWithIndex
      (elem, x) <- _elem.zipWithIndex
    } yield (x -> y) -> elem).toMap

    check(numWidth, numHeight, map)
    loop(lines.drop(numHeight + 1))
  }

  def check(numWidth: Int, numHeight: Int, map: Map[(Int, Int), Int]): Unit = {
    var cnt = 0
    val isArrived = MSet[(Int, Int)]()
    for {
      x <- 0 to (numWidth-1)
      y <- 0 to (numHeight-1)
    } {
      if (map(x -> y) == 1 && !isArrived.contains(x -> y)) {
        cnt = cnt + 1
        dfs(x -> y, map, isArrived)
      }
    }
    println(cnt)
  }

  def dfs(xy: (Int, Int), map: Map[(Int, Int), Int], isArrived: MSet[(Int, Int)]): Unit = {
    if (isArrived(xy)) return

    isArrived += xy
    val (x, y) = xy
    for {
      i <- -1 to 1
      j <- -1 to 1
    } {
      val xi = x + i
      val yj = y + j
      map.get(xi -> yj).foreach { n =>
        if (n == 1) {
          dfs(xi -> yj, map, isArrived)
        }
      }
    }
  }
}

