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;

const int MAX_LENGTH = 50;

int map[MAX_LENGTH][MAX_LENGTH];

void dfs(int x, int y) {
	for (int i = -1; i <= 1; i++) {
		for (int j = -1; j <= 1; j++) {
			if (y + i >= 0 && MAX_LENGTH > y + i && x + j >= 0 && MAX_LENGTH > x + j && map[y+i][x+j] == 1) {
				map[y+i][x+j] = 0;
				dfs(x + j, y + i);
			}
		}
	}

	return;
}

int main() {
	while (true) {
		int w, h, count;
		cin >> w >> h;
		if (w == 0) {
			break;
		}
		for (int i = 0; i < MAX_LENGTH; i++) {
			for (int j = 0; j < MAX_LENGTH; j++) {
				map[i][j] = 0;
			}
		}
		
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> map[i][j];
			}
		}
		
		count = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (map[i][j] == 1) {
					map[i][j] = 0;
					dfs(j, i);
					count++;
					/*
					for (int k = 0; k < h; k++) {
						for (int l = 0; l < w; l++) {
							cout << map[k][l] << " ";
						}
						cout << endl;
					}
					cout << endl;
					*/
				}
			}
		}
		cout << count << endl;
	}

	return 0;
}

C++: #include<algorithm>
#include<bitset>
#include<iostream>
#include<iomanip>
#include<set>
#include<string>
#include<math.h>

using namespace std;

class zahyo{
	public:
	int x;
	int y;
	zahyo(){
		x=0;
		y=0;
	}
	zahyo(int x_,int y_){
		x=x_;
		y=y_;
	}
	void print(){
		cout<<x<<" "<<y;
	}
	zahyo operator+(zahyo add){
		add.x=this->x+add.x;
		add.y=this->y+add.y;
		return add;
	}
	zahyo operator-(zahyo add){
		add.x=this->x-add.x;
		add.y=this->y-add.y;
		return add;
	}
	bool operator==(const zahyo add) const{
		if(add.x==this->x and add.y==this->y){return true;}
		else{return false;}
	}
	bool operator!=(const zahyo add) const{
		if(add.x==this->x or add.y==this->y){return false;}
		else{return true;}
	}
	bool operator<(const zahyo add) const{
		if(this->x<add.x){return true;}
		else if(this->x==add.x and this->y<add.y){return true;}
		else{return false;}
	}
	bool operator>(const zahyo add) const{
		if(this->x>add.x){return true;}
		else if(this->x==add.x and this->y>add.y){return true;}
		else{return false;}
	}
	void input(){
		cin>>x>>y;
	}
};

int n;
int counter=0;
int field[50][50]={{}};
int arrived[50][50]={{}};
int ways[8][2]={{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1},{0,1}};
int h,w;
void depthsearch(int h_,int w_){
	arrived[h_][w_]=1;
	for(int i=0;i<8;i++){
		int newh=h_+ways[i][0];
		int neww=w_+ways[i][1];
		if(newh>=0 and newh<h and neww>=0 and neww<w){
			if(arrived[newh][neww]==0 and field[newh][neww]==1){
				depthsearch(newh,neww);
			}
		}
	}
}

int main(){

	while(1){
		cin>>w>>h;
		if(w==0 and h==0){break;}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin>>field[i][j];
				arrived[i][j]=0;
			}
		}
		int ans=0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(arrived[i][j]==0 and field[i][j]==1){
					depthsearch(i,j);
					ans++;
				}
			}
		}
		cout<<ans<<endl;
	}
}

C++: #include <iostream>
using namespace std;

void proc(int *maps,int p,int w,int h){
	if(maps[p]==1) maps[p]=2;
	else return;
	int x=p%w;
	int y=p/w;
	for(int i=-1;i<=1;++i){
		for(int j=-1;j<=1;++j){
			int nx=x+j;
			int ny=y+i;
			if(nx>=0&&ny>=0&&nx<w&&ny<h){
				proc(maps,nx+ny*w,w,h);
			}
		}
	}
}

int main(void){
	int w,h;
	while(cin>>w>>h,w&&h){
		int chizu[2500];
		for(int i=0;i<h;++i){
			for(int j=0,a;j<w;++j){
				cin>>a;
				chizu[i*w+j]=a;
			}
		}
		int count = 0;
		for(int i=0;i<w*h;++i){
			if(chizu[i]==1){
				proc(chizu,i,w,h);
				++count;
			}
		}
		cout<<count<<endl;
	}
	return 0;
}

C++: #include <iostream>

using namespace std;

int w, h;
int c[50][50];
char dx[] = {-1,-1,-1,0,1,1,1,0};
char dy[] = {-1,0,1,1,1,0,-1,-1};

void dfs(int x, int y){
	c[x][y] = 0;
	for(int i=0;i<8;i++){
		int nx = x+dx[i], ny = y+dy[i];
		if(nx<0||h<=nx||ny<0||w<=ny||!c[nx][ny]) continue;
		dfs(nx,ny);
	}
}

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