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 <iostream>
#include <vector>
#include <map>
#include <set>
using namespace std;

void add_label(map<int,int> &tbl, int a, int b){
	if( a == b ) return;
	if(a < b){
		int tmp = a;
		a = b;
		b = tmp;
	}
	if( tbl.find(a) == tbl.end() ) {
		tbl[a] = b;
	} else {
		int bb = tbl.find(a)->second;
		if( b == bb ) return;
		if( bb > b ) {
			tbl[a] = b;
			tbl[bb] = b;
		} else {
			tbl[b] = bb;
		}
	}
}

int main(void){
	int w, h;
	while( cin>>w>>h, w||h ) {
		vector<vector<int> > field( h );
		vector<vector<int> > labeled( h );
		for( int i=0; i<h; ++i ) {
			field[i] = vector<int>( w );
			labeled[i] = vector<int>( w );
			for( int j=0; j<w; ++j ) {
				cin>>field[i][j];
				labeled[i][j] = 0;
			}
		}
		int label = 0;
		map<int, int> tbl;
		for( int i=0; i<h; ++i ) {
			for( int j=0; j<w; ++j ) {
				if( field[i][j] ){
					bool f = false;
					if( i ) {
						if( j && labeled[i-1][j-1] ) {
							if(!f) labeled[i][j] = labeled[i-1][j-1];
							f = true;
						}
						if( labeled[i-1][j] ) {
							if(!f) labeled[i][j] = labeled[i-1][j];
							else add_label( tbl, labeled[i][j], labeled[i-1][j] );
							f = true;
						}
						if( j + 1 < w && labeled[i-1][j+1] ) {
							if(!f) labeled[i][j] = labeled[i-1][j+1];
							else add_label( tbl, labeled[i][j], labeled[i-1][j+1] );
							f = true;
						}
						if( j && labeled[i][j-1] ) {
							if(!f) labeled[i][j] = labeled[i][j-1];
							else add_label( tbl, labeled[i][j], labeled[i][j-1] );
							f = true;
						}
					} else {
						if( j && labeled[i][j-1] ){
							if(!f) labeled[i][j] = labeled[i][j-1];
							else add_label( tbl, labeled[i][j], labeled[i][j-1] );
							f = true;
						}
					}
					if(!f) labeled[i][j] = ++label;
				}
			}
		}
		for( auto x : tbl ) {
			int a = x.first;
			int b = x.second;
			while( tbl.find(b) != tbl.end() ) {
				b = tbl.find(b)->second;
			}
			for( int i=0; i<h; ++i ) {
				for( int j=0; j<w; ++j ) {
					if( labeled[i][j] == a ) labeled[i][j] = b;
				}
			}
		}
		
		/*
		for( int i=0; i<h; ++i ) {
			cerr<<"!";
			for( int j=0; j<w; ++j ) {
				cerr<<labeled[i][j];
			}
			cerr<<endl;
		}*/
		
		set<int> nums;
		nums.insert(0);
		for( int i=0; i<h; ++i ) {
			for( int j=0; j<w; ++j ) {
				nums.insert( labeled[i][j] );
			}
		}
		cout<<nums.size()-1<<endl;
	}
	return 0;
}

C++: #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>

using namespace std;
typedef long long ll;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define DEC(i, s) for (int i = (s); i >= 0; i--)

#define SIZE(v) (int)((v).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))
#define FIND(m, w) ((m).find(w) != (m).end())

int w, h;
int field[100][100];
bool visit[100][100];
const int dx[8] = { 1, 1, 1, 0, -1, -1, -1, 0 };
const int dy[8] = { 1, 0, -1, -1, -1, 0, 1, 1 };

void dfs(int x, int y) {
  if (visit[y][x]) { return; }
  visit[y][x] = true;
  REP(i, 8) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx < 0 || nx >= w || ny < 0 || ny >= h) { continue; }
    if (field[ny][nx] == 0) { continue; }
    dfs(nx, ny);
  }
}

int main() {
  while (scanf("%d %d", &w, &h), w|h) {
    MEMSET(field, 0);
    MEMSET(visit, false);
    REP(y, h) REP(x, w) {
      scanf("%d", &field[y][x]);
    }
    int ans = 0;
    REP(y, h) REP(x, w) {
      if (visit[y][x] || field[y][x] == 0) { continue; }
      ans++;
      dfs(x, y);
    }
    printf("%d\n", ans);
  }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <functional>
#include <set>
#include <sstream>
#include <map>
#include <queue>

using namespace std;
 
const double eps=1e-10;

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

int main()
{

	int w,h;
	while(cin>>w>>h,w){
		vector< vector<int> > table(h,vector<int>(w));
		int res=0;
		for(int y=0;y<h;y++){
			for(int x=0;x<w;x++) cin>>table[y][x];
		}

		queue< pair<int,int> > q;

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

				if(table[i][j]==0) continue;

				res++;
				q.push(make_pair(i,j));

				while(!q.empty()){
					pair<int,int> p=q.front();
					q.pop();
					int y=p.first,x=p.second;

					table[y][x]=0;
					for(int k=0;k<8;k++){
						int nx=x+dx[k],ny=y+dy[k];
						if(0<=nx && nx<w && 0<=ny && ny<h && table[ny][nx]==1) q.push(make_pair(ny,nx));
					}
				}
			}
		}
		cout<<res<<endl;

	}


	return 0;
}

C++: #include<bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n ; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define whole(f, x, ...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x) // decltypeで型取得、引数があればva_argsのところに入れる
using namespace std;
typedef long long ll; // long longをllでかけるようにした
const int INF = 1e9;
int n, m;
char W[100][100];

void dfs(int y, int x){
    W[y][x] = '0';
    for(int dx = -1; dx <= 1; dx++){
        for(int dy = -1; dy <= 1; dy++){
            int nx = dx + x;
            int ny = dy + y;
            if(W[ny][nx] == '1' and 0 <= nx and nx < n and 0 <= ny and ny < m) dfs(ny, nx);
        }
    }
    return ;
}

int main(void){
    while(true){
        cin >> n >> m;
        if(n == 0 and m == 0) return 0;
        REP(i, m){
            REP(j, n){
                cin >> W[i][j];
            }
        }

        int ans = 0;
        REP(i, m){
            REP(j, n){
                if(W[i][j] == '1'){
                    dfs(i, j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
typedef pair<int,int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int w,h;
int nx,ny;
void dfs(vector<vector<int>> &G,int a,int b){
    G.at(a).at(b)=0;
    
    for(int k=-1;k<=1;k++){
        for(int s=-1;s<=1;s++){
            nx=a+k;
            ny=b+s;
        if(0<=nx&& nx<h && 0<=ny&& ny<w && G.at(nx).at(ny)==1) dfs(G,nx,ny);
        }
    }
    return ;
}



int main(){
    int w,h;
    cin>>w>>h;
    vector<vector<int>>G(h,vector<int>(w));
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>G.at(i).at(j);
        }
    }
    int count=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(G.at(i).at(j)==1){
                dfs(G,i,j);
                count++;
            }
        }
    }
cout<<count<<endl;
}