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 <string>
#include <algorithm>
using namespace std;
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REP(i,b) FOR(i,0,b)
#define INF 1e9

int w,h,c[50][50],i_num;
int dx[] = {1,0,-1}, dy[] = {1,0,-1};

void search(int x, int y){
  if(c[y][x] == 1 && x >=0 && x < w && y >=0 && y < h){
    c[y][x] = i_num;
    REP(i,3) REP(j,3){
      if(i != 0 || j != 0 || x-1 >= 0 || x+1 < w || y-1 >= 0 || y+1 < h)
      search(x+dx[i],y+dy[j]);
    }
  }
}

void solve(int X, int Y){
  i_num = 2;
  REP(i,20) REP(j,20) c[i][j] = 0;
  //0:ocean, 1:land, 2~:island_number

  REP(i,Y){
    REP(j,X){
      cin>>c[i][j];
    }
  }

  REP(i,Y){
    REP(j,X){
      if(c[i][j] == 1){
        search(j,i);
        i_num++;
      }
    }
  }

  cout<<i_num-2<<endl;
  /*REP(i,20){
     REP(j,20) cout<<c[i][j];
     cout<<endl;
  }*/
}

int main(){

  while(1) {
    cin>>w>>h;
    if(w == 0 && h == 0) break;
    solve(w,h);
  }

  return 0;
}

C++: #include <iostream>
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <utility>
#include <algorithm>
#include <functional>
#include <vector>
#include <numeric>
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vb = vector<bool>;
using Graph = vector<vector<int>>;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define REP(i,n) for(int i=0;i<n;i++)

vb seen;

void dfs(const Graph &G, int v) {
    seen[v] = true;
    for(auto next_v : G[v]) {
        if (seen[next_v]) continue;
        dfs(G, next_v);
    }
}

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

    int w, h;
    while(cin >> w >> h, w*h) {
        int map[h+1][w+1];
        REP(j, w+1) {
            map[0][j] = 0;
        }
        REP(i, h) {
            map[i+1][0] = 0;
            REP(j, w) {
                cin >> map[i+1][j+1];
            }
        }
        
        Graph G(w*h);
        FOR(i, 1, h+1) {
            FOR(j, 1, w+1) {
                if (map[i][j] == 1) {
                    int p1 = (i-1)*w+(j-1);
                    int p2;
                    if (map[i][j-1] == 1) {
                        p2 = (i-1)*w+(j-2);
                        G[p1].push_back(p2);
                        G[p2].push_back(p1);
                    }
                    if (map[i-1][j-1] == 1) {
                        p2 = (i-2)*w+(j-2);
                        G[p1].push_back(p2);
                        G[p2].push_back(p1);
                    }
                    if (map[i-1][j] == 1) {
                        p2 = (i-2)*w+(j-1);
                        G[p1].push_back(p2);
                        G[p2].push_back(p1);
                    }
                    if (map[i-1][j+1] == 1) {
                        p2 = (i-2)*w+j;
                        G[p1].push_back(p2);
                        G[p2].push_back(p1);
                    }
                }
            }
        }

        seen.assign(w*h, false);
        int cnt = 0;
        REP(i, w*h) {
            if (seen[i]) continue;
            if (map[i/w+1][i%w+1] == 0) {
                seen[i] = true;
                continue;
            }
            cnt++;
            dfs(G, i);
        }
        cout << cnt << "\n";
    }
}

C++: #include<iostream>
#include<queue>

typedef std::pair<int, int> P;
int vx[] = { 0, 1, 0, -1, 1, 1, -1, -1 }, vy[] = { 1, 0, -1, 0, 1, -1, 1, -1 };
int w, h;
int map[51][51];

void bfs(int x, int y){

	map[y][x] = 0;

	std::queue<P>que;
	que.push(P(x, y));

	while (!que.empty()){

		P p = que.front();
		que.pop();

		for (int i = 0; i < 8; i++){
			int nx = p.first + vx[i], ny = p.second + vy[i];

			if (0 <= nx&&nx < w && 0 <= ny&&ny < h&&map[ny][nx] != 0){
				map[ny][nx] = 0;
				que.push(P(nx, ny));
			}
		}
	}

	return;
}

int main(){

	while (std::cin >> w >> h, w || h){

		int cnt = 0;

		memset(map, 0, sizeof(map));

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

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

		std::cout << cnt << std::endl;
	}

	return 0;
}

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

int H, W;
int field[51][51];

//動的計画法
void dfs(int h, int w){
    field[h][w] = 0;

    //八方向を１つずつ探索
    for(int dh = -1; dh <= 1; dh++){
        for(int dw = -1; dw <= 1; dw++){
            int nh = h + dh;
            int nw = w + dw;

            //場外アウトまたは０ならば、それ以降（再帰的に探索する部分）は実行せずループの最初に戻る
            if(nh < 0 || nh > H || nw < 0 || nw > W || field[nh][nw] == 0) continue;

            //再帰的に探索（探索しているマスを基準にしてその八方向にあるマスを探索していく）
            dfs(nh, nw);
        }
    }
}

int main(void){
    while(true){
        cin >> W >> H;
        if(W + H == 0) break;

        for(int i = 0; i < H; i++){
            for(int j = 0; j < W; j++){
                cin >> field[i][j];
            }
        }

        //探索開始
        int c = 0; //島の個数
        for(int i = 0; i < H; i++){
            for(int j = 0; j < W; j++){
                if(field[i][j] == 0) continue; //島の一部でないならば、それ以降スルー
                dfs(i, j); //そのマスが陸であるならば、その八方向を動的計画法で探索していく
                c++;
            }
        }
        cout << c << endl;
    }
}