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>
#include <queue>
using namespace std;
int main() {
  int w, h, s, t, x, y, ans;
  queue<vector<int>> que;
  while (cin >> w >> h, w) {
    ans = 0;
    vector< vector<int> > m(h+2, vector<int>(w+2, 0));
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
	cin >> m[i+1][j+1];
      }
    }
    s = 0; t = 0;
    while (s != h+1 || t != w+1) {
      if (!m[s][t]) {
	if (t < w+1) ++t;
	else {
	  t = 0;
	  ++s;
	}
      } else {
	que.push({s, t});
	m[s][t] = 0;
	while (!que.empty()) {
	  x = que.front()[0];
	  y = que.front()[1];
	  que.pop();
	  if (m[x-1][y]) {
	    m[x-1][y] = 0;
	    que.push({x-1, y});
	  }
	  if (m[x+1][y]) {
	    m[x+1][y] = 0;
	    que.push({x+1, y});
	  }
	  if (m[x][y-1]) {
	    m[x][y-1] = 0;
	    que.push({x, y-1});
	  }
	  if (m[x][y+1]) {
	    m[x][y+1] = 0;
	    que.push({x, y+1});
	  }
	  if (m[x+1][y+1]) {
	    m[x+1][y+1] = 0;
	    que.push({x+1, y+1});
	  }
	  if (m[x+1][y-1]) {
	    m[x+1][y-1] = 0;
	    que.push({x+1, y-1});
	  }
	  if (m[x-1][y+1]) {
	    m[x-1][y+1] = 0;
	    que.push({x-1, y+1});
	  }
	  if (m[x-1][y-1]) {
	    m[x-1][y-1] = 0;
	    que.push({x-1, y-1});
	  }
	}
	++ans;
      }
    }
    cout << ans << endl;
    m.erase(m.begin(), m.end());
  }
  return 0;
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <queue>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <complex>
#include <fstream>
#include <stdio.h>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VEC;
typedef vector<VEC> MAT;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef vector<LL> VLL;

//container util

//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define MT(a,b,c) MP(a, MP(b, c))
#define T1 first
#define T2 second.first
#define T3 second.second


//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define EPS 1e-8

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

int m[55][55];
int main(){
	while(1){
		int h,w,ret=0;
		cin>>w>>h;
		if(w==0)return 0;
		memset(m, 0, sizeof(m));
		REP(i,w+2){
			m[0][i]=0;
			m[h+1][i]=0;
		}
		REP(i,h+2){
			m[i][0]=0;
			m[i][w+1]=0;
		}
		REP(i,h)REP(j,w)cin>>m[i+1][j+1];
		priority_queue<PII> q;
		FOR(i,1,h+1){
			FOR(j,1,w+1){
				if(m[i][j]==1){
					ret++;
					q.push(MP(i,j));
					while(!q.empty()){
						PII tmp = q.top();
						q.pop();
						if(m[tmp.first][tmp.second]==0)continue;
						m[tmp.first][tmp.second] = 0;
						REP(i,8)q.push(MP(tmp.first+dy[i], tmp.second+dx[i]));
					}
				}
			}
		}
		cout << ret << endl;
	}
}

C++: #include<iostream>
#include<vector>
#include <unordered_map>  
#include <algorithm>
#include <functional>
#include <map>
#include <string>
#include <sstream>
#include <cmath>
#include <float.h>
using namespace std;

bool checked[50][50];
int island[50][50];
int count1 = 0;
int w, h;

void checkIsland(int x, int y) {
	if (x < 0 || x >= w || y >= h || y < 0) { return; }
	if (!island[x][y]) return;
	checked[x][y] = true;
	checkIsland(x + 1, y);
	checkIsland(x + 1, y + 1);
	checkIsland(x + 1, y - 1);
	checkIsland(x - 1, y + 1);
	checkIsland(x, y + 1);
}

int main() {
	cin >> w >> h;
	for (int i = 0; i < w; ++i) {
		for (int j = 0; j < h; ++j) {
			checked[i][j] = false;
			cin >> island[i][j];
		}
	}
	for (int i = 0; i < w; ++i) {
		for (int j = 0; j < h; ++j) {
			if (checked[i][j]) continue;
			if (island[i][j]) {
				count1++;
				checkIsland(i, j);
			}
			else {
				checked[i][j] = true;
			}
		}
	}
	cout << count1 << endl;
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int field[64][64];
int cnt;
int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int w, h;

void dfs(int x, int y);

int main()
{
	int x, y;

	while (true){
		cnt = 0;

		scanf("%d %d", &w, &h);
		if (w == 0){
			break;
		}

		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				scanf("%d", &field[i][j]);
			}
		}

		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (field[i][j]){
					cnt++;
					dfs(j, i);
				}
			}
		}

		printf("%d\n", cnt);
	}

	return (0);
}

void dfs(int x, int y)
{
	field[y][x] = 0;

	for (int i = 0; i < 8; i++){
		int nx = x + dx[i];
		int ny = y + dy[i];

		if (field[ny][nx] && nx >= 0 && ny >= 0 && nx < w && ny < h){
			dfs(nx, ny);
		}
	}
}