def dfs(f, i, j)
    return f if f[i][j] == 0
    f[i][j] = 0
    if i.between?(0, f.size-2) && j.between?(0, f[0].size-2) && f[i+1][j+1] == 1
        f = dfs(f, i+1, j+1)
    end
    if i.between?(0, f.size-2) && j.between?(0, f[0].size-1) && f[i+1][j] == 1
        f = dfs(f, i+1, j)
    end
    if i.between?(0, f.size-2) && j.between?(1, f[0].size-1) && f[i+1][j-1] == 1
        f = dfs(f, i+1, j-1)
    end
    if i.between?(0, f.size-1) && j.between?(0, f[0].size-2) && f[i][j+1] == 1
        f = dfs(f, i, j+1)
    end
    if i.between?(0, f.size-1) && j.between?(1, f[0].size-1) && f[i][j-1] == 1
        f = dfs(f, i, j-1)
    end
    if i.between?(1, f.size-1) && j.between?(0, f[0].size-2) && f[i-1][j+1] == 1
        f = dfs(f, i-1, j+1)
    end
    if i.between?(1, f.size-1) && j.between?(0, f[0].size-1) && f[i-1][j] == 1
        f = dfs(f, i-1, j)
    end
    if i.between?(1, f.size-1) && j.between?(1, f[0].size-1) && f[i-1][j-1] == 1
        f = dfs(f, i-1, j-1)
    end
    return f
end

loop do
    m, n = gets.split.map(&:to_i)
    break if m == 0 && n == 0
    f = n.times.map{gets.split.map(&:to_i)}
    count = 0
    f.each_with_index do |r,i|
        r.each_with_index do |c,j|
            next if c == 0
            f = dfs(f, i, j)
            count += 1
        end
    end
    puts count
end