Go: package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

C++: #include<bits//stdc++.h>
using namespace std;
int sum;
void solve(bool a[50][50],int h, int w, int i, int j){
	for(int z=-1;z<2;z++){
		for(int x=-1;x<2;x++){
			if(i+z>=0&&i+z<h&&j+x>=0&&j+x<w&&a[i+z][j+x]){
				a[i+z][j+x]=false;
				solve(a,h,w,i+z,j+x);
			}
		}
	}
}
int main(){
	int h,w;
	while(cin>>w>>h&&h!=0&&w!=0){
		bool a[50][50];
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				int x;
				cin>>x;
				if(x==0) a[i][j]=false;
				else a[i][j]=true;
			}
		}
		sum=0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(a[i][j]){
					sum++;
					solve(a,h,w,i,j);
				}
			}
		}
		cout<<sum<<endl;
	}
}

C++: #include <stdio.h>

void dps(int il[50][50], int y, int x)	{
	il[y][x] = 0;
	
	for (int dy = -1; dy <= 1; dy++)	{
		for (int dx = -1; dx <= 1; dx++)	{
			if (dy == 0 && dx == 0)	continue;
			if (y+dy < 0 || y+dy >= 50 || x+dx < 0 || x+dx >= 50)	continue;
			if (il[y+dy][x+dx] == 1)	dps(il, y+dy, x+dx);
		}
	}
}

int main()	{
	
	int w, h, cnt;
	int il[50][50];
	
	while (true)	{
		
		scanf("%d %d", &w, &h);
		if (w == 0)	break;
		
		cnt = 0;
		
		for (int y = 0; y < 50; y++)	{
			for (int x = 0; x < 50; x++)	{
				il[y][x] = 0;
			}
		}
		
		for (int y = 0; y < h; y++)	{
			for (int x = 0; x < w; x++)	{
				scanf("%d", &il[y][x]);
			}
		}
		
		for (int y = 0; y < h; y++)	{
			for (int x = 0; x < w; x++)	{
				if (il[y][x] == 1)	{
					dps(il, y, x);
					cnt++;
				}
			}
		}
		
		printf("%d\n", cnt);
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int fill_land(int y,int x,int h,int w,vector<vector<int>> &c);

int main()
{

	for (;;)
	{
		int w,h;
		cin >> w >> h;
		if ((w==0)&&(h==0))
		{
			return 0;
		}

		vector<vector<int>> c(h,vector<int>(w));

		for (int i=0;i<h;++i)
		{
			for (int j=0;j<w;++j)
			{
				cin >> c[i][j];
			}
		}

		int count=0;
		for (int i=0;i<h;++i)
		{
			for (int j=0;j<w;++j)
			{
				if (c[i][j]!=0)
				{
					// land
					++count;
					c[i][j]=0;
					fill_land(i,j,h,w,c);
				}
			}
		}
		cout << count << endl;
	}
}

int fill_land(int y,int x,int h,int w,vector<vector<int>> &c)
{
	stack<pair<int,int>> st;

	st.push(make_pair(y,x));
	while (st.empty()==0)
	{
		auto p=st.top();
		st.pop();

		if ( (p.first>0) && (c[p.first-1][p.second]!=0) )
		{
			c[p.first-1][p.second]=0;
			st.push(make_pair(p.first-1,p.second));
		}
		if ( (p.first<h-1) && (c[p.first+1][p.second]!=0) )
		{
			c[p.first+1][p.second]=0;
			st.push(make_pair(p.first+1,p.second));
		}
		if ( (p.second>0) && (c[p.first][p.second-1]!=0) )
		{
			c[p.first][p.second-1]=0;
			st.push(make_pair(p.first,p.second-1));
		}
		if ( (p.second<w-1) && (c[p.first][p.second+1]!=0) )
		{
			c[p.first][p.second+1]=0;
			st.push(make_pair(p.first,p.second+1));
		}
		if ( (p.first>0) && (p.second>0) && (c[p.first-1][p.second-1]!=0) )
		{
			c[p.first-1][p.second-1]=0;
			st.push(make_pair(p.first-1,p.second-1));
		}
		if ( (p.first>0) && (p.second<w-1) && (c[p.first-1][p.second+1]!=0) )
		{
			c[p.first-1][p.second+1]=0;
			st.push(make_pair(p.first-1,p.second+1));
		}
		if ( (p.first<h-1) && (p.second>0) && (c[p.first+1][p.second-1]!=0) )
		{
			c[p.first+1][p.second-1]=0;
			st.push(make_pair(p.first+1,p.second-1));
		}
		if ( (p.first<h-1) && (p.second<w-1) && (c[p.first+1][p.second+1]!=0) )
		{
			c[p.first+1][p.second+1]=0;
			st.push(make_pair(p.first+1,p.second+1));
		}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int c[100][100];
int w, h;
void dfs(int x,int y) {
	c[x][y] = 0;
	for (int i = -1; i <= 1; i++) {
		for (int j = -1; j <= 1; j++) {
			if (x + i < w&&x + i >= 0 && y + j >= 0 && y + j < h) {
				if (c[x + i][y + j] == 1) {
					dfs(x + i, y + j);
				}
			}
		}
	}
}

int main(void) {
	int ans;
	while (1) {
		ans = 0;
		cin >> w >> h;
		if (w == 0 && h == 0) {
			break;
		}
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> c[j][i];
			}
		}
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (c[j][i] == 1) {
					dfs(j, i);
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}