def dfs(c, i, j)
  c[i][j] = 0
  (-1..1).each{|di|
    (-1..1).each{|dj|
      if (0...c.size).include?(i+di) && (0...c[0].size).include?(j+dj) && c[i+di][j+dj] == 1
        dfs(c, i+di, j+dj)
      end
    }
  }
end

loop {
  w, h = gets.split.map &:to_i
  break if w == 0
  c = h.times.map{ gets.split.map &:to_i }
  count = 0
  h.times{|i|
    w.times{|j|
      if c[i][j] == 1
        dfs(c, i, j)
        count += 1
      end
    }
  }
  puts count
}
