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 <bits/stdc++.h>

using namespace std;

int w, h;

vector<vector<int>> makearea(void) {
	vector<vector<int>> area(h);

	for(int i = 0; i < h; i++) {
		for(int j = 0; j < w; j++) {
			int tmp;
			cin >> tmp;
			area[i].push_back(tmp);
		}
	}
	
	/*for(auto i : area) {
		for(auto j : i) {
			cout << j << " ";
		}
		cout << endl;
	}*/

	return area;
}

void dfs(int y, int x, vector<vector<int>>& area, vector<vector<bool>>& checker) {
	if(x != w - 1) {
		if(area[y][x + 1] == 1 and checker[y][x + 1] == true) {
			checker[y][x + 1] = false;
			dfs(y, x + 1, area, checker);
		}
	}
	if(x != w - 1 and y != h - 1){
		if(area[y + 1][x + 1] == 1 and checker[y + 1][x + 1]) {
			checker[y + 1][x + 1] = false;
			dfs(y + 1, x + 1, area, checker);
		}
	}
	if(y != h - 1){
		if(area[y + 1][x] == 1 and checker[y + 1][x]) {
			checker[y + 1][x] = false;
			dfs(y + 1, x, area, checker);
		}
	}
	if(x != 0 and y != h - 1){
		if(area[y + 1][x - 1] == 1 and checker[y + 1][x - 1]) {
			checker[y + 1][x - 1] = false;
			dfs(y + 1, x - 1, area, checker);
		}
	}
	if(x != 0){
		if(area[y][x - 1] == 1 and checker[y][x - 1]) {
			checker[y][x - 1] = false;
			dfs(y, x - 1, area, checker);
		}
	}
	if(x != 0 and y != 0){
		if(area[y - 1][x - 1] == 1 and checker[y - 1][x - 1]) {
			checker[y - 1][x - 1] = false;
			dfs(y - 1, x - 1, area, checker);
		}
	}
	if(y != 0){
		if(area[y - 1][x] == 1 and checker[y - 1][x]) {
			checker[y - 1][x] = false;
			dfs(y - 1, x, area, checker);
		}
	}
	if(x != w - 1 and y != 0){
		if(area[y - 1][x + 1] == 1 and checker[y - 1][x + 1]) {
			checker[y - 1][x + 1] = false;
			dfs(y - 1, x + 1, area, checker);
		}
	}
}

int search(vector<vector<int>>& area) {
	int sum = 0;
	vector<vector<bool>> checker;
	checker = vector<vector<bool>>(area.size(), vector<bool>(area[0].size(), true));

	for(int i = 0; i < h; i++) {
		for(int j = 0; j < w; j ++) {
			if(area[i][j] == 1 and checker[i][j]) {
				checker[i][j] = false;
				dfs(i, j, area, checker);
				sum++;
			}
		}
	}
	return sum;
}

int main(void) {
	while(1) { 
		cin >> w >> h;
		if(w == 0 and h == 0) {
			break;
		}

		vector<vector<int>> area = makearea();
		int sum = search(area);

		cout << sum << endl;
	}
	return 0;
}

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

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

int main(void) {
	int w,h;
	while(cin >> w >> h && w && h) {
		vector< vector<int> > data(w+2,vector<int>(h+2,0));
		for (int i = 1; i <= h; ++i) {
			for (int j = 1; j <= w; ++j) {
				cin >> data[i][j];
			}
		}

		int nx,ny;
		int count = 0;
		queue<int> fx,fy;
		for (int i = 1; i <= h; ++i) {
			for (int j = 1; j <= w; ++j) {
				if (data[i][j] == 1) {
					nx = j;
					ny = i;
					data[i][j] = 0;
					fx.push(nx);
					fy.push(ny);
					++count;

					while (!fx.empty()) {
						nx = fx.front();
						ny = fy.front();
						fx.pop();
						fy.pop();
						for (int d = 0; d < 8; ++d) {
							if (data[ny+dy[d]][nx+dx[d]] == 1) {
								fx.push(nx+dx[d]);
								fy.push(ny+dy[d]);
								data[ny+dy[d]][nx+dx[d]] = 0;
							}
						}
					}
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}

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

#define PIAR pair<int, int>

struct pos{
    int x;
    int y;
};

int main(){
    int w, h;
    while(cin >> w >> h, w != 0 && h != 0){
        int field[52][52] = {{0}};
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= w; j++){
                cin >> field[i][j];
            }
        }

        stack<PIAR> st;
        int island = 0;
        int dx[] = {-1,  0, 1, 0, -1,  1, 1, -1};
        int dy[] = {0 , -1, 0, 1, -1, -1, 1,  1};
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= w; j++){
                //島を探す
                if(field[i][j] == 1){
                    st.push(PIAR(i, j));
                    field[i][j] = 0;
                    while(!st.empty()){
                    	int x = st.top().first;
                    	int y = st.top().second;
                        st.pop();
                        for(int k = 0; k < 8; k++){
                            int nextX = x + dx[k];
                            int nextY = y + dy[k];
                            if(field[nextX][nextY] == 1){
                                st.push(PIAR(nextX, nextY));
                                field[nextX][nextY] = 0;
                            }
                        }
                    }
                    island++;
                }

            }
        }

        cout << island << endl;
    }
}

C++: #include <bits/stdc++.h>
#define rep(i,s,n) for(int i=s;i<n;i++)
#define pb push_back
#define all(x) (x).begin(),(x).end()
using namespace std;
using P=pair<int,int>;
using G=vector<vector<int>>;
using ll=long long;

string subsum(int* A,int n,int m){
  bitset<2010> E(1);

  rep(i,0,n){
    E |= (E<<A[i]);
  }

  if(E.test(m)) return "yes";
  else return "no";
}

vector<bool> seen;
vector<int> d;
vector<int> f;

int H,W;
vector<vector<int>> grid;

void dfs(int h, int w){
  grid[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||nw<0||nh>=H||nw>=W) continue;
      //探索済みまたは海
      if(grid[nh][nw]==0) continue;

      dfs(nh,nw);
    }
  }
}

int main(int argc,char **argv){
  while(1){
    cin>>W>>H;
    if(W==0 && H==0) break;

    grid.assign(H,vector<int>(W,0));
    rep(h,0,H){
      rep(w,0,W) cin>>grid[h][w];
    }

    int cnt=0;
    rep(h,0,H){
      rep(w,0,W){
        if(grid[h][w]==0) continue;
        dfs(h,w);
        cnt++;
      }
    }

    cout<<cnt<<endl;


  }
  }