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;

int field[50][50];
int w, h;

void dfs(int x, int y) {
  if(x < 0 || h <= x || y < 0 || w <= y || field[x][y] == 0) return; 
  field[x][y] = 0;
  for(int i = -1; i <= 1; i++) {
    for(int j = -1; j <= 1; j++) {
      if(i == 0 && j == 0) continue;
      dfs(x+i, y+j);
    }
  }
}

int main() {

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

    int ans = 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;
  }
}

C++: #include <iostream>

using namespace std;

// 0: 海, 1: 陸
int board[50][50] = {{0}};

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

bool DFS(int i, int j, int w, int h) {
  if (i < 0 || i >= h || j < 0 || j >= w) return false;

  if (!board[i][j]) return false;

  board[i][j] = 0;
  for (int d = 0; d < 8; ++d)
    DFS(i + dy[d], j + dx[d], w, h);

  return true;
}

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

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

    int ans = 0;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (DFS(i, j, w, h)) ++ans;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) FOR(i,0,n)
#define repr(i,n) for(int i=(n)-1;0<=i;--i)
#define each(e,v) for(auto&& e:(v))
#define all(v) begin(v),end(v)
#define dump(x) cerr<<#x<<": "<<(x)<<endl
#define INF 100000000
#define IINF 9999999999999;
using vint = vector<int>;
using ll = long long;
using vll = vector<ll>;
template <class T> void chmin(T& a, const T& b) { a = min(a, b); }
template <class T> void chmax(T& a, const T& b) { a = max(a, b); }

bool earth[51][51];

void erase_island(int h, int w) {
    earth[h][w] = false;
    //cout << "# " << h << " " << w << endl;
    if(earth[h - 1][w - 1]) erase_island(h - 1, w - 1);
    if(earth[h - 1][w]) erase_island(h - 1, w);
    if(earth[h - 1][w + 1]) erase_island(h - 1, w + 1);
    if(earth[h][w + 1]) erase_island(h, w + 1);
    if(earth[h + 1][w + 1]) erase_island(h + 1, w + 1);
    if(earth[h + 1][w]) erase_island(h + 1, w);
    if(earth[h + 1][w - 1]) erase_island(h + 1, w - 1);
    if(earth[h][w - 1]) erase_island(h, w - 1);
}

int main() {
    //ofstream outputfile("output.txt");
    while(true) {
        int w, h;
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        rep(i, h) {
            rep(j, w) {
                int temp;
                cin >> temp;
                if(temp == 0) earth[i][j] = false;
                else earth[i][j] = true;
            }
        }

        int ans = 0;
        rep(i, h) {
            rep(j, w) {
                if(earth[i][j]) {
                    erase_island(i, j);
                    ++ans;
                }
            }
        }

        cout << ans << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<math.h>
using namespace std;
#define N (1000000000+7)
#define INF 1e16
typedef long long ll;
typedef pair<int,int> P;

int h,w;
int board[52][52];
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
bool check(int i, int j) {
	return (i >= 0) && (i < h) && (j >= 0) && (j < w) && (board[i][j] ==1);
}

void dfs(int x,int y){
    if(board[x][y]==0)return;
    board[x][y]=0;
    for(int i=x-1;i<=x+1;i++){
        for(int j=y-1;j<=y+1;j++){
            if(i==x && j==y)continue;
            if(check(i,j))dfs(i,j);
        }
    }
    return;
}

int main(void){
    while(true){
        int 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>>board[i][j];
            }
        }
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(board[i][j]==1){
                    dfs(i,j);
                    ans++;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}