Go: package main

import (
	"fmt"
)

func main() {
	var h, w int
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			return
		}
		matrix := make([][]int, h)
		Sean := make([][]bool, h)
		for i := 0; i < h; i++ {
			matrix[i] = make([]int, w)
			Sean[i] = make([]bool, w)
			for j := 0; j < w; j++ {
				fmt.Scan(&matrix[i][j])
			}
		}
		var count int
		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				if matrix[i][j] == 1 && Sean[i][j] == false {
					search(i, j, h, w, matrix, Sean)
					count++
				}
			}
		}
		fmt.Println(count)
	}
}
func search(x int, y int, h int, w int, matrix [][]int, Sean [][]bool) {
	if x < 0 || y < 0 || x >= h || y >= w || matrix[x][y] == 0 {
		return
	}
	if Sean[x][y] == true {
		return
	}

	Sean[x][y] = true

	search(x+1, y, h, w, matrix, Sean)
	search(x-1, y, h, w, matrix, Sean)
	search(x, y+1, h, w, matrix, Sean)
	search(x, y-1, h, w, matrix, Sean)
	search(x+1, y-1, h, w, matrix, Sean)
	search(x-1, y+1, h, w, matrix, Sean)
	search(x+1, y+1, h, w, matrix, Sean)
	search(x-1, y-1, h, w, matrix, Sean)
}

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

const int X[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int Y[8] = {0, 1, 1, 1, 0, -1, -1, -1};

int W, H;
vector<vector<int>> Field;

void dfs(int y, int x){
    Field[y][x] = 0;
    for(int i = 0; i < 8; i++){
        int NeX = x + X[i];
        int NeY = y + Y[i];
        if(NeX < 0 || W <= NeX || NeY < 0 || H <= NeY)continue;
        if(Field[NeY][NeX] == 0)continue;
        dfs(NeY, NeX);
    }
}

int main(){
    while(cin >> W >> H){
        if(W == 0 && H == 0)break;
        Field.assign(H, vector<int>(W, 0));
        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] == 0)continue;
                dfs(i, j);
                ans++;
            }
        }
        
        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <cmath>
#include <map>
#include <queue>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int W,H;
vector<vector<int>> C;
void dfs(int v, int w) {
  C[v][w] = 0;
  for(int dh = -1; dh <= 1; dh++) {
    for(int dw = -1; dw <= 1; dw++) {
      if(v+dh < 0 || v+dh >= H || w+dw < 0 || w+dw >= W) {continue;}
      if(C[v+dh][w+dw] == 0) {continue;}
      dfs(v+dh, w+dw);
    }
  }
}


int main() {

  while(cin >> W >> H) {
    if(W == 0 && H == 0) {break;}
    C.assign(H, vector<int>(W, 0));
    rep(i,H) {
      rep(j, W) {
        //int a;
        cin >> C[i][j];
      }
    }
    int count = 0;
    rep(i,H) {
      rep(j, W) {
        if(C[i][j] == 0) {continue;}
        dfs(i,j);
        count++;
      }
    }
    cout << count << endl;
  }
}

C++: #include "bits/stdc++.h"
using namespace std;
#define rep(i,a,n) for(ll i=a;i<n;i++)
#define ALL(s) s.begin(),s.end()
#define P pair<int,int>
#define print(n) cout<<n<<endl
const int M=100010;
const int MOD=1000000007;
const int INF=1000000007;
using ll=long long;


int w,h;
char c[55][55];
int ddx[8]={-1,-1,-1,0,0,1,1,1};
int ddy[8]={-1,0,1,-1,1,-1,0,1};
int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
bool used[55][55]={};
void dfs(int W,int H){
	if(W<0||W>=w||H<0||H>=h||used[W][H])return;
	used[W][H]=true;
	if(c[W][H]=='1'){
		rep(i,0,8)dfs(W+ddx[i],H+ddy[i]);
	}
}
int main(){
	while(1){
		int land=0;
		cin>>w>>h;
		if(w==0)break;
		rep(j,0,h){
			rep(i,0,w){
				cin>>c[i][j];
				used[i][j]=false;
			}
		}
		rep(j,0,h){
			rep(i,0,w){
				if(used[i][j]==false&&c[i][j]=='1'){
					dfs(i,j);
					land++;
				}
			}
		}
		print(land);
	}
	return 0;
}

