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;
using ll = long long;

void dfs(const vector<vector<bool>> &field, vector<vector<bool>> &visit, int x, int y, int w, int h) {
    visit[y][x] = true;
    for (int dx = -1; dx <= 1; dx++) {
        for (int dy = -1; dy <= 1; dy++) {
            if (dx == 0 && dy == 0)
                continue;
            int nx = x + dx;
            int ny = y + dy;

            if (0 <= nx && nx < w && 0 <= ny && ny < h && field[ny][nx] && !visit[ny][nx]) {
                dfs(field, visit, nx, ny, w, h);
            }
        }
    }
}

void solve() {
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0)
        exit(0);

    vector<vector<bool>> field(h, vector<bool>(w));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            int c;
            cin >> c;
            if (c)
                field[i][j] = true;
            else
                field[i][j] = false;
        }
    }

    int ans = 0;
    vector<vector<bool>> visit(h, vector<bool>(w, false));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (field[i][j] && !visit[i][j]) {
                dfs(field, visit, j, i, w, h);
                ans++;
            }
        }
    }

    cout << ans << endl;
}

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

    while (true)
        solve();

    return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
using ll = long long;

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

int h,w;
void dfs(vector<vector<int>> &s,int x,int y){
  if(x<0 || x>=h || y<0 || y>=w) return;
  if(!s[x][y]) return;
  s[x][y] = 0;
  rep(i,8) dfs(s,x+dx[i],y+dy[i]);
}
  
  
int main(){
  while(1){   
    cin>>w>>h; 
    if(!h) break;
    vector<vector<int>> s(h,vector<int>(w));
    rep(i,h)rep(j,w) cin>>s[i][j];
     
    int ans = 0;
    rep(k,250){
      rep(i,h){
        rep(j,w){
          if(s[i][j]){
            dfs(s,i,j);
            ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
}

C++: #include "bits/stdc++.h"
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define FORR(i, a, b) for(int i=(b)-1; i>=(a); --i)
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long LL;
typedef long double LD;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<bool> VB;
typedef vector<vector<int> > VVI;
typedef vector<vector<bool> > VVB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
const LL MOD=1000000007ll;
const LL INF=2000000000ll;

long power(long a,long b){return b?power(a*a%MOD,b/2)*(b%2?a:1)%MOD:1;}
long comb(int N,int K)
{
	long x=1,y=1;
	for(int i=1;i<=K;i++)
	{
		x=x*(N-i+1)%MOD;
		y=y*i%MOD;
	}
	return x*power(y,MOD-2)%MOD;
}

long hcomb(int N, int K){
    return comb(N+K-1, N-1);
}

void dfs(const vector<vector<int> > &G, int v, vector<int> &seen) {
    seen[v] = true; // v を訪問済にする

    // v から行ける各頂点 next_v について
    for (auto next_v : G[v]) { 
        if (seen[next_v]) continue; // next_v が探索済だったらスルー
        dfs(G, next_v, seen); // 再帰的に探索
    }
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(10);

    int w, h;

    cin >> w >> h;

    while (w || h){
        VVI c(h, VI(w));
        VVI G(h*w);
        REP(i,h) REP(j,w) cin >> c[i][j];
        VI seen(h*w,false);
        REP(i,h) REP(j,w){
            if (c[i][j]){
                if (i != 0) if (c[i-1][j]) G[i*w+j].push_back((i-1)*w+j);
                if (i != h-1) if (c[i+1][j]) G[i*w+j].push_back((i+1)*w+j);
                if (j != 0) if (c[i][j-1]) G[i*w+j].push_back(i*w+j-1);
                if (j != w-1) if (c[i][j+1]) G[i*w+j].push_back(i*w+j+1);
                if (i != 0 && j != 0) if (c[i-1][j-1]) G[i*w+j].push_back((i-1)*w+j-1);
                if (i != 0 && j != w-1) if (c[i-1][j+1]) G[i*w+j].push_back((i-1)*w+j+1);
                if (i != h-1 && j != 0) if (c[i+1][j-1]) G[i*w+j].push_back((i+1)*w+j-1);
                if (i != h-1 && j != w-1) if (c[i+1][j+1]) G[i*w+j].push_back((i+1)*w+j+1);
            }
            else {
                seen[i*w+j] = true;
            }
        }

        int ans = 0;
        REP(i,h*w){
            if (seen[i] == false){
                dfs(G,i,seen);
                ans++;
            }
        }

        cout << ans << endl;

        cin >> w >> h;
    }
}

C++: #include<iostream>
using namespace std;
int map[114][514];
int h, w;
void dfs(int i, int j) {
	map[i][j] = 0;
	if (i < h) {
		if (j < w) {
			if (map[i - 1][j - 1] == 1)dfs(i - 1, j - 1);
		}
		if(map[i - 1][j] = 1)dfs(i - 1, j);
		if (j > 0) {
			if (map[i - 1][j + 1] == 1)dfs(i - 1, j + 1);
		}
	}
	if (j < w) {
		if (map[i][j - 1] == 1)dfs(i, j - 1);
	}
	if (j > 0) {
		if (map[i][j + 1] == 1)dfs(i, j + 1);
	}
	if (i > 0) {
		if (j < w) {
			if (map[i + 1][j - 1] == 1)dfs(i + 1, j - 1);
		}
		if (map[i + 1][j] = 1)dfs(i + 1, j);
		if (j > 0) {
			if (map[i + 1][j + 1] == 1)dfs(i + 1, j + 1);
		}
	}
	return;
}
int main() {
	while (cin >> h >> w) {
		if (!h && !w)return 0;
		int INF = 0;
		for (int i = 0; i < h; i++)for (int j = 0; j < w; j++)cin >> map[i][j];
		for (int i = 0; i < h; i++)for (int j = 0; j < w; j++)if (map[i][j] == 1) {
			INF++;
			dfs(i, j);
		}
		cout << INF << endl;
	}
}