Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

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);
					}
				}
			}
		}
	}
	
}