C++: #include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#include<climits>
#include<ctime>
#include<cstring>

#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl;

using namespace std;

int W,H;

bool sima[52][52];
bool checkmap[52][52];

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


int saiki(int x, int y) 
{
	if(sima[x][y]==1){sima[x][y]=0;}
	else if(sima[x][y]==0){return 0;}

	rep(i,9){
		saiki(x+dx[i], y+dy[i]);
	}

	return 1;
}

int main() {

	while(cin >> W >> H) {
		int count=0;
		if (W == 0 && H == 0) { break; }

		rep(h,H) {
			rep(w,W) {
				sima[w][h] = 0;
				checkmap[w][h] = 0;
			}
		}

		rep(h,H) {
			rep(w,W) {
				cin >> sima[w+1][h+1];
			}
		}

		REP(h,1,H+1){
			REP(w,1,W+1){
				if(sima[w][h]==1){
					saiki(w,h);
					//cout << w << ":" << h << endl; 
					count++;	
				}
			}
		}
/*
	cout << "map" << endl; 

		rep(h,51){
			rep(w,51){
				cout << sima[w][h] << " ";
			}
			cout << endl;
		}


	
	cout << "checkmap" << endl; 

		rep(h,51){
			rep(w,51){
				cout << checkmap[w][h] << " ";
			}
			cout << endl;
		}
*/

		cout << count << endl;
	}

	return 0;
}

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

vector<int> route;
#define P 50
#define Q 50
char a[P+2][Q+2];


void search(int place1,int place2){
    a[place1][place2] = '0';
    if(a[place1][place2-1] == '1'){//左
        route.push_back(1);
        search(place1,place2-1);
    }
    else if(a[place1+1][place2] == '1'){//下
        route.push_back(2);
        search(place1+1,place2);
    }
    else if(a[place1][place2+1] == '1'){//右
        route.push_back(3);
        search(place1,place2+1);
    }
    else if(a[place1-1][place2] == '1'){//上
        route.push_back(4);
        search(place1-1,place2);
    }
    else if(a[place1+1][place2-1] == '1'){//左下
        route.push_back(5);
        search(place1+1,place2-1);
    }
    else if(a[place1+1][place2+1] == '1'){//右下
        route.push_back(6);
        search(place1,place2+1);
    }
    else if(a[place1-1][place2+1] == '1'){//右上
        route.push_back(7);
        search(place1-1,place2);
    }
    else if(a[place1-1][place2-1] == '1'){//左上
        route.push_back(8);
        search(place1-1,place2);
    }
    else{
        int bk = route.back();
        route.pop_back();//戻る
        if(bk == 0) return;
        else if(bk == 1)search(place1,place2+1);
        else if(bk == 2)search(place1-1,place2);
        else if(bk == 3)search(place1,place2-1);
        else if(bk == 4)search(place1+1,place2);
        else if(bk == 5)search(place1-1,place2+1);
        else if(bk == 6)search(place1-1,place2-1);
        else if(bk == 7)search(place1+1,place2-1);
        else if(bk == 8)search(place1+1,place2+1);
    }
}

int main(void){
    int ct;
    int x,y;
    while(1){
        ct = 0;
        cin >> y >> x;
        if(x == 0 & y == 0) break;
        for(int i = 0;i < x+2;i++){
            for(int j = 0;j < y+2;j++){
                if(i == 0||j == 0||i == x+1||j == y+1)a[i][j] = '0';
                else cin >> a[i][j];
                
            }
            
        }
        for(int i = 1;i < x+1;i++){
            for(int j = 1;j < y+1;j++){
                if(a[i][j] == '1'){
                    route.push_back(0);
                    search(i,j);
                    ct++;
                }
            }
        }
        cout << ct << endl;
    }
    return 0;
}