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;
int main(){
	int w,h,ct,num2;
	while(cin>>w>>h){
		ct=0;
		if(w==0&&h==0) break;
		int c[w+2][h+2];
		for(int i=0;i<w+2;i++){
			for(int j=0;j<h+2;j++){
				c[i][j]=0;
			}
		}
		for(int i=0;i<w;i++){
			for(int j=0;j<h;j++){
				cin>>c[i+1][j+1];
			}
		}
		for(int i=0;i<w+2;i++){
			for(int j=0;j<h+2;j++){
				if(c[i][j]==1){
					c[i][j]=2;
					do{
						num2=0;
						for(int ii=1;ii<w+1;ii++){
							for(int jj=1;jj<h+1;jj++){
								if(c[ii][jj]==2&&c[ii-1][jj-1]==1){
									c[ii-1][jj-1]=2;
									num2++;
								}
								else if(c[ii][jj]==2&&c[ii-1][jj]==1){
									c[ii-1][jj]=2;
									num2++;
								}
								else if(c[ii][jj]==2&&c[ii-1][jj+1]==1){
									c[ii-1][jj+1]=2;
									num2++;
								}
								else if(c[ii][jj]==2&&c[ii][jj-1]==1){
									c[ii][jj-1]=2;
									num2++;
								}
								else if(c[ii][jj]==2&&c[ii][jj+1]==1){
									c[ii][jj+1]=2;
									num2++;
								}
								else if(c[ii][jj]==2&&c[ii+1][jj-1]==1){
									c[ii+1][jj-1]=2;
									num2++;
								}
								else if(c[ii][jj]==2&&c[ii+1][jj]==1){
									c[ii+1][jj]=2;
									num2++;
								}
								else if(c[ii][jj]==2&&c[ii+1][jj+1]==1){
									c[ii+1][jj+1]=2;
									num2++;
								}
							}
						}
					} while(num2>0);
					ct++;
					for(int ii=0;ii<w+2;ii++){
						for(int jj=0;jj<h+2;jj++){
							if(c[ii][jj]==2){
								c[ii][jj]=0;
							}
						}
					}
				}
			}
		}
		cout<<ct<<endl;		
	}
    return 0;
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160

#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;

int w, h;
bool F[60][60];

struct point {
  int x, y;
};

void init() {
  fill(&F[0][0], &F[0][0]+60*60, 0);
  for (int i=1; i<=h; i++) {
    for (int j=1; j<=w; j++) {
      int temp;
      scanf("%d", &temp);
      F[i][j] = (bool) temp;
    }
  }
}

int solve() {
  int ans = 0;
  for (int i=1; i<=h; i++) {
    for (int j=1; j<=w; j++) {
      if (F[i][j]) {
	stack<point> S;
	point p = { i, j };
	S.push(p);
	while (! S.empty()) {
	  p = S.top();
	  S.pop();
	  F[p.x][p.y] = false;
	  for (int i=-1; i<=1; i++) {
	    for (int j=-1; j<=1; j++) {
	      point q = { p.x + i , p.y + j };
	      if (F[q.x][q.y]) {
		S.push(q);
	      }
	    }
	  }
	}
	ans++;
      }
    }
  }
  return ans;
}

int main() {
  while(scanf("%d%d", &w, &h) != EOF && w && h) {
    init();
    printf("%d\n", solve());
  }
}

C++: #include <bits/stdc++.h>
using namespace std;

int state[100][100];
bool reached[100][100];
int w,h;

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

void dfs(int x, int y) {

	if(x==0 || w<x || y==0 || h<y || state[x][y] == 0)
		return;
	if(reached[x][y] == true)
		return;

	reached[x][y]=true;

	for(int i=0; i<8; i++) {
		dfs(x+dx[i],y+dy[i]);
	}
}

int main() {

	while(cin >> w >> h,w){
		memset(state, 0, sizeof(state));
		memset(reached, false, sizeof(reached));
		for(int i=1; i<=h; i++) {
			for(int j=1; j<=w; j++) {
				cin >> state[i][j];
			}
		}
		int count=0;
		for(int i=1; i<=w; i++) {
			for(int j=1; j<=h; j++) {
				if(reached[i][j]==true || state[i][j] == 0)
					continue;
				else{
					dfs(i,j);
					count++;
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}

C++: #include <iostream>
#define MAXSIZE 50

using namespace std;

int cnt;
int island[MAXSIZE][MAXSIZE];

int check(int x, int y, int w, int h);

int main(int argc, char const *argv[])
{
	int w, h;

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

		/* processor */	
		cnt = 0;

		for(int x = 0; x < w; x++)
		{
			for(int y = 0; y < h; y++)
			{
				if(check(x, y, w, h))
				{
					cnt++;
				}
			}
		}

		cout << cnt << endl;
	}	

	return 0;
}

int check(int x, int y, int w, int h)
{
	int dx[] = {-1, 0, 1, 1, 1, 0, -1, -1};
	int dy[] = {-1, -1, -1, 0, 1, 1, 1, 0};

	if(island[y][x])
	{
		island[y][x] = 0;

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

			check(x + dx[i], y + dy[i], w, h);
		}

		return 1;
	}

	return 0;
}

C++: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "iomanip"
#include "random"

using namespace std;

const long long int MOD = 1000000007;
const long double EPS = 0.00000001;
const long double PI = 3.1415926535897932384626433;

long long int N, M, K, H, W, L, R;


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

	cin >> W>>H;
	while (H) {
		N = 0;
		vector<vector<int>>field(H + 2, vector<int>(W + 2, 0));
		for (int i = 1; i <= H; i++) {
			for (int j = 1; j <= W; j++) {
				cin >> field[i][j];
			}
		}
		for (int i = 1; i <= H; i++) {
			for (int j = 1; j <= W; j++) {
				queue<pair<int, int>>Q;
				if (field[i][j]) {
					field[i][j] = 0;
					Q.push({ i,j });
					N++;
					while (!Q.empty()) {
						int cy = Q.front().first;
						int cx = Q.front().second;
						//cout << N << " " << cy << " " << cx << endl;
						Q.pop();
						
						for (int k = -1; k <= 1; k++) {
							for (int l = -1; l <= 1; l++) {
								if (field[cy + k][cx + l]) {
									field[cy + k][cx + l] = 0;
									Q.push({ cy + k,cx + l });
								}
							}
						}
					}
				}
			}
		}
		cout << N << endl;
		cin >> W >> H;
	}
	return 0;
}