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>
#define rep(i,n) for(int i=0; i<n; i++)
using namespace std;
int table[105][105];
int check(int i, int j){
  for(int a=i-1; a<i+2; a++){
    for(int b=j-1; b<j+2; b++){
      if(table[a][b]==1){
        table[a][b]=0;
        check(a,b);
      }
    }
  }
  return 0;
}
int main(){
  int w,h;
  while(1){
    cin >> w >> h;
    if(w==0 && h==0) break;
    table[w+2][h+2]={};
    rep(i,h) rep(j,w) cin >> table[i+1][j+1];
    rep(i,h+1){
      rep(j,w+1){
        if(table[i][j]==1){
          table[i][j]=2;
          check(i,j);
        }
      }
    }
    int count=0;
    for(int i=1; i<h+1; i++) for(int j=1; j<w+1; j++) if(table[i][j]==2) count++;
    cout << count << endl;
    rep(i,h+2){
      rep(j,w+2) cout << table[i][j] << " ";
      cout << endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <queue>
#include <map>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstdarg>

using namespace std;
#define INF 2e9 + 9
#define ALL(v) v.begin() , v.end()
#define Int int64_t
#define pll pair<Int,Int>
template<class T> using pql = priority_queue<T, vector<T>>;
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;
#define ll(n) Int n; cin >> n;
#define LL(...) Int __VA_ARGS___; cin >> __VA_ARGS___;
#define OUT(...) cout << __VA_ARGS___;
#define RP(n, i, s) for(Int i = s ;i < n ;i++)
#define REP(n, i) for (Int i = 0; i < n; i++)
#define REPe(n, i) for (Int i = 1; i <= n; i++)
#define RPe(n, i, s) for (Int i = s; i <= n; i++)
#define upd_max(maxer, updater) if (maxer < updater) maxer = updater;

struct node {
    Int to;
    Int length;
};


vector<Int> divisor(Int n) {
    vector<Int> ret;
    for (Int i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i * i != n) ret.push_back(n / i);
        }
    }
    return (ret);
}


vector<vector<int>> to;
vector<int> flag;
vector<int> c;

Int dfs(int from, int mae) {
    flag[from] = 1;
    Int mini = 0;
    for (auto next : to[from]) {
        if (next == mae) continue;
        if (flag[next] == 1) {
            return -1;
        } else {
            mini = min(mini, dfs(next, from));
        }
    }
    return mini;
}

int dx8[8] = {0, -1, -1, -1, 0, 1, 1, 1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};

vector<vector<Int>> a;
vector<vector<Int>> d;
Int w, h;

void dfs(int x, int y, int id) {
    if (d[y][x] != 0) return;
    d[y][x] = id;
    REP(8, i) {
        Int tox = x + dx8[i];
        Int toy = y + dy8[i];
        if (tox >= 0 && toy >= 0 && tox < w && toy < h && a[toy][tox] == 1) {
            a[toy][tox] = 0;
            dfs(tox, toy, id);
        }
    }
}


int main(void) {
    while (true) {
        cin >> w >> h;
        if (w == 0) break;
        d.assign(h, vector<Int>(w, 0));
        a.clear();
        REP(h, i) {
            vector<Int> z;
            REP(w, k) {
                Int tmp;
                cin >> tmp;
                z.push_back(tmp);
            }
            a.push_back(z);
        }
        Int cnt = 1;
        // a[y][x]

        REP(h, k) {
            REP(w, i) {
                if (a[k][i] == 1) dfs(i, k, cnt);
                /*for (auto z : a){
                cout << "Cnt : " << cnt << endl;
                    for (auto zz : z){
                        cout << zz << " ";
                    }
                    cout << endl;
                }*/
                cnt++;
            }
        }
        map<int, int> mp;
        REP(h, k) {
            REP(w, i) {
                if (d[k][i] != 0) mp[d[k][i]]++;
            }
        }
        cout << mp.size() << endl;
    }
}

C++: #include <iostream>

using namespace std;

int m[51][51];
int w, h;
int isl;
const int dx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};

void dfs(int x, int y)
{
    m[x][y] = -1;
    for (int i = 0; i < 8; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        
        if (nx < 0 || ny < 0 || nx >= w || ny >= h)
            continue;
        else if (m[nx][ny] == 1)
            dfs(nx, ny);
    }
}

int main()
{
    while (cin >> w >> h, w || h) {
        isl = 0;

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

        for (int i = 0; i < h; i++) 
            for (int j = 0; j < w; j++)
                if (m[i][j] == 1) {
                    dfs(i, j);
                    isl++;
                }
            
        cout << isl << endl;
    }

    return 0;
}

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

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define abs(a) max((a),-(a))
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define repe(i,n) rep(i,(n)+1)
#define per(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define pere(i,n) rep(i,(n)+1)
#define all(x) (x).begin(),(x).end()
#define SP <<" "<<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000

typedef long long LL;
typedef long double LD;

int main(){
  while(1){
    int h,w;
    cin >> w >> h;
    if(w==0) return 0;
    vector<vector<int>> field(h,vector<int>(w));
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        cin >> field[i][j];
      }
    }
    queue<pair<int,int>> bfs;
    int nx,ny,hx[8]={-1,-1,-1,0,0,1,1,1},hy[8]={-1,0,1,-1,1,-1,0,1};
    int count=0;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        if(field[i][j]==1){
          count++;
          field[i][j]=0;
          bfs.push({j,i});
          while(!bfs.empty()){
            pair<int,int> now=bfs.front();
            bfs.pop();
            for(int i=0;i<8;i++){
              nx=now.first+hx[i];
              ny=now.second+hy[i];
              if(0<=nx&&nx<w&&0<=ny&&ny<h){
                if(field[ny][nx]==1){
                  field[ny][nx]=0;
                  bfs.push({nx,ny});
                }
              }
            }
          }
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}