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>
#include<algorithm>
#include<stack>
#include<queue>
#include<string.h>
#include<string>
#include<functional>
#include<set>
using namespace std;

#define INF 1<<21
#define DIV 1000000007
#define MAX 250000

int table[51][51];
int W, H;

void dfs(int x, int y) {
  table[y][x] = 0;

  for (int dx = -1; dx <= 1; dx++) {
	for (int dy = -1; dy <= 1; dy++) {
	  int nx = x + dx, ny = y + dy;
	  if (0 <= nx&&nx < W && 0 <= ny&&ny < H&&table[ny][nx] == 1)dfs(nx, ny);
	}
  }
  return;
}

int main(){
  while (cin >> W >> H, W || H) {
	int ans = 0;
	memset(table, 0, sizeof(table));
	int t;
	for (int y = 0; y < H; y++) {
	  for (int x = 0; x < W; x++) {
		cin >> table[y][x];
	  }
	}

	for (int y = 0; y < H; y++) {
	  for (int x = 0; x < W; x++) {
		if (table[y][x] == 1) {
		  dfs(x, y);
		  ans++;
		}
	  }
	}
	cout << ans << endl;
  }

}

C++: #include <bits/stdc++.h>

#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define INF  numeric_limits<int>::max()
#define NINF numeric_limits<int>::min()

using namespace std;
using ll = long long;

int W, H;
bool sea_map[50][50];
bool is_reached[50][50];

void dfs(int s_wi, int s_hi) {
    if(s_wi < 0 || W <= s_wi ||
       s_hi < 0 || H <= s_hi ||
       !sea_map[s_hi][s_wi]  ||
       is_reached[s_hi][s_wi]) {
        return;
    }

    is_reached[s_hi][s_wi] = true;

    dfs(s_wi + 1, s_hi + 1);
    dfs(s_wi + 1, s_hi    );
    dfs(s_wi + 1, s_hi - 1);
    dfs(s_wi    , s_hi - 1);
    dfs(s_wi - 1, s_hi - 1);
    dfs(s_wi - 1, s_hi    );
    dfs(s_wi - 1, s_hi + 1);
    dfs(s_wi    , s_hi + 1);
}

bool search_start_point(int& s_wi, int& s_hi) {
    REP(hi, H) {
        REP(wi, W) {
            if(sea_map[hi][wi] && !is_reached[hi][wi]) {
                s_wi = wi;
                s_hi = hi;
                return true;
            }
        }
    }
    return false;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    while(true) {
        std::cin >> W >> H;
        if(W == 0 && H == 0) {
            break;
        }

        REP(hi, H) {
            REP(wi, W) {
                std::cin >> sea_map[hi][wi];
                is_reached[hi][wi] = false;
            }
        }

        int ans = 0;
        while(true) {
            int s_wi, s_hi;
            if(!search_start_point(s_wi, s_hi)) {
                break;
            }
            else {
                dfs(s_wi, s_hi);
                ans++;
            }
        }

        std::cout << ans << std::endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define endl '\n'

const int N = 51;
int w,h;
int board[N][N];

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

void dfs(int x, int y){
    board[x][y] = 0;
    for(int i = 0; i < 8; i++){
        int nx = x + dx[i];
        int ny = y + dy[i];
        if(nx >= 0 && nx < h && ny >= 0 && ny < w){
            if(board[nx][ny] == 1) dfs(nx,ny);
        }
    }
}

void solve(){
    rep(i,h){
        rep(j,w) {
            cin >> board[i][j];
        }
    }

    int ans = 0;
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(board[i][j] == 1) {
                ans++;
                dfs(i,j);
            }
        }
    }
    cout << ans << endl;
}

signed main() {
    while(true){
        cin >> w >> h;
        if(w == 0 && h == 0) return 0;
        solve();
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using PL = pair<ll, ll>;
// Welcome to my source code!

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

void dfs(int cx, int cy) {
    for (int dx = -1; dx <= 1; dx++) {
        for (int dy = -1; dy <= 1; dy++) {
            int nx = cx + dx, ny = cy + dy;
            if (0 <= nx && nx < h && 0 <= ny && ny < w && c[nx][ny] != 0) {
                c[nx][ny] = 0;
                dfs(nx, ny);
            }
        }
    }
}

int main() {
    while (cin >> w >> h && w && h) {
        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] == 0) continue;
                dfs(i, j);
                ans++;
            }
        }
        cout << ans << endl;
    }
}