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;
typedef vector<vector<int> > field;

int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1};

void erase(field &f, int y, int x)
{
    f[y][x] = 0;
    for(int i=0; i<8; i++)
    {
        if(f[y+dy[i]][x+dx[i]]) erase(f, y+dy[i], x+dx[i]);
    }
}

int main()
{
    int w, h;
    while(cin >> w >> h, w|h)
    {
        field f(h+2, vector<int>(w+2, 0));
        for(int y=1; y<=h; y++)
        {
            for(int x=1; x<=w; x++)
            {
                int c;
                cin >> c;
                f[y][x] = c;
            }
        }
        int ct = 0;
        for(int y=1; y<=h; y++)
        {
            for(int x=1; x<=w; x++)
            {
                if(f[y][x])
                {
                    ct++;
                    erase(f, y, x);
                }
            }
        }
        cout << ct << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
int hx[8] = {0, 0, 1, -1, 1, 1, -1, -1};
int hy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
vector<vector<int>> mp;
vector<vector<bool>> used;
int w, h;

void dfs(int i, int j) {
  if (used[i][j])
    return;
  used[i][j] = true;
  for (int k = 0; k < 8; k++) {
    int ni = i + hx[k], nj = j + hy[k];
    if (ni >= h || ni < 0 || nj >= w || nj < 0)
      continue;
    if (used[ni][nj] || mp[ni][nj] == 0)
      continue;
    dfs(ni, nj);
  }
}

int main() {
  while (true) {
    cin >> w >> h;
    if (w == 0)
      break;
    mp = vector<vector<int>>(h, vector<int>(w));
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> mp[i][j];
    used = vector<vector<bool>>(h, vector<bool>(w));
    int ans = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (used[i][j])
          continue;
        if (mp[i][j] == 0)
          continue;
        dfs(i, j);
        ans++;
      }
    }
    cout << ans << endl;
  }
}

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

vector<int> route;
#define P 50
#define Q 50
char a[P+2][Q+2];


void search(int place1,int place2){
    a[place1][place2] = '0';
    if(a[place1][place2-1] == '1'){//左
        route.push_back(1);
        search(place1,place2-1);
    }
    else if(a[place1+1][place2] == '1'){//下
        route.push_back(2);
        search(place1+1,place2);
    }
    else if(a[place1][place2+1] == '1'){//右
        route.push_back(3);
        search(place1,place2+1);
    }
    else if(a[place1-1][place2] == '1'){//上
        route.push_back(4);
        search(place1-1,place2);
    }
    else if(a[place1+1][place2-1] == '1'){//左下
        route.push_back(5);
        search(place1+1,place2-1);
    }
    else if(a[place1+1][place2+1] == '1'){//右下
        route.push_back(6);
        search(place1,place2+1);
    }
    else if(a[place1-1][place2+1] == '1'){//右上
        route.push_back(7);
        search(place1-1,place2);
    }
    else if(a[place1-1][place2-1] == '1'){//左上
        route.push_back(8);
        search(place1-1,place2);
    }
    else{
        int bk = route.back();
        route.pop_back();//戻る
        if(bk == 0) return;
        else if(bk == 1)search(place1,place2+1);
        else if(bk == 2)search(place1-1,place2);
        else if(bk == 3)search(place1,place2-1);
        else if(bk == 4)search(place1+1,place2);
        else if(bk == 5)search(place1-1,place2+1);
        else if(bk == 6)search(place1-1,place2-1);
        else if(bk == 7)search(place1+1,place2-1);
        else if(bk == 8)search(place1+1,place2+1);
    }
}

int main(void){
    int ct;
    int x,y;
    while(1){
        ct = 0;
        cin >> y >> x;
        if(x == 0 & y == 0) break;
        for(int i = 0;i < x+2;i++){
            for(int j = 0;j < y+2;j++){
                if(i == 0||j == 0||i == x+1||j == y+1)a[i][j] = '0';
                else cin >> a[i][j];
                
            }
            
        }
        for(int i = 1;i < x+1;i++){
            for(int j = 1;j < y+1;j++){
                if(a[i][j] == '1'){
                    route.push_back(0);
                    search(i,j);
                    ct++;
                }
            }
        }
        cout << ct << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double EPS=1e-10;
const ll INF=1000000000;
int w,h;
int c[60][60];
bool visited[60][60];
int dx[8]={0,1,0,-1,1,-1,1,-1};
int dy[8]={1,0,-1,0,1,1,-1,-1};
bool field(int x,int y){
    if(x>=0&&x<w&&y>=0&&y<h){
        return true;
    }
    return false;
}
int dfs(int x,int y){
    if(visited[y][x]){
        return 0;
    }
    visited[y][x]=true;
    for(int i=0;i<8;i++){
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(field(nx,ny)&&c[ny][nx]){
            dfs(nx,ny);
        }
    }
    return 1;
}
int main(){
    while(cin>>w>>h,w+h){
        memset(visited,false,sizeof(visited));
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin>>c[i][j];
            }
        }
        int ans=0;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(c[i][j]){
                    ans+=dfs(j,i);
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}