object Main{

  var h:Int = 0
  var w:Int = 0

  def isValid(x:Int,y:Int):Boolean = {
    if( x < 0 || x >= this.w || y < 0 || y >= this.h) false
    else true
  }

  def dfs(x:Int,y:Int,cs: Array[Array[Int]]):Int = {
    val dxy = Array(1,0,-1)
    val ans = cs(y)(x)
    cs(y)(x) = 0

    for(i<-0 until 3){
      for(j<-0 until 3){
        if( isValid(x+dxy(j),y+dxy(i)) ){
          if(cs(y+dxy(i))(x+dxy(j)) == 1){
            dfs(x+dxy(j),y+dxy(i),cs)
          }
        }
      }
    }

    return ans
  }

  def main(args: Array[String]):Unit = {
    var wh = readLine.split(" ").map(n => n.toInt)
    while(wh(0)!=0 && wh(1)!=0){
      this.w = wh(0)
      this.h = wh(1)
      val cs = Array.ofDim[Int](h,w)
      for( i<- 0 until h){
        cs(i) = readLine.split(" ").map(n => n.toInt)
      }

      var ans = 0
      for( y<-0 until h){
        for( x<-0 until w){
          if(cs(y)(x)==1)
            ans += dfs(x,y,cs)

        }
      }
      println(ans)
      wh = readLine.split(" ").map(n => n.toInt)
    }
  }

}
  