package main

import (
	"bufio"
	"fmt"
	"math/big"
	"os"
	"strconv"
)

var (
	scan = NewScanner()
)

func DFS(field [][]int, h, w int) {
	field[h][w] = 0
	for dh := -1; dh <= 1; dh++ {
		for dw := -1; dw <= 1; dw++ {
			ch, cw := h+dh, w+dw
			if ch < 0 || len(field) <= ch || cw < 0 || len(field[0]) <= cw || field[ch][cw] == 0 {
				continue
			}
			DFS(field, ch, cw)
		}
	}
}

func Solve(field [][]int) int {
	count := 0
	for h, row := range field {
		for w, c := range row {
			if c == 0 {
				continue
			}
			DFS(field, h, w)
			count++
		}
	}
	return count
}

func main() {
	for W, H := scan.Int(), scan.Int(); W != 0 || H != 0; W, H = scan.Int(), scan.Int() {
		field := scan.IntSliceDim2(H, W)
		ans := Solve(field)
		fmt.Println(ans)
	}
}

// --------------------------------------------------------------------------------
// Input

type Scanner struct{ *bufio.Scanner }

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(nil, 100000000)
	return &Scanner{sc}
}

func (s *Scanner) String() string {
	s.Scan()
	return s.Text()
}

func (s *Scanner) StringSlice(l int) []string {
	if l == 0 {
		return []string{}
	}
	sl := make([]string, l, l)
	for i := range sl {
		sl[i] = s.String()
	}
	return sl
}

func (s *Scanner) Int() int {
	n, _ := strconv.Atoi(s.String())
	return n
}

func (s *Scanner) IntSlice(l int) []int {
	if l == 0 {
		return []int{}
	}
	sl := make([]int, l, l)
	for i := range sl {
		sl[i] = s.Int()
	}
	return sl
}

func (s *Scanner) IntPair() [2]int {
	return [2]int{s.Int(), s.Int()}
}

func (s *Scanner) IntPairSlice(l int) [][2]int {
	if l == 0 {
		return [][2]int{}
	}
	sl := make([][2]int, l, l)
	for i := range sl {
		sl[i] = s.IntPair()
	}
	return sl
}

func (s *Scanner) IntSliceDim2(l1, l2 int) [][]int {
	if l1 == 0 {
		return [][]int{}
	}
	sl := make([][]int, l1, l1)
	for i := range sl {
		sl[i] = s.IntSlice(l2)
	}
	return sl
}

func (s *Scanner) Int64() int64 {
	n, _ := strconv.ParseInt(s.String(), 10, 64)
	return n
}

func (s *Scanner) BigInt() *big.Int {
	return big.NewInt(s.Int64())
}

func (s *Scanner) BigIntSlice(l int) []*big.Int {
	if l == 0 {
		return []*big.Int{}
	}
	sl := make([]*big.Int, l, l)
	for i := range sl {
		sl[i] = s.BigInt()
	}
	return sl
}

func (s *Scanner) Float64() float64 {
	f, _ := strconv.ParseFloat(s.String(), 64)
	return f
}

func (s *Scanner) Float64Slice(l int) []float64 {
	if l == 0 {
		return []float64{}
	}
	sl := make([]float64, l, l)
	for i := range sl {
		sl[i] = s.Float64()
	}
	return sl
}

// --------------------------------------------------------------------------------
// Output

const (
	Yes = "Yes"
	No  = "No"

	YES = "YES"
	NO  = "NO"
)

func PrintBool(v bool, trueStr, falseStr string) {
	if v {
		fmt.Println(trueStr)
	} else {
		fmt.Println(falseStr)
	}
}

// --------------------------------------------------------------------------------
// sort.BigIntSlice

type BigIntSlice []*big.Int

func (sl BigIntSlice) Len() int {
	return len(sl)
}

func (sl BigIntSlice) Less(i, j int) bool {
	return sl[i].Cmp(sl[j]) == -1
}

func (sl BigIntSlice) Swap(i, j int) {
	sl[i], sl[j] = sl[j], sl[i]
}

