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>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;
 
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
 
const ll MOD=1e9+7;
const ll INF=1e18;
 
int dx[]={1, -1, 0, 0};
int dy[]={0, 0, 1, -1};

int mp[55][55];
int check[55][55];

int dx2[] = {-1,0,1,1,1,0,-1,-1};
int dy2[] = {1,1,1,0,-1,-1,-1,0};

int main(){

    while(true){
        int w,h;
        int ans = 0;
        cin >> w >> h;
        if(w + h == 0) break;
        for(int i = 0;i < h;i++){
            for(int j = 0;j < w;j++){
                cin >> mp[i][j];
                check[i][j] = 0;
            }
        }
        for(int i = 0;i < h;i++){
            for(int j = 0;j < w;j++){
                if(mp[i][j] == 1 && check[i][j] == 0){
                    ans++;
                    queue<P> que;
                    que.push(P(i,j));
                    while(!que.empty()){
                        P q = que.front(); que.pop();
                        if(check[q.fs][q.sc] != 0) continue;
                        check[q.fs][q.sc] = 1;
                        for(int k = 0;k < 8;k++){
                            int ni = q.fs + dx2[k];
                            int nj = q.sc + dy2[k];
                            if(min(ni,nj) >= 0 && ni < h && nj < w && mp[ni][nj] == 1 && check[ni][nj] == 0){
                                que.push(P(ni,nj));
                            }
                        }
                    }
                }
            }
        }
        cout << ans << endl;
    }

}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i=0; i<(n); ++i)
#define repr(i, a, b) for (int i=a; i<(b); ++i)
#define reprev(i, n) for (int i=n-1; i>=0; --i)
#define reprrev(i, a, b) for (int i=b-1; i>=(a); --i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

