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>
#define rep(i, n) for (int i=0; i<(n); ++i)
#define repr(i, a, b) for (int i=a; i<(b); ++i)
#define reprev(i, n) for (int i=n-1; i>=0; --i)
#define reprrev(i, a, b) for (int i=b-1; i>=(a); --i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

int main() {
    cout << fixed << setprecision(10);
    
    int w=1, h=1;

    while(w != 0 && h != 0) {
        int ans = 0;
        cin >> w >> h;
        if (w == 0 && h == 0) {
            break;
        }
        vector<vector<int>> m(h, vector<int>(w));
        rep(i, h) {
            rep(j, w) {
                cin >> m[i][j];
            }
        }
        stack<P> q;
        rep(i, h) {
            rep(j, w) {
                // 到達していない陸じゃなければcontinue
                if (m[i][j] != 1) continue;
                q.emplace(make_pair(i, j));
                m[i][j] = 0;
                while(!q.empty()) {
                    P pp = q.top();
                    q.pop();
                    rep(d, 8) {
                        int nh = pp.first + dh[d], nw = pp.second + dw[d];
                        if (nh >= 0 && nw >= 0 && nh < h && nw < w && m[nh][nw] == 1) {
                            q.emplace(make_pair(nh, nw));
                            m[nh][nw] = 0;
                        }
                    }
                }
                ans += 1;
            }
        }

        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#define rep(i,to) for(int i=0;i<to;i++)
using namespace std;

int w,h;
int f[51][51];

int di[] = { 0, 0, 1, 1, 1,-1,-1,-1};
int dj[] = { 1,-1, 0, 1,-1, 0, 1,-1};

void dfs(int i,int j){
	f[i][j] = 0;
	rep(k,8){
		int ni = i + di[k];
		int nj = j + dj[k];
		if(ni>=0 && ni<h && nj>=0 && nj<w && f[ni][nj]==1)
			dfs(ni,nj);
	}
}

int main(void){
	while(cin>>w>>h && (w||h)){
		rep(i,h) rep(j,w) cin>>f[i][j];

		int ans = 0;
		rep(i,h) rep(j,w) if(f[i][j]==1) { dfs(i,j); ans++;}
		cout<<ans<<endl;
	}

	return 0;
}

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

int array[1000][1000];
int X,Y;

void dfs(int y, int x){

  int nx,ny;
  array[y][x]=0;
  for(int dx=-1;dx<=1;dx++)
    for(int dy=-1;dy<=1;dy++){
      nx=x+dx;
      ny=y+dy;
      if(nx<X && ny<Y && nx>=0 && ny>=0 && array[ny][nx]!=0 ) dfs(ny,nx);
    }

}


int main(){

  while(1){
    cin>>X>>Y;
    int cnt=0;
    if(X==0 && Y==0) break;
    for(int i=0;i<Y;i++)
      for(int j=0;j<X;j++)
	cin>>array[i][j];
    
    for(int i=0;i<Y;i++)
      for(int j=0;j<X;j++)
	if(array[i][j]==1){ 
	  dfs(i,j);
	  cnt++;
	}
    cout<<cnt<<endl;
  }
  return 0;
}

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

int w, h;
int map[52][52];

int check(int i, int j)
{
	if (map[i][j] == 0)return 0;
	else
	{
		map[i][j] = 0;
		check(i-1, j - 1);
		check(i-1, j);
		check(i-1, j + 1);
		check(i, j - 1);
		check(i, j + 1);
		check(i + 1, j - 1);
		check(i + 1, j);
		check(i + 1, j + 1);
		return 0;
	}
}

int main()
{
	while (true)
	{
		map[52][52] = { 0 };
		int count = 0;
		cin >> w >> h;
		if (w == 0)return 0;

		for (int i = 1; i < h+1; i++)
		{
			for (int j = 1; j < w+1; j++)
			{
				cin >> map[i][j];
			}
		}
		for (int i = 1; i < h+1; i++)
		{
			for (int j = 1; j < w+1; j++)
			{
				if (map[i][j] == 1)
				{
					check(i, j);
					count++;
				}
			}
		}
		cout << count << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
int dx[8] = {1,0,-1,0,1,-1,-1,1};//右、上、左、下、右上、左上、左下、右下のつもり
int dy[8] = {0,1,0,-1,1,1,-1,-1};//実際は色々反転している。どうせ4か8方向調べるから関係ないとしている。
bool seen[60][60]={true};
int h,w;
void dfs(vector<vector<int> > &G,int i,int j){
    if(G[i][j]) seen[i][j] = true;
    for(int k=0;k<8;k++){
	int di = i + dx[k];
	int dj = j + dy[k];
	if(di < 0 || h <= di || dj < 0 || w <= dj)continue;
	
	if(seen[di][dj]==false) dfs(G,di,dj);
    }
}
int main(void){
    while(cin >> w >> h){
	if(w == 0 && h== 0) break;
	for(int i=0;i<h;i++)for(int j=0;j<w;j++) seen[i][j] = true;
	vector<vector<int> > G(h,vector<int>(w));
	for(int i=0;i<h;i++){
	    for(int j=0;j<w;j++){
		cin >> G[i][j];
		if(G[i][j]) seen[i][j] = false;
	    }
	}
	int t =0;
	for(int i=0;i<h;i++){
	    for(int j=0;j<w;j++){
		if(seen[i][j]) continue;
		dfs(G,i,j);
		t++;
	    }
	}
	cout << t << endl;
    }

    return 0;
}

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

int x,y;
int field[50][50];

void dfs(int w, int h){
	field[w][h]=0;
	for(int dy=-1; dy<=1; dy++){
		for(int dx=-1; dx<=1; dx++){
			int nx = w + dx;
			int ny = h + dy;
			if(nx>=0 && nx<x && ny>=0 && ny<y && field[nx][ny]==1)
				dfs(nx,ny);
		}
	}
	return;
}


int main(){
	while(1){
		int result = 0;
		cin >> x >> y;
		if(x == 0 && y == 0)
			break;
		for(int j=0; j<y; j++){
			for(int i=0; i<x; i++){
				cin >> field[i][j];
			}
		}
		for(int j=0; j<y; j++){
			for(int i=0; i<x; i++){
				if(field[i][j]==1){
					result++;
					dfs(i,j);
				}
			}
		}
		cout << result << endl;
	}
}