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 main(void){
	int w, h;
	int dx[] = {1,1,0,-1,-1,-1,0,1};
	int dy[] = {0,1,1,1,0,-1,-1,-1};
	while(true){
		cin >> w >> h;
		if(!w&&!h)break;
		vector<int> c[h+2];
		c[0].resize(w+2);
		c[h+1].resize(w+2);
		for(int i = 1; i <= h; ++i){
			c[i].resize(w+2);
			for(int j = 1; j <= w; ++j){
				cin >> c[i][j];
			}
		}
		int ans = 0;
		queue<pair<int, int>> q;
		for(int i = 1; i <= h; ++i){
			for(int j = 1; j <= w; ++j){
				if(c[i][j] == 1){
					q.emplace(i,j);
					ans++;
					c[i][j] = 0;
					while(!q.empty()){
						int x = q.front().first;
						int y = q.front().second;
						q.pop();
						for(int dir = 0; dir < 8; ++dir){
							int nx = x + dx[dir];
							int ny = y + dy[dir];
							if(c[nx][ny]==1){
								q.emplace(nx,ny);
								c[nx][ny]=0;
							}
						}
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
#define int long long
#define double long double
#define fo(a,b) for(int a=0;a<b;a++)
#define Sort(a) sort(a.begin(),a.end())
#define rev(a) reverse(a.begin(),a.end())
#define fi first
#define se second
#define th third
#define sz size()
#define bgn begin()
#define en end()
#define pb push_back
#define pp() pop_back()
#define V vector
#define P pair
#define yuko(a) setprecision(a)
#define uni(a) a.erase(unique(a.begin(),a.end()),a.end())
#define Q queue
#define pri priority_queue
#define Pri priority_queue<int,vector<int>,greater<int>>
#define PriP priority_queue<P<int,int>,vector<P<int,int>>,greater<P<int,int>>>
#define ff first.first
#define fs first.second
#define sf second.first
#define ss second.second
#define all(a)  (a).begin(),(a).end()
#define elif else if

const int dx[8]={1,1,1,0,0,-1,-1,-1};
const int dy[8]={-1,0,1,-1,1,-1,0,1};
int H,W;
V<V<int>> field;

void dfs(int h,int w){
  field[h][w]=0;
  fo(dir,8){
    int nh=h+dx[dir];
    int nw=w+dy[dir];
    if(nh<0||nw<0||nh>=H||nw>=W)
      continue;
    if(field[nh][nw]==0)
      continue;
    dfs(nh,nw);
  }
}
signed main(){
  while(cin>>W>>H){
    if(H==0||W==0)
      break;
    field.assign(H,V<int> (W,0));
    fo(h,H){
      fo(w,W){
        cin>>field[h][w];
      }
    }
    int ans=0;
    fo(h,H){
      fo(w,W){
        if(field[h][w]==0)
          continue;
        dfs(h,w);
        ++ans;
      }
    }
    cout<<ans<<endl;
  }
}

C++: # include <iostream>

const int MAX_W = 50;
const int MAX_H = 50;

int w, h;

int c[MAX_H][MAX_W];

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

void dfs(int x, int y) {
	c[y][x] = 0;
	for (int i = 0; i < 8; ++i) {
		int nx = x + dx[i];
		int ny = y + dy[i];
		if (nx >= 0 && ny >= 0 && nx < w&&ny < h&&c[ny][nx] == 1)
			dfs(nx, ny);
	}
}

int solve() {
	int sum = 0;
	for (int i = 0; i < h; ++i) {
		for (int j = 0; j < w; ++j) {
			if (c[i][j] == 1) {
				++sum;
				dfs(j, i);
			}
		}
	}
	return sum;
}

int main() {
	while (1) {
		std::cin >> w >> h;
		if ((w + h) == 0)
			break;
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j)
				std::cin >> c[i][j];
		}
		int res = solve();
		std::cout << res << std::endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

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

void dfs(vector< vector<int> > &isisland, vector< vector<bool> > &vvisited, int x, int y) {
    // ここで島の色塗りを行う
    vvisited[y][x] = true;
    for(int i = 0; i < 8; ++i) {
        int nx = x+dx[i]; int ny = y+dy[i];
        if(nx >= 0 && nx < w && ny >= 0 && ny < h) {
            if(isisland[ny][nx] == 1 && !vvisited[ny][nx]) {
                dfs(isisland, vvisited, nx, ny);
            }
        }
    }
}

int main() {
    while(1) {
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        else {
            vector< vector<int> > island(h, vector<int>(w, 0));
            for(int i = 0; i < h; ++i) {
                for(int j = 0; j < w; ++j) {
                    cin >> island[i][j];
                }
            }

            vector< vector<bool> > visited(h, vector<bool>(w, false));
            int total = 0;
            for(int i = 0; i < h; ++i) {
                for(int j = 0; j < w; ++j) {
                    if(island[i][j] == 1 && !visited[i][j]) {
                        total++;
                        dfs(island, visited, j, i);
                    }
                }
            }
            cout << total << endl;
        }
    }    
}