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;

struct UnionFind{
    vector<int> data;

    UnionFind(int N) : data(vector<int>(N,-1)){}

    int root(int x){
        if(data[x] < 0){
            return x;
        }else{
            return data[x] = root(data[x]);
        }
    }

    bool same(int x,int y){
        return root(x) == root(y);
    }

    void unite(int x,int y){
        if(!same(x,y)){
            x = root(x);
            y = root(y);
            if(data[x] < data[y]){
                data[x] += data[y];
                data[y] = x;
            }else{
                data[y] += data[x];
                data[x] = y;
            }
        }
        return;
    }

};

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

void solve(int w,int h){
    swap(w,h);
    int c[w][h];
    for(int i = 0;i < w;i++){
        for(int j = 0;j < h;j++){
            cin >> c[i][j];
        }
    }
    UnionFind uf(w*h);
    for(int i = 0;i < w;i++){
        for(int j = 0;j < h;j++){
            for(int k = 0;k < 8;k++){
                int x = i + dx[k];
                int y = j + dy[k];
                if(0 <= x && x < w && 0 <= y && y < h){
                    if(c[i][j] && c[x][y]){
                        uf.unite(i*h+j,x*h+y);
                    }
                }
            }
        }
    }
    set<int> s;
    for(int i = 0;i < w*h;i++)s.insert(uf.root(i));
    int ans = 0;
    for(auto x:s){
        ans += c[x/h][x%h];
    }
    cout << ans << endl;
}

int main(){
    int w,h;
    while(true){
        cin >> w >> h;
        if(!w && !h)return 0;
        solve(w,h);
    }
}

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

struct mapst {
	int x, y;
};

#define func(x,y) (c[y][x]&&!f[y][x])

int main() {
	int w, h, ans=0;
	int c[52][52]={{0}}, f[52][52]={{1}};
	int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
	int dy[8] = {0, -1, -1, -1, 0, 1, 1, 1};
	queue<mapst> q;
	while(1) {
		cin >> w >> h;
		if(!w&&!h) break;
		ans = 0;
		for(int i=0; i<52; ++i) for(int j=0; j<52; ++j) {
			c[i][j] = 0;
			f[i][j] = 1;
		}
		for(int i=1; i<=h; ++i) for(int j=1; j<=w; ++j) {
			cin >> c[i][j];
			f[i][j] = 1-c[i][j];
		}
		for(int i=1; i<=h; ++i) for(int j=1; j<=w; ++j) {
			if(func(j,i)) {
				mapst m, ms;
				m.x = j; m.y = i;
				q.push(m);
				ans++;
				do {
					m = q.front();
					q.pop();
					f[m.y][m.x] = 1;
					for(int k=0; k<8; k++) {
						if(func(m.x+dx[k],m.y+dy[k])) {
							ms.x = m.x+dx[k]; ms.y = m.y+dy[k];
							q.push(ms);
						}
					}
				}while(!q.empty());
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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

using namespace std;
using ll = long long;
using ld = long double;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define reps(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
	int w, h;
	int c[50][50] = {};

	void dfs(int i, int j) {
		c[i][j] = 0;

		for (int di = -1; di <= 1;di++) {
			for (int dj = -1; dj <= 1;dj++) {
				int ni = i + di;
				int nj = j + dj;

				if (0 <= ni&&ni < h && 0 <= nj&&nj < w && c[ni][nj] == 1) {
					dfs(ni, nj); 
				}
			}
		}
		return;
}

int main() {
	cin.sync_with_stdio(false);
	vector<int>ans;
	while (1) {
		cin >> w >> h;
		rep(i, h) {
			rep(j, w) {
				cin >> c[i][j];
			}
		}
		int res = 0;
		rep(i, h) {
			rep(j, w) {
				if (c[i][j] == 1) {
					dfs(i, j);
					res++;
				}
			}
		}
		if (w == 0&&h == 0) {
			break;
		}
		ans.push_back(res);


	}
	rep(i, ans.size()) {
		cout << ans[i] << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <string>
#include <sstream>

using namespace std;

void dfs(int i, int j, int** c,bool **used, int h, int w){
		if(used[i][j])return;
		used[i][j] = true;
		for(int x = -1; x < 2; x++){
				for(int y = -1; y < 2; y++){
						int dx = i + x;
						int dy = j + y;
						if(dx > -1 && dx < h && dy > -1 && dy < w){
								if(!used[dx][dy] && c[dx][dy] == 1){
										dfs(dx, dy, c, used, h, w);
								}
						}
				}
		}
}

int main(int argc, char const* argv[])
{
	int w, h;
	cin >> w >> h;
	while(w != 0 || h != 0){
			int **c = new int*[h];
			bool **used = new bool*[h];
			for(int i = 0; i < h; i++){
					c[i] = new int[w];
					used[i] = new bool[w];
					for(int j = 0; j < w; j++){
							cin >> c[i][j];
							used[i][j] = false;
					}
			}
			int res = 0;
			for(int i = 0; i < h; i++){
					for(int j = 0; j < w; j++){
							if(used[i][j])continue;
							if(c[i][j] == 0)continue;
							dfs(i, j, c, used, h, w);
							res++;
					}
			}
			cout << res << endl;
			cin >> w >> h;
	}
	return 0;
}

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

bool MAP[50][50];
int X, Y;
const int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1},
          dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};

void init() {
  for (int i = 0; i < 50; i++)
    for (int j = 0; j < 50; j++)
      MAP[i][j] = false;
}

void sink_one_island(int x, int y) {
  if (x < 0 or X < x or y < 0 or Y < y or !MAP[x][y])
    return;

  MAP[x][y] = false;
  for (int i = 0; i < 8; i++)
    sink_one_island(x + dx[i], y + dy[i]);
}

int sink_all_islands() {
  int islands = 0;

  for (int x = 0; x < X; x++) {
    for (int y = 0; y < Y; y++) {
      if (MAP[x][y]) {
        sink_one_island(x, y);
        islands++;
      }
    }
  }

  return islands;
}

int main() {
  while (cin >> X >> Y and (X != 0 or Y != 0)) {
    int s;

    for (int y = 0; y < Y; y++) {
      for (int x = 0; x < X; x++) {
        cin >> s;
        MAP[x][y] = (s == 1)? true:false;
      }
    }

    int ids = sink_all_islands();

    cout << ids << endl;
  }
}