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;

bool field[100][100];

int w, h;

int dx[8] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[8] = {0, -1, 0, 1, -1, 1, -1, 1};

void bfs(int x, int y) {
  field[y][x] = false;
  for(int i = 0; i < 8; i++) {
    int tx = x+dx[i], ty = y+dy[i];

    if(0 <= tx && tx < w && 0 <= ty && ty < h && field[ty][tx]) {
      bfs(tx, ty);
    }
  }
}

int main() {
  while(cin >> w >> h, w+h) {
    for(int i = 0; i < h; i++) {
      for(int j = 0; j < w; j++) {
        cin >> field[i][j];
      }
    }

    int ans = 0;
    for(int i = 0; i < h; i++) {
      for(int j = 0; j < w; j++) {
        if(field[i][j]) {
          ans++;
          bfs(j, i);
          /*for(int k = 0; k < h; k++) {
            for(int l = 0; l < w; l++) {
              cout<< field[k][l] << ' ';
            }
            cout<< endl;
          }
          cout << endl;*/
        }
      }
    }
    cout << ans << endl;
  }
}

C++: #include <iostream>
#include <queue>

using namespace std;

struct  pos {
	int x, y;
	pos(int _x, int _y) { x = _x; y = _y;}
};

int a[50][50];

void ans(int p,int q,int h,int w);

int main(){
	int H,W,count;
	while(1){
		count=0;
		cin >> H;
		cin >> W;
		if(H==0&&W==0){
			break;
		}
		for(int i=0;i<W;i++){
			for(int j=0;j<H;j++){
				cin >> a[i][j];
			}
		}
		
		for(int i=0;i<W;i++){
			for(int j=0;j<H;j++){
				if(a[i][j]==1){
					a[i][j]=0;
					ans(i,j,H,W);
					count++;
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}

void ans(int p, int q, int h, int w)
{
	queue<pos> Q;
	Q.push(pos(p, q));
	while (Q.size()) {
		pos b= Q.front();
		Q.pop();
		for (int i=-1;i<=1;i++) {
			for (int j=-1;j<=1;j++) {
				if(0<=b.x+i&&b.x+i<w&&0<=b.y+j&&b.y+j<h){
					if(a[b.x+i][b.y+j]==1){
						a[b.x+i][b.y+j]=0;
						Q.push(pos(b.x+i,b.y+j));
					}
				}
			// mawariwomiru
			}
		}
	}
}


/*
void ans(int p,int q,int h,int w){
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			if(0<=p+i&&p+i<h&&0<=q+j&&q+j<w){
				if(a[p+i][q+j]==1){
					a[p+i][q+j]=0;
					Q.push(pos(p+i,q+j));
				}
			}
		}
	}
	if(Q.size()){
		pos b=Q.front();
		Q.pop();
		ans(b.x,b.y,h,w);
	}
}*/

C++: #include <iostream>
#include <vector>
using namespace std;
const int di[8] = {-1, 0, 1, 0, -1, -1, 1, 1};
const int dj[8] = {0, 1, 0, -1, -1, 1, 1, -1};
bool ongrid(int h, int w, vector< vector<int> >& g)
{
    if (h >= 0 && h < g.size() && w >= 0 && w < g[0].size()) return true;
    return false;
}

bool dfs(int h, int w, vector< vector<int> >& g, vector< vector<bool> >& seen)
{
    seen[h][w] = true;
    if (g[h][w] == 0) return false;
    for (int i = 0; i < 8; i++)
    {
        if (ongrid(h+di[i], w+dj[i], g) && seen[h+di[i]][w+dj[i]] == false)
            dfs(h+di[i], w+dj[i], g, seen);
    }
    return true;
}

int main()
{
    while (true)
    {
        int w, h; cin >> w >> h;
        if (w == 0 && h == 0) break;
        vector< vector<int> > g(h, vector<int>(w));
        for (int i = 0; i < h; i++)
            for (int j = 0; j < w; j++)
                { int c; cin >> c; g[i][j] = c;}
        vector< vector<bool> > seen(h, vector<bool>(w, false));
        int cnt = 0;
        for (int i = 0; i < h; i++)
            for (int j = 0; j < w; j++)
                if (seen[i][j] == false)
                    if (dfs(i, j, g, seen) == true) cnt++;
        cout << cnt << endl;
    }
}

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


int main(void){

	int w, h;

	while (cin >> w >> h, w, h){
		int c[100][100] = { 0 };
		vector<bool> island;
		island.push_back(false);

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

		int count = 1;
		for (int j = 1; j <= h; j++){
			for (int i = 1; i <= w; i++){
				if (c[j][i] != 0){

					if (c[j][i - 1] != 0) c[j][i] = c[j][i - 1];
					else if (c[j - 1][i + 1] != 0) c[j][i] = c[j - 1][i + 1];
					else if (c[j - 1][i] != 0) c[j][i] = c[j - 1][i];
					else if (c[j - 1][i - 1] != 0) c[j][i] = c[j - 1][i - 1];
					else{
						c[j][i] = count;
						count++;
						island.push_back(true);
					}

					island[c[j][i - 1]] = island[c[j - 1][i + 1]] = island[c[j - 1][i]] = island[c[j - 1][i - 1]] = false;
					island[c[j][i]] = true;

				}
			}
		}

		int result = 0;
		vector<bool>::iterator it;
		for (it = island.begin(); it != island.end(); ++it) 	if (*it == true) result++;
		
		cout << result << endl;

	}

	return 0;
}

/*
for(i=0;i<N;i++){
cin >> X[i];
}


for(i=0;i<N;i++){
for(j=0;j<N;j++){
cin >> X[i][j];
}
}
*/

C++: #include<stdio.h>

#define R(q,n) for(int q=0; q<n; q++)
size_t m[50][50],W,H,i,j,a,c,d;

void f(int x, int y)
{
	m[x][y]=0;
	R(b,9)
	{
		c = x+b/3-1;
		d = y+b%3-1;

		if(c<W&&d<H&&m[c][d])
			f(c,d);
	}
}

main()
{
	while(scanf("%d%d",&W,&H),W&H)
	{
		a=0;
		R(i,H) R(j,W)
			scanf("%d", &m[j][i]);

		R(i,H) R(j,W)
		if(m[j][i]){
				f(j,i);
				a++;
		}
		printf("%d\n", a);
	}
}