import scala.collection.mutable.Stack
import scala.util.control.Breaks.{breakable, break}

object Main extends App {
  val input = io.Source.stdin.getLines()
  breakable {
    while (input.hasNext) {
      val Array(w,h) = input.next().split(' ').map(_.toInt)
      if (w == 0 || h == 0) break()
      val field = Array.fill(h)(input.next().split(' ').map(_.toInt))
      var count = 0
      val visited = Array.ofDim[Boolean](w,h)
      for (i <- 0 until w; j <- 0 until h; if canVisit(i,j)) {
        count += 1
        dfs(i, j)
      }
      println(count)

      def canVisit(p: (Int, Int)): Boolean =
        p._1 >= 0 && p._1 < w && p._2 >= 0 && p._2 < h &&
          !visited(p._1)(p._2) && field(p._2)(p._1) == 1

      def dfs(start: (Int, Int)): Unit = {
        val stack = Stack[(Int, Int)]()
        stack.push(start)
        while (stack.nonEmpty) {
          val (x, y) = stack.pop()
          visited(x)(y) = true
          Seq(
            (x, y - 1), (x, y + 1),
            (x - 1, y), (x - 1, y - 1), (x - 1, y + 1),
            (x + 1, y), (x + 1, y - 1), (x + 1, y + 1)
          ).withFilter(canVisit).foreach(stack.push)
        }
      }
    }
  }
}
