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>
#include <algorithm> // min()
#include <string>
#include <vector>
#include <climits> // INT_MAX
#include <cmath> // ceil()
#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;
using ll = long long;
const int INF = INT_MAX;
using Graph = vector<vector<int>>;
using Vi = vector<int>;
using Vpii = vector<pair<int, int>>;
using Pii = pair<int, int>;


vector<bool> seen;
int ans = 0;
int land = 0;
bool is_tree = false;
Pii region;


void dfs(Graph &g, int x, int y) {
    if (x < 0 || x >= region.first) return;
    if (y < 0 || y >= region.second) return;

    if (g[y][x] == 0) return;

    if (g[y][x] == 1) {
        g[y][x] = 0;
        land--;
    }

    dfs(g, x + 1, y);
    dfs(g, x - 1, y);
    dfs(g, x, y + 1);
    dfs(g, x, y - 1);
    dfs(g, x + 1, y + 1);
    dfs(g, x + 1, y - 1);
    dfs(g, x - 1, y + 1);
    dfs(g, x - 1, y - 1);
}


int main() {
    Pii start;
    Vi answers;
    vector<Graph> graphs;
    int w, h, c;

    while (true)
    {
        Graph graph;
        land = 0;
        ans = 0;

        cin >> w >> h;
        if (w == 0 && h == 0) break;
        region = { w, h };

        rep(i, h) {
            Vi temp;
            rep(j, w) {
                cin >> c;
                temp.emplace_back(c);
                land += c;
            }
            graph.emplace_back(temp);
        }

        while (land) {
            int x;
            rep(y, h) {
                auto vec = graph[y];
                auto iter = find(vec.begin(), vec.end(), 1);
                if (iter != vec.end()) {
                    x = distance(vec.begin(), iter);
                    start = { x, y };
                    break;
                }
            }
            dfs(graph, start.first, start.second);
            ans++;
        }

        answers.emplace_back(ans);

    }

    for (auto&& a : answers) {
        cout << a << endl;
    }

}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int w, h;
int c[100][100];
int ans;

void solve(int y, int x){
	c[y][x] = 0;

	for (int i = -1; i <= 1; i++){
		for (int j = -1; j <= 1; j++){
			int ny = y + i, nx = x + j;
			if (ny >= 0 && ny < h&&nx >= 0 && nx < w){
				if (c[ny][nx] == 1)solve(ny, nx);
			}
		}
	}
}

int main(){
	while (cin >> w >> h&&w + h){
		REP(i, h){
			REP(j, w){
				cin >> c[i][j];
			}
		}
		ans = 0;
		REP(i, h){
			REP(j, w){
				if (c[i][j] == 1){
					solve(i, j);
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <string.h>

using namespace std;
char Field[52][52];
int w, h;

void DFS(int y, int x)
{
	Field[y][x] = 0;
	for(int i = -1; i <= 1; ++i)
	{
		for(int j = -1; j <= 1; ++j)
		{
			int X = x + j;
			int Y = y + i;
			if((0 < X && X <= w) && (0 < Y && Y <= h) && Field[Y][X] == '1')
			{
				DFS(Y, X);
			}
		}
	}
}

void solve()
{
	while(cin >> w >> h, w || h)
	{
		memset(Field, 0, sizeof(Field));
		for(int i = 1; i <= h; ++i)
		{
			for(int j = 1; j <= w; ++j)
			{
				cin >> Field[i][j];
			}
		}

		int count = 0;
		for(int i = 1; i <= h; ++i)
		{
			for(int j = 1; j <= w; ++j)
			{
				if(Field[i][j] == '1')
				{
					count++;
					DFS(i, j);
				}
			}
		}
		cout << count << endl;
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include <bits/stdc++.h>
using namespace std;
#include <bits/stdc++.h>
using namespace std;
struct Graph{
  using ver_t=int32_t; using edge_t=pair<ver_t,ver_t>;
  vector<vector<ver_t>> field;
  const edge_t dr[8]={{1,1},{1,0},{1,-1},{0,1},{0,-1},{-1,1},{-1,0},{-1,-1}};
  void dfs(edge_t frm){
    // preorder
    field[frm.first][frm.second]=0;
    for(const auto& v:dr){
      auto x=frm.first+v.first; auto y=frm.second+v.second;
      if(x>=0&&x<field.size()&&y>=0&&y<field.front().size()&&field[x][y]==1){
        dfs(make_pair(x,y));
      }
    }
    // postorder
  }
  // void bfs(ver_t frm){
  //   seen[frm]=0;
  //   deque<ver_t> que(1,frm); // dfs,bfs
  //   while(!que.empty()){
  //     auto u=que.front(); que.pop_front(); // bfs
  //     for(const auto& v:edge[u]){
  //       if(seen[v]==NO){
  //         seen[v]=seen[u]+1;
  //         que.push_back(v); // dfs,bfs
  //       }
  //     }
  //   }
  // }
};
int32_t main(){
  Graph g;
  // size_t n; cin>>n;
  // for(size_t i=0; i<n; ++i){
  size_t w,h; cin>>w>>h;
  while(w!=0||h!=0){
    g.field.resize(h);
    for(auto&& x:g.field){
      x.resize(w);
      for(auto&& y:x) cin>>y;
    }
    size_t ret=0;
    for(size_t i=0; i<h; ++i){
      for(size_t j=0; j<w; ++j){
        if(g.field[i][j]==1){ ++ret; g.dfs(make_pair(i,j)); }
      }
    }
    cout<<ret<<'\n';
    cin>>w>>h;
  }
}