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 <cmath>
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<set>
#include<tuple>
#include<bitset>
#include<iomanip>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll inf = 1000000007;
int dx[8] = { 1,-1,0,0,-1,-1,1,1 };
int dy[8] = { 0,0,1,-1,-1,1,-1,1 };
int dif[55][55];
char c[55][55];
int h, w;
void dfs(P p) {
	queue<P>que;
	que.push(p);
	while (!que.empty()) {
		int nowy = que.front().first;
		int nowx = que.front().second;
		que.pop();
		for (int i = 0; i < 8; i++) {
			int newy = nowy + dy[i];
			int newx = nowx + dx[i];
			if (newy >= 0 && newy < h && newx >= 0 && newx < w && dif[newy][newx] == inf && c[newy][newx] == '1') {
				dif[newy][newx] = 1;
				que.push(make_pair(newy, newx));
			}
		}

	}
	
}

int main() {
	vector<ll>ans;
	while (1) {
		
		ll count = 0;
		cin >> w >> h;
		if (h == 0 && w == 0) {
			break;
		}
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> c[i][j];
				dif[i][j] = inf;
			}
		}
		
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (dif[i][j] == inf&&c[i][j]=='1') {
					count++;
					dif[i][j] = 1;
					dfs(make_pair(i, j));
				}
			}
		}
		ans.push_back(count);
	}
	for (int i = 0; i < ans.size(); i++) {
		cout << ans[i] << endl;
	}
	return 0;
}

C++: #include<bits//stdc++.h>
using namespace std;
int sum;
void solve(bool a[50][50],int h, int w, int i, int j){
	for(int z=-1;z<2;z++){
		for(int x=-1;x<2;x++){
			if(i+z>=0&&i+z<h&&j+x>=0&&j+x<w&&a[i+z][j+x]){
				a[i+z][j+x]=false;
				solve(a,h,w,i+z,j+x);
			}
		}
	}
}
int main(){
	int h,w;
	while(cin>>w>>h&&h!=0&&w!=0){
		bool a[50][50];
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				int x;
				cin>>x;
				if(x==0) a[i][j]=false;
				else a[i][j]=true;
			}
		}
		sum=0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(a[i][j]){
					sum++;
					solve(a,h,w,i,j);
				}
			}
		}
		cout<<sum<<endl;
	}
}

C++: #include <stdio.h>

void dps(int il[50][50], int y, int x)	{
	il[y][x] = 0;
	
	for (int dy = -1; dy <= 1; dy++)	{
		for (int dx = -1; dx <= 1; dx++)	{
			if (dy == 0 && dx == 0)	continue;
			if (y+dy < 0 || y+dy >= 50 || x+dx < 0 || x+dx >= 50)	continue;
			if (il[y+dy][x+dx] == 1)	dps(il, y+dy, x+dx);
		}
	}
}

int main()	{
	
	int w, h, cnt;
	int il[50][50];
	
	while (true)	{
		
		scanf("%d %d", &w, &h);
		if (w == 0)	break;
		
		cnt = 0;
		
		for (int y = 0; y < 50; y++)	{
			for (int x = 0; x < 50; x++)	{
				il[y][x] = 0;
			}
		}
		
		for (int y = 0; y < h; y++)	{
			for (int x = 0; x < w; x++)	{
				scanf("%d", &il[y][x]);
			}
		}
		
		for (int y = 0; y < h; y++)	{
			for (int x = 0; x < w; x++)	{
				if (il[y][x] == 1)	{
					dps(il, y, x);
					cnt++;
				}
			}
		}
		
		printf("%d\n", cnt);
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int fill_land(int y,int x,int h,int w,vector<vector<int>> &c);

int main()
{

	for (;;)
	{
		int w,h;
		cin >> w >> h;
		if ((w==0)&&(h==0))
		{
			return 0;
		}

		vector<vector<int>> c(h,vector<int>(w));

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

		int count=0;
		for (int i=0;i<h;++i)
		{
			for (int j=0;j<w;++j)
			{
				if (c[i][j]!=0)
				{
					// land
					++count;
					c[i][j]=0;
					fill_land(i,j,h,w,c);
				}
			}
		}
		cout << count << endl;
	}
}

int fill_land(int y,int x,int h,int w,vector<vector<int>> &c)
{
	stack<pair<int,int>> st;

	st.push(make_pair(y,x));
	while (st.empty()==0)
	{
		auto p=st.top();
		st.pop();

		if ( (p.first>0) && (c[p.first-1][p.second]!=0) )
		{
			c[p.first-1][p.second]=0;
			st.push(make_pair(p.first-1,p.second));
		}
		if ( (p.first<h-1) && (c[p.first+1][p.second]!=0) )
		{
			c[p.first+1][p.second]=0;
			st.push(make_pair(p.first+1,p.second));
		}
		if ( (p.second>0) && (c[p.first][p.second-1]!=0) )
		{
			c[p.first][p.second-1]=0;
			st.push(make_pair(p.first,p.second-1));
		}
		if ( (p.second<w-1) && (c[p.first][p.second+1]!=0) )
		{
			c[p.first][p.second+1]=0;
			st.push(make_pair(p.first,p.second+1));
		}
		if ( (p.first>0) && (p.second>0) && (c[p.first-1][p.second-1]!=0) )
		{
			c[p.first-1][p.second-1]=0;
			st.push(make_pair(p.first-1,p.second-1));
		}
		if ( (p.first>0) && (p.second<w-1) && (c[p.first-1][p.second+1]!=0) )
		{
			c[p.first-1][p.second+1]=0;
			st.push(make_pair(p.first-1,p.second+1));
		}
		if ( (p.first<h-1) && (p.second>0) && (c[p.first+1][p.second-1]!=0) )
		{
			c[p.first+1][p.second-1]=0;
			st.push(make_pair(p.first+1,p.second-1));
		}
		if ( (p.first<h-1) && (p.second<w-1) && (c[p.first+1][p.second+1]!=0) )
		{
			c[p.first+1][p.second+1]=0;
			st.push(make_pair(p.first+1,p.second+1));
		}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int c[100][100];
int w, h;
void dfs(int x,int y) {
	c[x][y] = 0;
	for (int i = -1; i <= 1; i++) {
		for (int j = -1; j <= 1; j++) {
			if (x + i < w&&x + i >= 0 && y + j >= 0 && y + j < h) {
				if (c[x + i][y + j] == 1) {
					dfs(x + i, y + j);
				}
			}
		}
	}
}

int main(void) {
	int ans;
	while (1) {
		ans = 0;
		cin >> w >> h;
		if (w == 0 && h == 0) {
			break;
		}
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> c[j][i];
			}
		}
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (c[j][i] == 1) {
					dfs(j, i);
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}