int main() {
    cout << fixed << setprecision(10);
    
    int w=1, h=1;

    while(w != 0 && h != 0) {
        int ans = 0;
        cin >> w >> h;
        if (w == 0 && h == 0) {
            break;
        }
        vector<vector<int>> m(h, vector<int>(w));
        rep(i, h) {
            rep(j, w) {
                cin >> m[i][j];
            }
        }
        stack<P> q;
        rep(i, h) {
            rep(j, w) {
                // 到達していない陸じゃなければcontinue
                if (m[i][j] != 1) continue;
                q.emplace(make_pair(i, j));
                m[i][j] = 0;
                while(!q.empty()) {
                    P pp = q.top();
                    q.pop();
                    rep(d, 8) {
                        int nh = pp.first + dh[d], nw = pp.second + dw[d];
                        if (nh >= 0 && nw >= 0 && nh < h && nw < w && m[nh][nw] == 1) {
                            q.emplace(make_pair(nh, nw));
                            m[nh][nw] = 0;
                        }
                    }
                }
                ans += 1;
            }
        }

        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#define rep(i,to) for(int i=0;i<to;i++)
using namespace std;

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

int di[] = { 0, 0, 1, 1, 1,-1,-1,-1};
int dj[] = { 1,-1, 0, 1,-1, 0, 1,-1};

void dfs(int i,int j){
	f[i][j] = 0;
	rep(k,8){
		int ni = i + di[k];
		int nj = j + dj[k];
		if(ni>=0 && ni<h && nj>=0 && nj<w && f[ni][nj]==1)
			dfs(ni,nj);
	}
}

int main(void){
	while(cin>>w>>h && (w||h)){
		rep(i,h) rep(j,w) cin>>f[i][j];

		int ans = 0;
		rep(i,h) rep(j,w) if(f[i][j]==1) { dfs(i,j); ans++;}
		cout<<ans<<endl;
	}

	return 0;
}

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

int array[1000][1000];
int X,Y;

void dfs(int y, int x){

  int nx,ny;
  array[y][x]=0;
  for(int dx=-1;dx<=1;dx++)
    for(int dy=-1;dy<=1;dy++){
      nx=x+dx;
      ny=y+dy;
      if(nx<X && ny<Y && nx>=0 && ny>=0 && array[ny][nx]!=0 ) dfs(ny,nx);
    }

}


int main(){

  while(1){
    cin>>X>>Y;
    int cnt=0;
    if(X==0 && Y==0) break;
    for(int i=0;i<Y;i++)
      for(int j=0;j<X;j++)
	cin>>array[i][j];
    
    for(int i=0;i<Y;i++)
      for(int j=0;j<X;j++)
	if(array[i][j]==1){ 
	  dfs(i,j);
	  cnt++;
	}
    cout<<cnt<<endl;
  }
  return 0;
}

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

int w, h;
int map[52][52];

int check(int i, int j)
{
	if (map[i][j] == 0)return 0;
	else
	{
		map[i][j] = 0;
		check(i-1, j - 1);
		check(i-1, j);
		check(i-1, j + 1);
		check(i, j - 1);
		check(i, j + 1);
		check(i + 1, j - 1);
		check(i + 1, j);
		check(i + 1, j + 1);
		return 0;
	}
}

int main()
{
	while (true)
	{
		map[52][52] = { 0 };
		int count = 0;
		cin >> w >> h;
		if (w == 0)return 0;

		for (int i = 1; i < h+1; i++)
		{
			for (int j = 1; j < w+1; j++)
			{
				cin >> map[i][j];
			}
		}
		for (int i = 1; i < h+1; i++)
		{
			for (int j = 1; j < w+1; j++)
			{
				if (map[i][j] == 1)
				{
					check(i, j);
					count++;
				}
			}
		}
		cout << count << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
int dx[8] = {1,0,-1,0,1,-1,-1,1};//右、上、左、下、右上、左上、左下、右下のつもり
int dy[8] = {0,1,0,-1,1,1,-1,-1};//実際は色々反転している。どうせ4か8方向調べるから関係ないとしている。
bool seen[60][60]={true};
int h,w;
void dfs(vector<vector<int> > &G,int i,int j){
    if(G[i][j]) seen[i][j] = true;
    for(int k=0;k<8;k++){
	int di = i + dx[k];
	int dj = j + dy[k];
	if(di < 0 || h <= di || dj < 0 || w <= dj)continue;
	
	if(seen[di][dj]==false) dfs(G,di,dj);
    }
}
int main(void){
    while(cin >> w >> h){
	if(w == 0 && h== 0) break;
	for(int i=0;i<h;i++)for(int j=0;j<w;j++) seen[i][j] = true;
	vector<vector<int> > G(h,vector<int>(w));
	for(int i=0;i<h;i++){
	    for(int j=0;j<w;j++){
		cin >> G[i][j];
		if(G[i][j]) seen[i][j] = false;
	    }
	}
	int t =0;
	for(int i=0;i<h;i++){
	    for(int j=0;j<w;j++){
		if(seen[i][j]) continue;
		dfs(G,i,j);
		t++;
	    }
	}
	cout << t << endl;
    }

    return 0;
}

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

int x,y;
int field[50][50];

void dfs(int w, int h){
	field[w][h]=0;
	for(int dy=-1; dy<=1; dy++){
		for(int dx=-1; dx<=1; dx++){
			int nx = w + dx;
			int ny = h + dy;
			if(nx>=0 && nx<x && ny>=0 && ny<y && field[nx][ny]==1)
				dfs(nx,ny);
		}
	}
	return;
}


int main(){
	while(1){
		int result = 0;
		cin >> x >> y;
		if(x == 0 && y == 0)
			break;
		for(int j=0; j<y; j++){
			for(int i=0; i<x; i++){
				cin >> field[i][j];
			}
		}
		for(int j=0; j<y; j++){
			for(int i=0; i<x; i++){
				if(field[i][j]==1){
					result++;
					dfs(i,j);
				}
			}
		}
		cout << result << endl;
	}
}