Go: // AOJ 1160: How Many Islands? http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160

package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"strconv"
)

var SEA = 0
var LAND = 1

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func discoverIslands(M, V [][]int, H, W int) int {
	// 島を探す
	queue := list.New()
	islands := 0
	for i := 1; i <= H; i++ {
		for j := 1; j <= W; j++ {
			// 訪問済みの土地ならスキップ
			if V[i][j] == 1 {
				continue
			}

			// 訪問中の土地が海なら訪問済みにしてスキップ
			if M[i][j] == SEA {
				V[i][j] = 1
				continue
			}

			// 未訪問の陸地が見つかったので
			// 島の数を1つ増やし、この陸地から辿れる陸地を訪問していく
			islands++
			queue.PushBack([]int{i, j})

			for queue.Len() > 0 {
				cur := queue.Remove(queue.Front()).([]int)
				h, w := cur[0], cur[1]
				if V[h][w] == 1 {
					continue
				}
				V[h][w] = 1

				// 全方向に未訪問の陸地がないか探索
				for v := -1; v <= 1; v++ {
					for l := -1; l <= 1; l++ {
						dh := h + v
						dw := w + l
						if M[dh][dw] == LAND && V[dh][dw] == 0 {
							queue.PushBack([]int{dh, dw})
						}
					}
				}
				/*
					if M[h-1][w] == LAND {
						queue.PushBack([]int{h - 1, w})
					}
					if M[h-1][w+1] == LAND {
						queue.PushBack([]int{h - 1, w + 1})
					}
					if M[h][w+1] == LAND {
						queue.PushBack([]int{h, w + 1})
					}
					if M[h+1][w+1] == LAND {
						queue.PushBack([]int{h + 1, w + 1})
					}
					if M[h+1][w] == LAND {
						queue.PushBack([]int{h + 1, w})
					}
					if M[h+1][w-1] == LAND {
						queue.PushBack([]int{h + 1, w - 1})
					}
					if M[h][w-1] == LAND {
						queue.PushBack([]int{h, w - 1})
					}
					if M[h-1][w-1] == LAND {
						queue.PushBack([]int{h - 1, w - 1})
					}
				*/
			}
		}
	}
	return islands
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	for {
		W, H := nextInt(sc), nextInt(sc)

		if W == 0 && H == 0 {
			break
		}
		// Initialize map and visted list
		M := make([][]int, H+2)
		V := make([][]int, H+2)
		for i := 0; i < H+2; i++ {
			M[i] = make([]int, W+2)
			V[i] = make([]int, W+2)
		}
		for i := 1; i <= H; i++ {
			for j := 1; j <= W; j++ {
				M[i][j] = nextInt(sc)
			}
		}

		islands := discoverIslands(M, V, H, W)

		fmt.Println(islands)
	}
}

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

bool f[51][51];
int w, h;
int dx[8] = { 0, 0, -1,  1, -1,  1, -1,  1};
int dy[8] = {-1, 1,  0,  0, -1, -1,  1,  1};

void dfs(int x, int y) {
	if(!f[y][x]) return;
	f[y][x] = 0;
	for (int i = 0; i < 8; i++) {
		if (x + dx[i] < w && y + dy[i] < h
			 && x + dx[i] >= 0 && y + y + dy[i] >= 0)
			dfs(x + dx[i], y + dy[i]);
	}
}

int main()
{	
	int n;
	
	while (cin >> w >> h, w) {
		n = 0;
		
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> f[i][j];
			}
		}
		
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (f[i][j]) {
					n++;
					dfs(j, i);
				}
			}
		}
		
		cout << n << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (ll i = 0; i < n; ++i)
#define sz(x) int(x.size())
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define FORV(i, v) for (auto i = v.begin(); i != v.end(); i++)
using namespace std;

using ll = long long;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
const int INF = 1001001001;
const long long INFLL = LLONG_MAX / 3;
const double eps = (1e-9);

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

void dfs(const vector<vector<int>>& c, vector<vector<bool>>& visited, int h, int w) {
  visited[h][w] = true;
  rep(i, 8) {
    int nh = h + dh[i];
    int nw = w + dw[i];
    if (nh < 0 || nh >= c.size() || nw < 0 || nw >= c[0].size()) continue;
    if (visited[nh][nw]) continue;
    if (c[nh][nw] == 0) continue;
    dfs(c, visited, nh, nw);
  }
}

bool solve() {
  int w, h; cin >> w >> h;
  if (w == 0 && h == 0) return false;
  vector<vector<int>> c(h, vector<int>(w));
  rep(i, h) rep(j, w) cin >> c[i][j];
  vector<vector<bool>> visited(h, vector<bool>(w, false));

  int ans = 0;
  rep(i, h) rep(j, w) {
    if (visited[i][j]) continue;
    if (c[i][j] == 0) continue;
    dfs(c, visited, i, j);
    ans++;
  }
  cout << ans << endl;
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (solve()) {}

  return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
int dp[50][50];
int main(){
	int w,h;
	while(true){
	cin>>w>>h;
	if(w==0&&h==0)
		break;
	for(int i=0;i<h;i++)
		for(int j=0;j<w;j++)
			cin>>dp[i][j];
	int count=0;
	while(true){
		bool t=true;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(dp[i][j]!=0)
					t=false;
			}
		}
		if(t==true)
			break;
		int x=0,y=0;
		while(true){
			x=0;
			while(true){
				if(dp[y][x]==1){
					t=true;
					break;
				}
				x++;
				if(x==w)
					break;
			}
			if(t==true)
				break;
			y++;
		}
		for(int f=max(0,y-1);f<=min(h-1,y+1);f++){
			for(int g=max(0,x-1);g<=min(w-1,x+1);g++){
				if(dp[f][g]==1)
				    dp[f][g]=2;
			}
		}
		dp[y][x]=0;
		while(true){
			t=true;
			for(int i=0;i<h;i++){
				for(int j=0;j<w;j++){
					if(dp[i][j]==2){
						for(int f=max(0,i-1);f<=min(h-1,i+1);f++){
			                for(int g=max(0,j-1);g<=min(w-1,j+1);g++){
								if(dp[f][g]==1)
				                    dp[f][g]=2;
			                }
						}
						dp[i][j]=0;
						t=false;
					}
				}
			}
			if(t==true)
				break;
		}
		count++;
	}
	cout<<count<<endl;
	}
	return 0;
}

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

int MAP(vector<vector<int>> &m, int i, int j){
	int w = m[0].size();
	int h = m.size();

	if(!(0 <= i && i < h && 0 <= j && j < w)){
		return 0;
	}

	if(m[i][j] == 0){
		return 0;
	}
	else if(m[i][j] = 1){
		m[i][j] = 0;
		MAP(m, i - 1, j - 1);
		MAP(m, i, j - 1);
		MAP(m, i + 1, j - 1);
		MAP(m, i - 1, j);
		MAP(m, i + 1, j);
		MAP(m, i - 1, j + 1);
		MAP(m, i, j + 1);
		MAP(m, i + 1, j + 1);
	}
	return 1;
}

int main(){
	int w, h;

	while(true){
		int count = 0;
		cin >> w >> h;

		if(w == 0 && h == 0){
			break;
		}

		vector<vector<int> > map(h, vector<int>(w, 0));

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

		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				count += MAP(map, i , j);
			}
		}

		cout << count << endl;
	}
}