Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		return []byte(f1())
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

// -----------------------------------------------------------------------------

func main() {
	defer stdout.Flush()

	for {
		ans, ok := solve()
		if !ok {
			break
		}
		println(ans)
	}

}

func solve() (int, bool) {
	w := readInt()
	h := readInt()
	if w == 0 && h == 0 {
		return 0, false
	}

	c := make([][]int, h+2)
	c[0] = make([]int, w+2)
	c[h+1] = make([]int, w+2)
	for i := 1; i-1 < h; i++ {
		c[i] = make([]int, w+2)
		for j := 1; j-1 < w; j++ {
			c[i][j] = readInt()
		}
	}

	var ans int
	for i := 1; i-1 < h; i++ {
		for j := 1; j-1 < w; j++ {
			if 0 < c[i][j] {
				ans++
				dfs(c, i, j)
			}
		}
	}
	return ans, true
}

func dfs(c [][]int, y, x int) {
	c[y][x] = 0
	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{-1, -1, -1, 0, 0, 1, 1, 1}
	for i := range dx {
		nx := x + dx[i]
		ny := y + dy[i]
		if 0 < c[ny][nx] {
			dfs(c, ny, nx)
		}
	}
}

// -----------------------------------------------------------------------------

C++: #include<iostream>

using namespace std;

void riku(int,int);
int w,h;
int map[51][51],num;

int main(){
	//テ」ツつ?」ツ?」テ」ツ?アテ」ツδ催」ツδε」ツδ暗」ツ?ォテ」ツつづ」ツ?セテ」ツ?ィテ」ツつづ」ツ?ォテヲツ崢クテ」ツ??」ツ?ヲテ」ツ??」ツつ凝、ツコツコテ」ツ?ッテ」ツ??」ツ??
	//テァツ可ケテ」ツ?ォテ・ツ、ツ嘉」ツ?ェテヲツゥツ淌ィツδステ」ツ?ッテ、ツスツソテ」ツ?」テ」ツ?ヲテ」ツ??」ツ?ェテ」ツ??・ツ?催・ツクツーテ」ツδ療」ツδュテ」ツつーテ」ツδゥテ」ツδ?」ツ??」ツ?」テ」ツ?淌」ツ??
	cin>>w;
	cin>>h;
	while(w!=0 && h!=0){
		//テ・ツ?・テ・ツ環崚」ツ?ィテ・ツ按敕ヲツ慊淌・ツ個?
		for(int lop=0;lop<51;lop++){
			for(int lop2=0;lop2<51;lop2++){
				map[lop][lop2]=0;
			}
		}
		for(int lop=0;lop<h;lop++){
			for(int lop2=0;lop2<w;lop2++){
				cin>>map[lop][lop2];
			}
		}
		num=0;
		
		//テ」ツ?禿」ツ?禿」ツ?凝」ツつ嘉ヲツ慊ャテゥツ。ツ?
		for(int lop=0;lop<h;lop++){
			for(int lop2=0;lop2<w;lop2++){
				if(map[lop][lop2]==1){
					riku(lop,lop2);
					num++;
				}
			}
		}
		cout<<num<<endl;
		
		cin>>w;
		cin>>h;
	}
	return 0;
}

void riku(int hh,int ww){
	//テ」ツ?禿」ツ?ョテゥツ鳴「テヲツ閉ーテ」ツつ津・ツ?催・ツクツーテ」ツ?療」ツ?ヲ
	//テ、ツクツ?」ツ?、テ」ツ?ョテゥツ卍クテ」ツ?ォテ」ツ?、テ」ツ?ェテ」ツ?古」ツ?」テ」ツ?ヲテ」ツ??」ツつ凝」ツ?ィテ」ツ?禿」ツつ催」ツつ津」ツ?、テ」ツ?カテ」ツ?療」ツ?ヲテ」ツ??」ツ??
	//テ・ツ?ィテゥツδィテヲツスツーテ」ツ?療」ツ?淌」ツつ?テ」ツ?、テ」ツ?ョテゥツ卍クテ」ツ?ィテ」ツ?療」ツ?ヲテ」ツつォテ」ツつヲテ」ツδウテ」ツδ暗」ツ?療」ツ?ヲテ」ツ??」ツつ?
	map[hh][ww]=0;
	int hhh,www;
	
	/*for(int lop=-1;lop<=1;lop++){
		for(int lop2=-1;lop2<=1;lop2++){
			hhh=hh+lop2;
			www=ww+lop;
			if(0<=www && www<w && 0<=hhh && hhh<h && map[hhh][www]==1){
				riku(hhh,www);
			}
		}
	}*/
	
	for(int lop=-1;lop<=1;lop++){
		for(int lop2=-1;lop2<=1;lop2++){
			if(hh+lop>=0 && ww+lop2>=0){//テ・ツキツヲテァツォツッorテ、ツクツ甘ァツォツッテ」ツ?ョテヲツ卍づ」ツ?ッテ」ツつケテ」ツδォテ」ツδシ
				if(hh+lop<h && ww+lop2<w){//テ・ツ渉ウテァツォツッorテ、ツクツ凝ァツォツッテ」ツ?ェテ」ツつ嘉」ツつケテ」ツδォテ」ツδシ
					if(map[hh+lop][ww+lop2]==1){
						//テゥツ堋」テヲツ篠・テ」ツ?療」ツ?ヲテ」ツ??」ツつ凝」ツδ榲」ツつケテ」ツ??テ」ツ?ョテヲツ卍?
						//テ・ツ?催・ツクツーテ」ツ?療」ツ?ヲテ」ツ?敕」ツ?ョテ」ツδ榲」ツつケテ」ツ?ョテゥツ堋」テヲツ篠・テ」ツδ榲」ツつケテ」ツつ津ィツェツソテ」ツ?ケテ」ツつ?
						riku(hh+lop,ww+lop2);
					}
				}
			}
		}
	}
	
}