import scala.io.StdIn


object Main {
  def main(args: Array[String]): Unit = {
    while(true) {
      val n :: m :: _res = StdIn.readLine().split(" ").map(_.toInt).toList
      if(n == 0) { return; }

      val c = (for {
        i <- 1 to m
      } yield {
        StdIn.readLine.split(" ").map(_.toInt)
      }).toArray

      println(solve(n, m, c))
    }
  }



  def solve(w: Int, h: Int, c: Array[Array[Int]]): Int = {
    case class I(x: Int, y: Int, var isLand: Boolean) {
      def coodinateIndexes: Seq[(Int, Int)] = {
        //if(!isLand) return Seq.empty

        (for {
          i <- List(-1, 0, 1)
          j <- List(-1, 0, 1)
          if !(i == 0 && j == 0)
        } yield (x + i, y + j)).filterNot{ case (nx, ny) =>
            (nx == x && ny == y) ||
            (nx < 0) || (nx >= w) ||
            (ny < 0) || (ny >= h)
        }
      }
    }

    val is = c.zipWithIndex.map{ case (row, rId) =>
      row.zipWithIndex.map{ case (i, cId) =>
        I(cId, rId, i == 1)
      }
    }


    def rec(i: Int, j: Int): Int = {
      val now = is(j)(i)
      if(now.isLand) {
        now.isLand = false
        now.coodinateIndexes.filter{ case (x, y) =>
          is(y)(x).isLand
        }.map{ case (x, y) => rec(x, y)}
        1
      } else 0
    }

    (for {
      i <- 0 until w
      j <- 0 until h
    } yield rec(i, j)).sum
  }
}