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;

#define rep(i, n) REP(i, 0, n)
#define REP(i,k,n) for(int i = k; i < n; i++)
#define mmax(start, end) *max_element(start, end)
#define mmin(start, end) *min_element(start, end)
#define argmax max_element
#define argmin min_element
#define push push_back
#define pop pop_back
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
typedef string str;

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

template<class T = ll> T in() {
    T _in;
    cin >> _in;
    return _in;
}

int w, h;
bool c[55][55] = {};

void DFS(int y, int x) {
    c[y][x] = 0;
    rep(i, 8) {
        if(y+dy[i]<0 || y+dy[i]>=h || x+dx[i]<0 || x+dx[i]>=w) continue;
        if(c[y + dy[i]][x + dx[i]]) {
            DFS(y + dy[i], x + dx[i]);
        }
    }
}

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

    while(cin >> w >> h,w){
        rep(i, h) {
            rep(j, w) {
                cin >> c[i][j];
            }
        }

        int ans = 0;
        // 探索
        rep(y, h) {
            rep(x, w) {
                if (c[y][x]) {
                    DFS(y, x);
                    ans++;
                }
            }
        }

        cout << ans << endl;
    };
    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;


int w , h;
int land[51][51];

void dfs(int px , int py){
	int tx , ty;
	land[py][px]=0;
	for(int i=-1 ; i<=1 ; ++i){
		for(int j=-1 ; j<=1 ; ++j){
			tx=px+i , ty=py+j;
			if(tx<0 || tx>=w || ty<0 || ty>=h) continue;
			if(land[ty][tx]) dfs(tx , ty);
		}
	}
}


int main(){
	int cnt=0;
	while(cin >> w >> h , w||h){
		for(int i=0 ; i< h ; ++i){
			for(int j=0 ; j< w ; ++j){
				cin >> land[i][j];
			}
		}
		for(int i=0 ; i<h ; ++i){
			for(int j=0 ; j<w ; ++j){
				if(land[i][j]){
					dfs( j , i);
					cnt++;
				}
			}
		}
		cout << cnt << endl;
		cnt=0;
	}
	return 0; 
}

C++: #include"bits/stdc++.h"

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;


vector<string>road;
int ix[] = { 0,1,1,1,0,-1,-1,-1 };
int iy[] = { -1,-1,0,1,1,1,0,-1 };
void dfs(int y, int x, int h, int w) {
	road[y][x] = '0';
	rep(i, 8) {
		int dx = x + ix[i], dy = y + iy[i];
		if (dx < 0 || w <= dx || dy < 0 || h <= dy || road[dy][dx] == '0')continue;
		dfs(dy, dx, h, w);
	}
}

int main() {
	while (1) {
		int h, w, cnt = 0;
		string k;
		cin >> w >> h;
		if (h == 0 || w == 0)break;
		road.resize(h);
		road.assign(h, "");
		rep(i, h)rep(j, w) {
			cin >> k;
			road[i] += k;
		}
		rep(i, h)rep(j, w) {
			if (road[i][j] == '0')continue;
			dfs(i, j, h, w);
			cnt++;
		}
		cout << cnt << endl;
	}
	return 0;
}

C++: #include <math.h>

#include <algorithm>
#include <bitset>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

void dfs(int x, int y, vector<vector<int>>& field) {
  if (0 <= x && x < field.size() && 0 <= y && y < field[0].size()) {
    if (field[x][y] == 0) {
      return;
    }
  } else {
    return;
  }

  field[x][y] = 0;
  for (int dx = -1; dx <= 1; dx++) {
    for (int dy = -1; dy <= 1; dy++) {
      dfs(x + dx, y + dy, field);
    }
  }
  return;
}

int main() {
  vector<int> ans_v;
  for (int k = 0; k < 100; k++) {
    int w, h;
    cin >> w >> h;
    if (w == 0) {
      break;
    }

    vector<vector<int>> field(h, vector<int>(w));
    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) {
          dfs(i, j, field);
          ans++;
        }
      }
    }

    ans_v.push_back(ans);
  }

  for (int i = 0; i < ans_v.size(); i++) {
    cout << ans_v[i] << endl;
  }

  return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <array>
#include <queue>
#include <deque>
#include <set>
#include <list>
#include <map>
#include <stack>
#include <utility>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <climits>
#include <cfloat>
#include <bitset>
#include <random>
#include <functional>
#include <sstream>
#include <iomanip>

using namespace std;

#define rep(i, n) for(int i=0; i<(n); i++)
#define FOR(i, m, n) for(int i=(m);i<(n);i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define SORT(x) sort((x).begin(),(x).end())
#define REVE(x) reverse((x).begin(),(x).end())
#define mp make_pair
#define pb push_back

typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<vector<int>> VVI;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<bool> VB;

int h, w;
VVI c(55, VI(55));
vector<VB> visited(55, VB(55));

int d1[] = { 1,1,1,0,-1,-1,-1,0 };
int d2[] = { -1,0,1,1,1,0,-1,-1 };

void dfs(int x, int y) {
	if (x < 0 || y < 0 || x >= h || y >= w || c[x][y] == 0 || visited[x][y])return;
	visited[x][y] = true;
	rep(i, 8)dfs(x + d1[i], y + d2[i]);
}


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

		rep(i, 52)rep(j, 52) { c[i][j] = 0; visited[i][j] = false; }
		rep(i, h)rep(j, w)cin >> c[i][j];



		int cnt = 0;
		rep(i, h)rep(j, w) {
			//printf("visited[%d][%d]=%s", i, j, visited[i][j] ? "true" : "false");
			if (c[i][j] == 1 && visited[i][j] == false) {
				//puts(" -> o");
				dfs(i, j);
				cnt++;
			}
			else {
				//puts(" -> x");
			}
		}
		cout << cnt << endl;
	}
}