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 field[64][64];
bool visited[64][64];
int dx[] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[] = {1, -1, 0, 0, 1, -1, 1, -1};

void rec(int y, int x)
{
	if (visited[y][x] == false){
		return;
	}
	visited[y][x] = false;
	for (int i = 0; i < 8; i++){
		rec(y + dy[i], x + dx[i]);
	}
	return;
}

int main(void)
{
	int w, h;
	while (cin >> w >> h, w && h){
		for (int i = 0; i < 64; i++){
			for (int j = 0; j < 64; j++){
				field[i][j] = 0;
				visited[i][j] = false;
			}
		}
		for (int i = 1; i <= h; i++){
			for (int j = 1; j <= w; j++){
				cin >> field[i][j];
				if (field[i][j] == 1){
					visited[i][j] = true;
				}
			}
		}
		int ret = 0;
		for (int i = 0; i < 64; i++){
			for (int j = 0; j < 64; j++){
				if (visited[i][j] == true){
					rec(i, j);
					ret++;
				}
			}
		}
		cout << ret << endl;
	}
	
	return 0;
}

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

int main()
{
	while (true)
	{
		int w, h;
		int num = 0;
		cin >> w >> h;
		if (w == 0 && h == 0)break;

		int **c;
		c = new int*[h + 2];
		for (int i = 0; i < h + 2; i++)	c[i] = new int[w + 2]; 
		for (int i = 0; i < h + 2; i++)				  
		{
			for (int l = 0; l < w + 2; l++)	c[i][l] = 0;
		}

		for (int i = 1; i < h + 1; i++)
		{
			for (int l = 1; l < w + 1; l++)	cin >> (c[i])[l];
		}
		for (int i = 1; i < h + 1; i++)
		{
			for (int l = 1; l < w + 1; l++)
			{
				if (c[i][l] == 1)		     
				{
					if (c[i + 1][l - 1] == 0 && c[i + 1][l] == 0 && c[i + 1][l + 1] == 0 && c[i][l + 1] == 0)
					{
						num++; 
					}

					else if ((c[i + 1][l - 1] == 1 && c[i + 1][l] == 0) && (c[i + 1][l + 1] == 1 || c[i][l + 1] == 1))	   
					{
						c[i][l] = 0;
						c[i + 1][l] = 1; 
					}
					else
					{
						c[i][l] = 0; 
					}

				}
			}
		}
		cout << num << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>

using namespace std;

typedef pair<int, int> p;

int t_x[] = {1, 1, 1, 0, -1, -1, -1, 0};
int t_y[] = {1, 0, -1, -1, -1, 0, 1, 1};

int field[52][52];  

int main()
{
  while (true) {
    memset(field, 0, sizeof field);
    int width, height;
    scanf("%d %d", &width, &height);
    
    if (width == 0 && height == 0) break;
    
    for (int i = 1; i <= height; i++) {
      for (int j = 1; j <= width; j++) {
	scanf(" %d", &field[i][j]);
      }
    }
    
    queue<p> que;
    int c = 0;

    for (int i = 1; i <= height; i++) {
      for (int j = 1; j <= width; j++) {
	if (field[i][j] == 1) {
	  c++;
	  que.push(p(i, j));
	  field[i][j] = 0;
	  while (!que.empty()) {
	    p tmp = que.front();
	    que.pop();
	    //field[tmp.second][tmp.first] = 0;
	    //cout << tmp.first << ", " << tmp.second << endl;
	    for (int k = 0; k < 8; k++) {
	      int tx = tmp.second + t_x[k];
	      int ty = tmp.first + t_y[k];
	      //cout << ty << ", "<< tx << " =  "<< field[ty][tx] << endl;
	      if (field[ty][tx] == 1) {
		field[ty][tx] = 0;
		que.push(p(ty, tx));
	      }
	    }
	    //cout << endl;
	    //cout << i << " " << j << endl;
	    for (int k = 1; k <= height; k++) {
	      for (int l = 1; l <= width; l++) {
		//cout << field[k][l] << " ";
	      }
	      //cout << endl;
	    }
	    //cout << endl;
	  }
	}
      }     
    }
    cout << c << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#include <math.h>
using namespace std;
int W,H;
char field[60][60];
int dx[8]{-1,0,1,-1,1,-1,0,1};
int dy[8]{1,1,1,0,0,-1,-1,-1};

void dfs(int x,int y){
    field[x][y] = '0';
    for(int i = 0;i < 8;i++){
        int nx = x + dy[i];
        int ny = y + dx[i];
        if(0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] == '1'){
            dfs(nx,ny);
        }
    }
    return;
}
int main(){
    for(int k = 0;1;k++){
    cin >> W >> H;
    if(H == 0 && W == 0){
        return 0;
    }
    for(int i = 0;i < H;i++){
        for(int j = 0;j < W;j++){
            cin >> field[i][j];
        }
    }
    int ans = 0;
    bool frag = 0;
    for(int i = 0;i < H;i++){
        for(int j = 0;j < W;j++){
            if(field[i][j] == '1'){
                ans++;
                dfs(i,j);
            }
        }
    }
    cout << ans  << endl;
    }
}