// --------------------------------------------------------------------------------
// Mint

const (
	Mod = 1000000007
)

type Mint int

func NewMint(value int) Mint {
	return Mint((value%Mod + Mod) % Mod)
}

func (x Mint) Add(y Mint) Mint {
	if v := x + y; v >= Mod {
		return v - Mod
	} else {
		return v
	}
}

func (x Mint) Sub(y Mint) Mint {
	if v := x + Mod - y; v >= Mod {
		return v - Mod
	} else {
		return v
	}
}

func (x Mint) Mul(y Mint) Mint {
	return (x * y) % Mod
}

func (x Mint) pow(n int) Mint {
	if n == 0 {
		return Mint(1)
	}
	a := x.pow(n >> 1)
	a = a.Mul(a)
	if n&1 != 0 {
		a = a.Mul(x)
	}
	return a
}

func (x Mint) Inverse() Mint {
	return x.pow(Mod - 2)
}

func (x Mint) Div(y Mint) Mint {
	return x * y.Inverse()
}

func (x Mint) Neg() Mint {
	return NewMint(int(-x))
}

// --------------------------------------------------------------------------------
// Factorial

type Factorial struct {
	fact        []Mint
	factInverse []Mint
}

func NewFactorial(n int) *Factorial {
	f := new(Factorial)
	f.init(n)
	return f
}

func (f *Factorial) init(n int) {
	fact := make([]Mint, n+1, n+1)
	fact[0] = NewMint(1)
	for i := 1; i <= n; i++ {
		fact[i] = fact[i-1].Mul(NewMint(i))
	}

	inv := make([]Mint, n+1, n+1)
	inv[n] = fact[n].Inverse()
	for i := n; i >= 1; i-- {
		inv[i-1] = inv[i].Mul(NewMint(i))
	}

	f.fact = fact
	f.factInverse = inv
}

func (f *Factorial) Get(n int) Mint {
	return f.fact[n]
}

func (f *Factorial) GetInverse(n int) Mint {
	return f.factInverse[n]
}

func (f *Factorial) Permutation(n, k int) Mint {
	if k < 0 || n < k {
		return 0
	}
	return f.fact[n].Mul(f.factInverse[n-k])
}

func (f *Factorial) Combination(n, k int) Mint {
	if k < 0 || n < k {
		return 0
	}
	return f.fact[n].Mul(f.factInverse[k]).Mul(f.factInverse[n-k])
}

// --------------------------------------------------------------------------------
// PrimeFactorization

func PrimeFactorization(n int) map[int]int {
	m := map[int]int{}
	for i := 2; i*i <= n; i++ {
		for n%i == 0 {
			n /= i
			m[i]++
		}
	}
	if n != 1 {
		m[n]++
	}
	return m
}

// --------------------------------------------------------------------------------
// Sieve

type Sieve struct {
	n          int
	primes     []int
	maxFactors []int
}

func NewSieve(n int) *Sieve {
	sieve := &Sieve{
		n:          n,
		primes:     []int{},
		maxFactors: make([]int, n+1, n+1),
	}
	sieve.init()
	return sieve
}

func (s *Sieve) init() {
	s.maxFactors[0] = -1
	s.maxFactors[1] = -1
	for i := 2; i <= s.n; i++ {
		if s.maxFactors[i] != 0 {
			continue
		}
		s.primes = append(s.primes, i)
		s.maxFactors[i] = i
		for j := i * i; j <= s.n; j += i {
			if s.maxFactors[j] == 0 {
				s.maxFactors[j] = i
			}
		}
	}
}

func (s *Sieve) IsPrime(x int) bool {
	return s.maxFactors[x] == x
}

func (s *Sieve) PrimeFactorization(x int) map[int]int {
	m := map[int]int{}
	for x != 1 {
		m[s.maxFactors[x]]++
		x /= s.maxFactors[x]
	}
	return m
}

// --------------------------------------------------------------------------------
// MathUtil

func Pow(x, p int) int {
	pow := 1
	for i := 0; i < p; i++ {
		pow *= x
	}
	return pow
}



Measured memory_bytes: 1716