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 W,H, grid[50][50];

int dfs(int sy, int sx){
	if(grid[sy][sx]==0)return 0;

	stack<pair<int,int>> S;
	S.push( {sy,sx} );

	while(S.size()){
		pair<int,int> u=S.top();	S.pop();
		grid[u.first][u.second]=0;

		for(int dy=-1; dy<=1; dy++){
			for(int dx=-1; dx<=1; dx++){
				int nY=u.first+dy, nX=u.second+dx;
				if(nY<0||nX<0||nY>=H||nX>=W)continue;
				else if(grid[nY][nX]==0)continue;

				S.push( {nY,nX} );
			}
		}
	}
	return 1;
}

int main(){
	while((cin>>W>>H)&&W&&H){
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				cin>>grid[i][j];
			}
		}

		int ans=0;
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				ans+=dfs(i,j);
			}
		}
		cout<<ans<<endl;
	}

	return 0;
}

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

#define REP(i,n) for(int i=0;i<n;++i)
#define RREP(i,n) for(int i=n-1;i>=0;--i)
#define FOR(i,m,n) for(int i=m;i<n;++i)
#define RFOR(i,m,n) for(int i=m-1;i>=n;--i)
#define ALL(v) v.begin(),v.end()
#define PB(a) push_back(a)
#define INF 1000000001
#define MOD 1000000007

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

using namespace std;

void dfs(vector<vector<int> > &v,int x,int y) {
	v[x][y] = 0;
	REP(i, 8) {
		int nx = x + dx[i], ny = y+dy[i];
		if (nx >= 0 && ny >= 0 && nx < v.size() && ny < v[0].size() && v[nx][ny] == 1) {

			dfs(v, nx, ny);
		}
	}
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	while (1) {
		int w, h, cnt = 0;
		cin >> w >> h;
		if (!w) break;
		vector<int> dm(w);
		vector<vector<int> > v(h, dm);
		REP(i, h)REP(j, w) cin >> v[i][j];
		REP(i, h)REP(j, w)
			if (v[i][j] == 1) {
				dfs(v, i, j);
				cnt++;
			}
		cout << cnt << endl;
	}
	return 0;
}

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

using namespace std;
typedef long long ll;

#define LOOP(i, a, n) for (int i = (int)a; i < (int)n; ++i)
#define REP(i, n) LOOP(i, 0, n)
#define MOD 1000000007
#define INF 1000000000
#define PI 3.14159265359
#define MAX_W 50
#define MAX_H 50

int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1}, dy[8] = {-1, 1, 0, 0, -1, 1, -1, 1}, board[MAX_H][MAX_W], w, h;

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

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

  return;
}

int main(void) {
  vector<int> ans;

  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0) {
      break;
    }
    fill(board[0], board[49], 0);
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> board[i][j];
        //cout << "i:" << i << " j:" << j << endl;
      }
    }

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

    ans.push_back(res);
  }

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