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;

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

int H,W;
vector<vector<int>> field;
int cnt;

bool seen[510][510];
void dfs(int h,int w){
  seen[h][w] = true;

  for(int dir=0;dir<8;dir++){
    int nh=h+dx[dir];
    int nw=w+dy[dir];

    if(nh<0 || nh>=H || nw<0 || nw>=W) continue;
    if(field[nh][nw] == 0) continue;

    if(seen[nh][nw]) continue;

    dfs(nh,nw);
  }
}

int main(){
  cin >> W >> H;
  while(H!=0 || W!=0){
    field.resize(H);
    for(int i=0;i<H;i++){
      field[i].resize(W);
    }
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
        cin >> field[i][j];
      }
    }

    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
        seen[i][j] = false;
      }
    }
    cnt = 0;
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
        if(seen[i][j]) continue;
        if(field[i][j] == 1){
          dfs(i,j);
          cnt++;
        }
      }
    }

    cout << cnt << endl;
    cin >> W >> H;
  }
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
using namespace std;

void solve(vector<vector<bool> > land)
{
	vector<vector<bool> > check(land.size(), vector<bool>(land[0].size(), false));

	int num = 0;
	for(unsigned i=1; i<land.size()-1; ++i){
		for(unsigned j=1; j<land[0].size()-1; ++j){
			if(!land[i][j] || check[i][j])
				continue;
			check[i][j] = true;
			++ num;

			queue<pair<int, int> > place;
			place.push(make_pair(i,j));
			while(!place.empty()){
				int y = place.front().first;
				int x = place.front().second;
				for(int dy=-1; dy<=1; ++dy){
					for(int dx=-1; dx<=1; ++dx){
						if(land[y+dy][x+dx] && !check[y+dy][x+dx]){
							check[y+dy][x+dx] = true;
							place.push(make_pair(y+dy, x+dx));
						}
					}
				}
				place.pop();
			}
		}
	}

	cout << num << endl;
}

int main()
{
	for(;;){
		int w, h;
		cin >> w >> h;
		if(w == 0 && h == 0)
			break;

		vector<vector<bool> > land(h+2, vector<bool>(w+2, false));
		for(int i=1; i<=h; ++i){
			for(int j=1; j<=w; ++j){
				int tmp;
				cin >> tmp;
				land[i][j] = (tmp == 1);
			}
		}

		solve(land);
	}

	return 0;
}

C++: #include <iostream>
#include <cstdio>
using namespace std;
char field[51][51];
int W, H;

void dfs(int x, int y) {
  //cout << "field[x][y] = " << field[x][y] << endl;
  field[x][y] = '0';
  
  for(int dx = -1 ; dx <= 1 ; dx++) {
    for(int dy = -1 ; dy <= 1 ; dy++) {
      int nx = x + dx, ny = y + dy;
      if(0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] == '1') dfs(nx, ny);
    }
  }
}

int main(void) {
  while(cin >> W >> H, W != 0 && H != 0) {
    int res = 0;
    for(int i = 0 ; i < H ; i++)
      for(int j = 0 ; j < W ; j++) cin >> field[i][j];
    
    for(int i = 0 ; i < H ; i++) {
      for(int j = 0 ; j < W ; j++) {
	if(field[i][j] == '1') {
	  dfs(i, j);
	  res++;
	  /*cout << endl;
	  for(int b = 0 ; b < H ; b++) {
	    for(int a = 0 ; a < W ; a++) {
	      cout << field[b][a] << ' ';
	    }
	    cout << endl;
	  }
	  cout << endl;*/
	}
      }
    }
    cout << res << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

void dfs (vector<vector<int>> &vec, vector<vector<int>> &shd, int h, int w) {
  if (h < 0 || h >= vec.size() || w < 0 || w >= vec[0].size()) return;
  else if (!vec[h][w]) return;
  else if (shd[h][w]) return;
  else shd[h][w] = 1;

  dfs(vec, shd, h + 1, w);
  dfs(vec, shd, h , w + 1);
  dfs(vec, shd, h - 1, w);
  dfs(vec, shd, h, w - 1);
  dfs(vec, shd, h + 1, w + 1);
  dfs(vec, shd, h + 1, w - 1);
  dfs(vec, shd, h - 1, w + 1);
  dfs(vec, shd, h - 1, w - 1);

  return;
}
     
int main() {
  int w, h, cou=0;
  vector<int> vec_ans(1000,2147483647);

  cin >> w >> h;

  do {
    int ans=0;
    vector<vector<int>> vec(h, vector<int>(w, 0));
    vector<vector<int>> shd(h, vector<int>(w, 0));
    rep(i, h) rep(j, w) cin >> vec[i][j];

    rep(i, h) rep(j, w) {
      if (!shd[i][j] && vec[i][j]) {
        ++ans;
        dfs(vec, shd, i, j);
      }
    }

    vec_ans[cou] = ans;
    ++cou;
    cin >> w >> h;
  } while (w || h);

  for (int i=0; vec_ans[i] != 2147483647; ++i) {
    cout << vec_ans[i] << endl;
  }
}