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;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MOD (int) (1e9+7)
#define INF (int) 2e9

bool seen[50*50];
vector<int> edges[50*50];

void dfs(int s, int p){
    seen[s] = true;
    for (auto v: edges[s]){
        if(v==p) continue;
        if(!seen[v]) dfs(v, s);
    }
}


int main(){
    int w, h;
    int di[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
    int dj[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
    
    while(true){
        cin >> w >> h;
        if(w==0 && h==0) break;
        int c[h][w];
        rep(i, h){
            rep(j, w){
                cin >> c[i][j];
                edges[w*i+j] = {};
            }
        }

        // 隣接リスト構築
        rep(i, h){
            rep(j, w){
                if(c[i][j]==1){
                    rep(k, 8){
                        int i_ = i+di[k];
                        int j_ = j+dj[k];
                        if(i_>=0 && i_<h && j_>=0 && j_<w && c[i_][j_]==1){
                            int idx = w * i_ + j_;
                            edges[w*i+j].emplace_back(idx);
                            // edges[idx].emplace_back(w*i+j);
                        }
                    }
                }
            }
        }

        rep(i, h){
            rep(j, w){
                seen[w*i+j] = false;
            }
        }

        // ここからdfs実行
        int cnt = 0;
        rep(i, h){
            rep(j, w){
                int num = w*i+j;
                if(c[i][j]==1 && !seen[num]){
                    dfs(num, -1);
                    cnt++;
                }
            }
        }
        cout << cnt << endl;
    }

    // printf("%d\n", N);
    return 0;
}

C++: #include<iostream>
#include<cstdlib>
#include<cstring>
#include<stack>
#include<queue>
#include<algorithm>
#include<vector>
#include<math.h>
#include<bitset>
#include<iomanip>
#include<numeric>
using namespace std;

typedef stack<int> IntStack;
typedef queue<int> IntQueue;

const double pi = 3.141592653589793;


//最大公約数
template<class T> T gcd(T a, T b)
{
	while (true)
	{
		T r;
		if (a%b == 0) return b;
		else
		{
			r = a % b;
			a = b;
			b = r;
		}
	}
}
//最小公倍数
template<class T> T lcm(T a, T b)
{
	return a / gcd(a, b)*b;
}
//文字0~9をint型0~9に変換
int char_to_int(char c)
{
	if (c == '0') return 0;
	if (c == '1') return 1;
	if (c == '2') return 2;
	if (c == '3') return 3;
	if (c == '4') return 4;
	if (c == '5') return 5;
	if (c == '6') return 6;
	if (c == '7') return 7;
	if (c == '8') return 8;
	if (c == '9') return 9;
	else return -1;
}
//a^b mod MODを計算
long long int ModPower(long long int a, long long int b, long long int MOD)
{
	if (b == 0) return 1;
	else if (b % 2 == 0)
	{
		long long int d = ModPower(a, b / 2, MOD);
		return (d*d) % MOD;
	}
	else
	{
		return (a*ModPower(a, b - 1, MOD)) % MOD;
	}
}
//nCr mod MODを計算
long long int ModCombi(long long int n, long long int r, long long int MOD)
{
	if (n == 0 || r == 0) return 1;

	long long int Kn = 1, Kr = 1, Knr = 1;

	long long int temp = 1;
	for (long long int i = 1; i <= n; i++)
	{
		temp *= i;
		temp %= MOD;
		if (i == n) Kn = temp;
		if (i == r) Kr = temp;
		if (i == n - r) Knr = temp;
	}

	long long int x = ModPower(Kr, MOD - 2, MOD);
	long long int y = ModPower(Knr, MOD - 2, MOD);

	long long int ans;
	ans = Kn * x;
	ans %= MOD;
	ans *= y;
	ans %= MOD;

	return ans;
}
//int型10進数をord桁のstring型2進数に変換する
string binary(int x,int ord)
{
	int y;
	string ans = "";
	for (int i = 0; i < ord; i++)
	{
		y = x / pow(2, ord - 1 - i);
		y %= 2;
		if (y == 0) ans += "0";
		else ans += "1";
	}

	return ans;
}


/*ここまでテンプレ*//*ここまでテンプレ*//*ここまでテンプレ*//*ここまでテンプレ*/

int H, W;
int field[55][55];

int d[3] = { -1,0,1 };

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

	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			int nx = x + d[i];
			int ny = y + d[j];
			if (0 <= nx && nx < H && 0 <= ny && ny < W&&field[nx][ny] == 1) dfs(nx, ny);
		}
	}
}

