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;
typedef long long ll;
// #define int long long

template <class T>
bool INRANGE(T x, T a, T b) { return a <= x && x <= b; }
template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
                      << " " << __FILE__ << endl;

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int dh[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dw[] = {-1, 0, 1, -1, 1, -1, 0, 1};

vvi m;
int cnt;
vector<vector<bool>> visited;

void next(int h, int w) {
    if (m[h][w] == 0) return;
    if (visited[h][w]) return;
    visited[h][w] = true;

    REP(i, 8) {
        int nh = h + dh[i];
        int nw = w + dw[i];
        if (!INRANGE(nh, 0, (int)m.size()-1)) continue;
        if (!INRANGE(nw, 0, (int)m[0].size()-1)) continue;
        next(nh, nw);
    }
}

void dfs(int h, int w) {
    if (m[h][w] == 0) return;
    if (visited[h][w]) return;
    visited[h][w] = true;

    cnt++;
    REP(i, 8) {
        int nh = h + dh[i];
        int nw = w + dw[i];
        if (!INRANGE(nh, 0, (int)m.size()-1)) continue;
        if (!INRANGE(nw, 0, (int)m[0].size()-1)) continue;
        next(nh, nw);
    }
}

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

    while (true)
    {
        int w, h;
        cin >> w >> h;
        if (w == 0 and h == 0) break;

        m = vvi();
        REP(_, h) {
            vi l(w);
            //cin >> l;
            REP(i, w) cin >> l[i];
            m.emplace_back(l);
        }

        visited = vector<vector<bool>>(h, vector<bool>(w, false));
        cnt = 0;
        REP(hi, h) {
            REP(wi, w) {
                dfs(hi, wi);
            }
        }

        cout << cnt << endl;
    }
    
    


    return 0;
}

C++: #include <stdio.h>
#include <stack>

#define MAX_W 50
#define MAX_H 50
#define LAND 1
#define SEA 0
int map[MAX_H][MAX_W];
int h, w;
using namespace std;
stack<int> x, y;

bool input() {
  int i, j;

  scanf("%d %d", &w, &h);
  if (w == 0 && h == 0) return false;
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      scanf("%d", &map[i][j]);
    }
  }

  return true;
}

int main() {
  int i, j, cnt;

  while (input()) {
    cnt = 0;
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        if (map[i][j] == LAND) {
          cnt++;
          x.push(j);
          y.push(i);
          while (!x.empty() && !y.empty()) {
            int a = x.top();
            x.pop();
            int b = y.top();
            y.pop();
            if (a < 0 || w <= a || b < 0 || h <= b || map[b][a] == SEA)
              continue;
            map[b][a] = SEA;
            x.push(a - 1);
            y.push(b - 1);
            x.push(a - 1);
            y.push(b);
            x.push(a - 1);
            y.push(b + 1);
            x.push(a);
            y.push(b - 1);
            x.push(a);
            y.push(b + 1);
            x.push(a + 1);
            y.push(b - 1);
            x.push(a + 1);
            y.push(b);
            x.push(a + 1);
            y.push(b + 1);
          }
        }
      }
    }
    printf("%d\n", cnt);
  }

  return 0;
}

C++: #include <iostream>
using namespace std;

void input(int **, int, int);
int howManyIslands(int **, int, int);
void howBigIslands(int **, bool **, int, int, int, int);

int main()
{
	int **island, w, h;
	while(true){
		cin >> w >> h;
		if(w==h && h==0) break;
		island = new int*[w];
		for(int i=0; i<w; i++)
			island[i] = new int[h];
		input(island, w, h);
		cout << howManyIslands(island, w, h) << endl;
		for(int i=0; i<w; i++)
			delete[] island[i];
		delete[] island;
	}
	return 0;
}

void input(int **island, int w, int h)
{
	char buf[60];
	cin.clear();cin.ignore();
	for(int i=0; i<h; i++){
		cin.getline(buf, sizeof(buf));
		int j=0, k=0;
		while(buf[j] != '\0'){
			if(buf[j] != ' '){
				switch(buf[j]){
				case '0':
					island[k][i] = 0;
					k++;
					break;
				case '1':
					island[k][i] = 1;
					k++;
					break;
				}
				if(w==k) break;
			}
			j++;
		}
	}
}

int howManyIslands(int **island, int w, int h)
{
	bool **check;
	int ans = 0;
	check = new bool*[w];
	for(int i=0; i<w; i++){
		check[i] = new bool[h];
		for(int j=0; j<h; j++)
			check[i][j] = false;
	}
	for(int i=0; i<w; i++){
		for(int j=0; j<h; j++){
			if(check[i][j])continue;
			if(island[i][j]){
				ans++;
				howBigIslands(island, check, i, j, w, h);
			} else {
				check[i][j] = true;
			}
		}
	}
	for(int i=0; i<w; i++)
		delete[] check[i];
	delete[] check;

	return ans;
}

void howBigIslands(int **island, bool **check, int x, int y, int w, int h)
{
	if(check[x][y]) return;
	enum __DIR{ UPPER_LEFT, UP, UPPER_RIGHT, LEFT, RIGHT, LOWER_LEFT,
		UNDER, LOWER_RIGHT, DIR_END};
	int periphe[DIR_END][2]={
			{-1, -1}, {0, -1}, {1, -1},
			{-1, 0}, {1, 0},
			{-1, 1}, {0, 1}, {1, 1}
	};
	check[x][y] = true;
	for(int i=0; i<DIR_END; i++){
		if(x+periphe[i][0] < 0 || w-1 < x+periphe[i][0] ||
			y+periphe[i][1] < 0 || h-1 < y+periphe[i][1]) continue;
		if(island[x+periphe[i][0]][y+periphe[i][1]]){
			if(!check[x+periphe[i][0]][y+periphe[i][1]])
				howBigIslands(island, check,
				x+periphe[i][0], y+periphe[i][1], w, h);
		}
	}
}