import scala.annotation.tailrec

object Main {
  val SEA = 0
  val LAND = 1

  def main(args: Array[String]): Unit = {
    import scala.io.StdIn.readLine
    while (true) {
      val Array(nx, ny) = readLine().split(" ").map(_.toInt)
      if (nx == 0 && ny == 0) return
      
      val list = for {
        yi <- 0 until ny
        (t, xi) <- readLine().split(" ").map(_.toInt).zipWithIndex
        if t == LAND
      } yield {
        (xi, yi)
      }

      println(count(list.toSet))
    }
  }

  @tailrec
  def count(lands: Set[(Int, Int)], n: Int = 0): Int = {
    if (lands.isEmpty) {
      n
    } else {
      count(lands &~ splitIsland(lands), n + 1)
    }
  }

  def splitIsland(lands: Set[(Int, Int)]): Set[(Int, Int)] = {
    import scala.collection.mutable

    val one = mutable.Set[(Int, Int)]()

    def eachPosition(x: Int, y: Int): Unit = {
      for {
        xi <- x - 1 to x + 1
        yi <- y - 1 to y + 1 if lands.contains((xi, yi)) && !one.contains((xi, yi))
      } {
        one += ((xi, yi))
        eachPosition(xi, yi)
      }
    }

    val (x, y) = lands.head
    eachPosition(x, y)
    one.toSet
  }
}

