Go: package main

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

var sc = bufio.NewScanner(os.Stdin)
var route [][]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func dfs(h, w int) {
	if route[h][w] == 0 {
		return
	}

	route[h][w] = 0
	for _, x := range []int{-1, 0, 1} {
		for _, y := range []int{-1, 0, 1} {
			if route[h+x][w+y] == 0 {
				continue
			}
			dfs(h+x, w+y)
		}
	}
}

func main() {
	writer := bufio.NewWriter(os.Stdout)
	for {
		w, h := scanInt(), scanInt()
		if w == 0 && h == 0 {
			break
		}

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

		total := 0
		for i := 1; i <= h; i++ {
			for j := 1; j <= w; j++ {
				if route[i][j] == 1 {
					total++
					dfs(i, j)
				}
			}
		}
		fmt.Fprintln(writer, total)
	}
	writer.Flush()
}

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

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

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

	return;
}

int main(){

	int w, h;

	while (cin >> w >> h, w, h){

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

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

		cout << result << endl;

	}

	return 0;
}

//??????????????°??£????????????
//???????????±?????????????????????WA?????????
/*
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);
					}

					bool buff = island[c[j][i]];

					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]] = buff && 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 <iostream>
#include <cstdio>
using namespace std;
char field[51][51];
int W, H;

void dfs(int x, int y) {
  //cout << "field[x][y] = " << field[x][y] << endl;
  field[x][y] = '0';
  
  for(int dx = -1 ; dx <= 1 ; dx++) {
    for(int dy = -1 ; dy <= 1 ; dy++) {
      int nx = x + dx, ny = y + dy;
      if(0 <= nx && nx < W && 0 <= ny && ny < H && field[nx][ny] == '1') dfs(nx, ny);
    }
  }
}

int main(void) {
  while(cin >> W >> H, W != 0 && H != 0) {
    int res = 0;
    for(int i = 0 ; i < H ; i++)
      for(int j = 0 ; j < W ; j++) cin >> field[i][j];
    
    for(int i = 0 ; i < W ; i++) {
      for(int j = 0 ; j < H ; j++) {
	if(field[i][j] == '1') {
	  dfs(i, j);
	  
	  /*cout << endl;
	  for(int b = 0 ; b < H ; b++) {
	    for(int a = 0 ; a < W ; a++) {
	      cout << field[b][a] << ' ';
	    }
	    cout << endl;
	  }
	  cout << endl;*/
	  res++;
	}
      }
    }
    cout << res << endl;
  }
  return 0;
}

C++: #include<stdio.h>
#include<iostream>

int field[51][51];
int w,h;

void land(int x,int y){
	field[x][y]=0;

	for(int yy=-1;yy<=1;yy++){
		for(int xx=-1;xx<=1;xx++){
			if(x+xx>=0&&x+xx<=w&&y+yy>=0&&y+yy<=h){
			   if(field[x+xx][y+yy]==1) land(x+xx,y+yy);
			}
		}
	}
	return;
}

int main(void){

	int kazu=0;

	while(1){

	scanf("%d %d",&w,&h);

	if(w==0&&h==0) break;

	kazu=0;

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			scanf("%d",&field[j][i]);
		}
	}

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){

			if(field[j][i]==1){
				land(j,i);
				kazu++;
			}

		}
	}

	printf("%d\n",kazu);
	}

	return 0;
}

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

int H, W;
vector<vector<int>> field(H, vector<int>(W));
vector<vector<bool>> seen(H, vector<bool>(W));

const int dh[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dw[8] = {0, 1, 0, -1, 1, -1, 1, -1};

void dfs(int h, int w)
{
    seen[h][w] = true;
    int nh, nw;
    for (int i=0; i<8; i++)
    {
        nh = h + dh[i];
        nw = w + dw[i];
        if (nh<0 || nh>=H || nw<0 || nw>=W) continue;
        if (seen[nh][nw]) continue;
        if (field[nh][nw]==0) continue;
        dfs(nh, nw);
    }
}

int main()
{
    while (cin >> W >> H)
    {
        if (W==0 || H==0) break;
        field.resize(H);
        seen.resize(H);
        for (int h=0; h<H; h++)
        {
            field[h].resize(W);
            seen[h].resize(W);
        }
        for (int h=0; h<H; h++)
        {
            for (int w=0; w<W; w++) 
            {
                cin >> field[h][w];
                seen[h][w]=false;
            }
        }
        int answer=0;
        for (int h=0; h<H; h++)
        {
            for (int w=0; w<W; w++)
            {
                if (seen[h][w]==false && field[h][w]==1) dfs(h, w), answer++;
            }
        }
        cout << answer << endl;
    }
}

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

int MAP(vector<vector<int>> &m, int i, int j){
	int w = m[0].size();
	int h = m.size();

	if(!(0 <= i && i < h && 0 <= j && j < w)){
		return 0;
	}

	if(m[i][j] == 0){
		return 0;
	}
	else if(m[i][j] = 1){
		m[i][j] = 0;
		MAP(m, i - 1, j - 1);
		MAP(m, i, j - 1);
		MAP(m, i + 1, j - 1);
		MAP(m, i - 1, j);
		MAP(m, i + 1, j);
		MAP(m, i - 1, j + 1);
		MAP(m, i, j + 1);
		MAP(m, i + 1, j + 1);
	}
	return 1;
}

int main(){
	int w, h;

	while(true){
		int count = 0;
		cin >> w >> h;

		if(w == 0 && h == 0){
			break;
		}

		vector<vector<int>> map(h, vector<int>(w, 0));

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

		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				count += MAP(map, i , j);
			}
		}

		cout << count << endl;
	}
}

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

#define rep(i, n) for (int i = 0; i < (n); i++)

template <class T>
T next()
{
    T x;
    cin >> x;
    return (x);
}

int w, h;
vector<vector<int>> islands;

void dfs(int ii, int jj)
{
    islands[jj][ii] = 0;
    for (int i = ii - 1; i <= ii + 1; i++)
    {
        for (int j = jj - 1; j <= jj + 1; j++)
        {
            if (i < 0 || i == w || j < 0 || j == h)
            {
            }
            else if (islands[j][i] > 0)
            {
                dfs(i, j);
            }
        }
    }
}

int main()
{
    vector<int> ans;
    while (true)
    {
        int cnt = 0;
        w = next<int>();
        h = next<int>();
        if (w + h == 0)
        {
            break;
        }
        islands.resize(h);
        rep(j, h)
        {
            islands[j].resize(w);
            rep(i, w)
            {
                cin >> islands[j][i];
            }
        }
        rep(j, h)
        {
            rep(i, w)
            {
                if (islands[j][i] > 0)
                {
                    cnt++;
                    dfs(i, j);
                }
            }
        }
        ans.push_back(cnt);
    }
    for (int i : ans)
    {
        cout << i << endl;
    }
}