int main()
{
	while (true) {
		cin >> W >> H;

		if (H == 0 && W == 0) break;

		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] == 1)
				{
					dfs(i, j);
					ans++;
				}
			}
		}

		cout << ans << endl;
	}

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int m[51][51];
void del(int a,int b){
	m[a][b]=0;
	if(m[a-1][b]==1)del(a-1,b);
	if(m[a][b-1]==1)del(a,b-1);
	if(m[a+1][b]==1)del(a+1,b);
	if(m[a][b+1]==1)del(a,b+1);
	if(m[a-1][b-1]==1)del(a-1,b-1);
	if(m[a-1][b+1]==1)del(a-1,b+1);
	if(m[a+1][b-1]==1)del(a+1,b-1);
	if(m[a+1][b+1]==1)del(a+1,b+1);
}
int main(){
	int a,b,i,j,w,q;
	while(cin>>a>>b,a!=0&&b!=0){
		int c=0;
		for(i=0;i<b;i++)for(j=0;j<a;j++)cin>>m[i][j];
        for(i=0;i<b;i++)for(j=0;j<a;j++)if(m[i][j]==1){del(i,j);c++;}
        	cout<<c<<endl;
    }
}

C++: #include <bits/stdc++.h>

using namespace std;

// 入力
int H, W;

// MAP
vector<vector<int>> field;

// 左、上、右、下、左上、右上、右下、左下
vector<int> dx = {-1, 0, 1, 0, -1, 1, 1, -1};
vector<int> dy = {0, 1, 0, -1, 1, 1, -1, -1};

// 探索しない条件
// 1, 範囲外の場合
// 2, 海の場合

void dfs(int h, int w) {
    // 探索実行
    field.at(h).at(w) = 0;

    for (int i = 0; i < 8; i++) {  // 行先
        int nx = w + dx.at(i);
        int ny = h + dy.at(i);

        // 1, 範囲外の場合
        if (nx < 0 || W <= nx || ny < 0 || H <= ny) continue;

        // 2, 海の場合 + 探索済みの場合
        if (field.at(ny).at(nx) == 0) continue;

        dfs(ny, nx);
    }
}

int main() {
    while (cin >> W >> H) {
        if (H == 0 || W == 0) break;
        field.assign(H, vector<int>(W, 0));
        for (int h = 0; h < H; h++) {
            for (int w = 0; w < W; w++) {
                cin >> field.at(h).at(w);
            }
        }

        // 探索開始
        int count = 0;
        for (int h = 0; h < H; h++) {
            for (int w = 0; w < W; w++) {
                if (field.at(h).at(w) == 0) continue;
                dfs(h, w);
                count++;
            }
        }

        cout << count << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int c[51][51],w,h;
void func(int i,int j){
  c[i][j]=0;
  if(i+1<h&&c[i+1][j]==1)
    func(i+1,j);
  if(i-1>=0&&c[i-1][j]==1)
    func(i-1,j);
  if(j+1<w&&c[i][j+1]==1)
    func(i,j+1);
  if(j-1>=0&&c[i][j-1]==1)
    func(i,j-1);
  if(i+1<h&&j+1<w&&c[i+1][j+1]==1)
    func(i+1,j+1);
  if(i+1<h&&j-1>=0&&c[i+1][j-1]==1)
    func(i+1,j-1);
  if(i-1>=0&&j+1<w&&c[i-1][j+1]==1)
    func(i-1,j+1);
  if(i-1>=0&&j-1>=0&&c[i-1][j-1]==1)
    func(i-1,j-1);
}
int main(){
  while(1){
    int i,j,ans=0;
    cin>>w>>h;
    if(w==0&&h==0)
      break;
    for(i=0;i<h;i++)
      for(j=0;j<w;j++)
	cin>>c[i][j];
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
	if(c[i][j]==1){
	  func(i,j);
	  ans++;
	}
      }
    }
    cout<<ans<<endl;
  }
}