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<stdio.h>
void printmap();
void draw(int x,int y);
int w,h,c[50][50];

int main(){
	int i,j;
	int count;


	scanf("%d %d",&w,&h);


	while((w!=0)&&(h!=0)){
		count=0;
	
		for(j=0;j<h;j++){
			for(i=0;i<w;i++){
				scanf("%d",&c[j][i]);
			}
		}
		//printf("\n");
		
		for(i=0;i<w;i++){
			c[h][i]=0;
		}
		for(i=0;i<h;i++){
			c[i][w]=0;
		}

		for(i=0;i<w;i++){
			for(j=0;j<h;j++){
				if(c[j][i]==1){
					count++;
					//printf("count up count=%d\n",count);
					draw(j,i);
					//printmap();
				}
			}
		}

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

void draw(int x,int y){
	//printf("delete %d %d\n",x,y);
	c[x][y]=0;
	if(c[x+1][y]==1){
		draw(x+1,y);
	}
	if(c[x-1][y]==1){
		draw(x-1,y);
	}
	if(c[x][y-1]==1){
		draw(x,y-1);
	}
	if(c[x][y+1]==1){
		draw(x,y+1);
	}

}

void printmap(){
	int i,j;
	for(j=0;j<h;j++){
		for(i=0;i<w;i++){
			printf("%d ",c[j][i]);
		}
		printf("\n");
	}
}

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

int w, h;
int map[50][50];
int ans;

void Suibotu(int y, int x)
{//??¨??????????????????????±?????????¶????????°???????°´????????????
	map[y][x] = 0;
	if(x<w-1 && map[y][x+1]==1)				//???
		Suibotu(y, x+1);
	if(x<w-1 && y<h-1 && map[y+1][x+1]==1)	//??????
		Suibotu(y+1, x+1);
	if(y<h-1 && map[y+1][x]==1)				//???
		Suibotu(y+1, x);
	if(x>0 && y<h-1 && map[y+1][x-1]==1)	//??????
		Suibotu(y+1, x-1);
	if(x>0 && map[y][x-1]==1)				//???
		Suibotu(y, x-1);
	if(x>0 && y>0 && map[y-1][x-1]==1)		//??????
		Suibotu(y-1, x-1);
	if(y>0 && map[y-1][x]==1)				//???
		Suibotu(y-1, x);
	if(x<w-1 && y>0 && map[y-1][x+1]==1)	//??????
		Suibotu(y-1, x+1);
}
int main(){
	cin >> w >> h;
	while(w+h>0){
		ans = 0;
		for(int i=0; i<h; i++)
			for(int j=0; j<w; j++)
				cin >> map[i][j];
			for(int i=0; i<h; i++)
			for(int j=0; j<w; j++)
				if(map[i][j]==1){
					ans++;
					Suibotu(i, j);
				}
		cout << ans << endl;
		cin >> w >> h;
	}
	return 0;
}

C++: #include <iostream>
#include <stack>
using namespace std;
int dx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
int dy[] = { -1, -1, -1, 0, 1, 1, 1, 0 };

int main(){
	int w, h;
	while( cin >> w >> h && w && h ){
		int field[50][50];
		stack< pair<int, int> > st;
		for( int i = 0; i < h; i++ ){
			for( int j = 0; j < w; j++ ){
				cin >> field[i][j];
			}
		}
		int sum = 0;
		for( int i = 0; i < h; i++ ){
			for( int j = 0; j < w; j++ ){
				if( field[i][j] == 1 ){
					sum++;
					field[i][j] = 0;
					st.push( pair<int, int>(i, j) );
					//cout << i << ' ' << j << endl;
					while( !st.empty() ){
						//cout << st.top().second << ' ' << st.top().first << endl;
						int x0 = st.top().second;
						int y0 = st.top().first;
						st.pop();
						for( int k = 0; k < 8; k++ ){
							int x = x0 + dx[k];
							int y = y0 + dy[k];
							if( 0 <= x && x < w && 0 <= y && y < h && field[y][x] == 1 ){
								st.push( pair<int, int>(y, x) );
								field[y][x] = 0;
								//cout << y << ' ' << x << endl;
							}
						}
					}
				}
			}
		}
		cout << sum << endl;
	}
}