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>
using namespace std;
int a[52][52];
int sima(int i,int j,int k){
	if(a[i-1][j-1] == 1){
		a[i-1][j-1] = k;
		sima(i-1,j-1,k);
	}
	if(a[i-1][j] == 1){
		a[i-1][j] = k;
		sima(i-1,j,k);
	}
	if(a[i-1][j+1] == 1){
		a[i-1][j+1] = k;
		sima(i-1,j+1,k);
	}
	if(a[i][j-1] == 1){
		a[i][j-1] = k;
		sima(i,j-1,k);
	}
	if(a[i][j+1] == 1){
		a[i][j+1] = k;
		sima(i,j+1,k);
	}
	if(a[i+1][j-1] == 1){
		a[i+1][j-1] = k;
		sima(i+1,j-1,k);
	}
	if(a[i+1][j] == 1){
		a[i+1][j] = k;
		sima(i+1,j,k);
	}
	if(a[i+1][j+1] == 1){
		a[i+1][j+1] = k;
		sima(i+1,j+1,k);
	}
}

int main() {
	int w,h,k;
	while(1){
		for(int i = 0;i < 52;i++){
			for(int j = 0;j < 52;j++){
				a[i][j] = 0;
			}
		}
		cin >> w >> h;
		if(w == 0)break;
		for(int i = 1;i <= h;i++){
			for(int j = 1;j <= w;j++){
				cin >> a[i][j];
			}
		}
		k = 2;
		for(int i = 1;i <= h;i++){
			for(int j = 1;j <= w;j++){
				if(a[i][j] == 1){
					sima(i,j,k);
					k++;
				}
			}
		}
		cout << k-2 <<endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int dx[]={-1,0,1,-1,1,-1,0,1};
int dy[]={-1,-1,-1,0,0,1,1,1};

    int w,h;
void dfs(int i,int j,vector<vector<int>>& x){
        x[i][j]=0;
        for(int r=0;r<8;r++)
            if(0<=i+dx[r] && i+dx[r]<h && 0<=j+dy[r] && j+dy[r]<w && x[i+dx[r]][j+dy[r]])
                dfs(i+dx[r],j+dy[r],x);



}
int main(){
    while(cin>>w>>h,w){
        vector<vector<int>> x(h,vector<int>(w,0));
        for(int i=0;i<h;i++)
            for(int j=0;j<w;j++)
                cin>>x[i][j];
        int cnt=0;
        for(int i=0;i<h;i++)
            for(int j=0;j<w;j++){
                if(x[i][j]){
                    cnt++;
                    dfs(i,j,x);


                }




            }
        cout<<cnt<<endl;



    }


    return 0;
}

C++: #include <stack>
#include <cstdio>

using namespace std;
bool c[52][52]; 
int check(int arg[][52], int i, int j);

int main(void){
	int w, h;
	int arg[52][52];
	int i, j;
	int count;


	scanf("%d %d", &w, &h);
	while(w!=0 && h!=0){
		count=0;
		for(i=0; i<=h+1; i++){
			for(j=0; j<=w+1; j++){
				if(i==0 || i==(h+1) || j==0 || j==(w+1)){
					c[i][j]=false; 
					arg[i][j]=0;
				}
			}
		}

		for(i=1; i<=h; i++){
			for(j=1; j<=w; j++){ 
				scanf("%d",&arg[i][j]);
				if(arg[i][j])
					c[i][j]=true;
				else
					c[i][j]=false;
			}
		}

		for(i=1; i<=h; i++){
			for(j=1; j<=w; j++){
				if(arg[i][j]==1 && c[i][j]==true){
					count = count + check(arg, i, j);
				}
			}
		}

		printf("%d\n", count);
		scanf("%d %d", &w, &h);
	}

	return 0;
}

int check(int arg[][52], int i, int j){

	if(arg[i+1][j]==1 && c[i+1][j]==true){
		c[i+1][j]=false;
		check(arg, i+1, j);
	}
	if(arg[i][j+1]==1 && c[i][j+1]==true){
		c[i][j+1]=false;
		check(arg, i, j+1);
	}
	if(arg[i-1][j]==1 && c[i-1][j]==true){
		c[i-1][j]=false;
		check(arg, i-1, j);
	}
	if(arg[i][j-1]==1 && c[i][j-1]==true){
		c[i][j-1]=false;
		check(arg, i, j-1);
	}
	if(arg[i-1][j+1]==1 && c[i-1][j+1]==true){
		c[i-1][j+1]=false;
		check(arg, i-1, j+1);
	}
	if(arg[i-1][j-1]==1 && c[i-1][j-1]==true){
		c[i-1][j-1]=false;
		check(arg, i-1, j-1);
	}
	if(arg[i+1][j+1]==1 && c[i+1][j+1]==true){
		c[i+1][j+1]=false;
		check(arg, i+1, j+1);
	}
	if(arg[i+1][j-1]==1 && c[i+1][j-1]==true){
		c[i+1][j-1]=false;
		check(arg, i+1, j-1);
	}

	return 1;

}

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

void search(vector<vector<int>>& map, int x, int y)
{
	if (map[x][y] < 0)
		return;

	if (map[x][y] == 1) {
		map[x][y] = 0;
		search(map, x - 1, y);
		search(map, x + 1, y);
		search(map, x, y - 1);
		search(map, x, y + 1);
		search(map, x - 1, y - 1);
		search(map, x + 1, y + 1); 
		search(map, x + 1, y - 1);
		search(map, x - 1, y + 1);
	}
}
int main()
{
	int w, h;
	while (1) {
		cin >> w >> h;
		if (w == 0 && h == 0)
			break;

		vector<vector<int>> map;
		map = vector<vector<int>>(h+2, vector<int>(w+2, -1));

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

C++: #include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;

int w,h;
int dim[52][52];
int mark[52][52];
int dirx[8] = {-1,-1,-1,0,0,1,1,1};
int diry[8] = {-1,0,1,-1,1,-1,0,1};
queue<pair<int,int> > q;
int check(int x,int y){
	if (x <0 || x >= h || y<0 || y>=w)return 0;
	return 1;
}
int main(){
	while(scanf("%d %d", &w, &h)!= EOF){
		if (w==0 && h==0)return 0;
		memset(mark, -1, sizeof(mark));
		for (int i=0;i<h;i++){
			for (int j=0;j<w;j++){
				scanf("%d", &dim[i][j]);
			}	
		}
		int island = 0;
		for (int i=0;i<h;i++){
			for (int j=0;j<w;j++){
				if (dim[i][j] == 1 && mark[i][j] == -1){
					island++;
					q.push(make_pair(i, j));
					mark[i][j] = 1;
					while(!q.empty()){
						pair<int, int> temp= q.front();
						q.pop();
						for (int k=0;k<8;k++){
							int tx = temp.first + dirx[k];
							int ty = temp.second + diry[k];
							if (check(tx,ty) == 1){
								if (dim[tx][ty] && mark[tx][ty] == -1){
									mark[tx][ty] = 1;
									q.push(make_pair(tx,ty));
								}
							}
						}
					}
				}
			}	
		}
		printf("%d\n", island);
	}
	return 0;
}