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;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define REP(i, d, n) for(int i=(d); i<(n); ++i)
#define all(v) v.begin(), v.end()

const int x[] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int y[] = {-1, 0, 1, -1, 1, -1, 0, 1};

int dfs(int i, int j, vector<vector<int> >& graph, vector<vector<int> >& visit, const int h, const int w){
    if(visit[i][j] == 1 || graph[i][j] == 0){
        return 0;
    }
    visit[i][j] = 1;
    rep(k, 8){
        int di = i + x[k];
        int dj = j + y[k];
        if(di < 0 || dj < 0 || di >= h || dj >= w){
            continue;
        }
        if(visit[di][dj] == 1){
            continue;
        }
        dfs(di, dj, graph, visit, h, w);
    }
    return 1;
}

int main() {

    while(true){
        int w, h;
        cin >> w >> h;
        if(w == 0 && h == 0){
            break;
        }
        vector<vector<int> > graph(h, vector<int>(w)); 
        rep(i, h){
            rep(j, w){
                cin >> graph[i][j];
            }
        }
        vector<vector<int> > visit(h, vector<int>(w));
        int ans = 0;
        rep(i, h){
            rep(j, w){
                ans += dfs(i, j, graph, visit, h, w);
            }
        }
        cout << ans << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int x[50][50], col[50][50], h, w, c, i, j, k, l, p, q;
queue<int>Q, R;
int main() {
	while (true) {
		c = 0;
		memset(col, 0, sizeof(col));
		cin >> w >> h;
		if (h == 0) { break; }
		for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { cin >> x[i][j]; } }
		for (i = 0; i < 50; i++) {
			for (j = 0; j < 50; j++) {
				if (x[i][j] == 1 && col[i][j] == 0) {
					c++;
					col[i][j] = c;
					Q.push(i * 100 + j);
					while (!Q.empty()) {
						while (!Q.empty()) {
							p = Q.front() / 100;
							q = Q.front() % 100;
							for (k = -1; k < 2; k++) {
								for (l = -1; l < 2; l++) {
									if (x[p + k][q + l] == 1 && col[p + k][q + l] == 0) {
										col[p + k][q + l] = c;
										R.push((p + k) * 100 + q + l);
									}
								}
							}
							Q.pop();
						}
						while (!R.empty()) {
							Q.push(R.front());
							R.pop();
						}
					}
				}
			}
		}
		cout << c << endl;
	}
}

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() {

	memset(state, 0, sizeof(state));
	memset(reached, false, sizeof(reached));

	cin >> w >> h;
	for(int i=1; i<=w; i++) {
		for(int j=1; j<=h; 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 <bits/stdc++.h>
using namespace std;
#define FOR(i,o,n) for(long long i = o;i<n;i++)
#define oneforall ios::sync_with_stdio(false);cin.tie(0);
#define all(v) (v).begin(),(v).end()
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define int long long 
const long long inf=1e18;
void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
void out(){cout << "\n";} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);}
typedef vector<int> vi;
typedef vector<long long> vl;
typedef long long ll;
typedef vector<pair<long, long > > vpll;
typedef vector<pair<int, int > > vpii;
#define FORR(x,arr) for(auto& x:arr)
#define ZERO(a) memset(a,0,sizeof(a))





//3111111111111111111111111111111

	int x,x1;
	int morty[310][310];
	int tmpx[3] = {-1,1,0};
	void dfs(int tmp,int tmp1){
		morty[tmp][tmp1] = 0;
		FOR(i,0,3)FOR(i1,0,3){
			int nowtmp = tmp+tmpx[i];
			int nowtmp1 = tmp1+tmpx[i1];
			if(!(nowtmp == tmp and nowtmp1 == tmp1) and nowtmp>=0 and nowtmp<=x-1 and nowtmp1>=0 and nowtmp1<=x1-1 and morty[nowtmp][nowtmp1] == 1){
				dfs(nowtmp,nowtmp1);
			}
		}
	}




	void solve(){
		
		while(1){
			
			in(x1,x);
			if(x == 0 and x1 == 0)break;
			FOR(i,0,x)FOR(i1,0,x1){
				ini(tmp);
				if(tmp == 0){
					morty[i][i1] = 0;
				}
				else{
					morty[i][i1] = 1;
				}
			}
			int rick = 0;
			//FOR(i,0,x){
			//	FOR(i1,0,x1){
				//cout<<morty[i][i1]<<" ";			
			//	}
				//out();
			//}
			FOR(i,0,x)FOR(i1,0,x1){
				if(morty[i][i1] == 1){
					dfs(i,i1);
					rick++;
				}
			}
			out(rick);
		}








		
		
	}























int32_t main() {
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall 
	oneforall

	solve();
	


	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int w,h,c[100][100],ans;
pair<int,int> movemy[8]={{0,1},{1,0},{1,1},{-1,0},{0,-1},{-1,1},{1,-1},{-1,-1}};


void land(int y,int x){
	if(y<0||y>h-1||x<0||x>w-1||c[y][x]==0)return;
	c[y][x]=0;
	for(int i=0;i<8;i++){
		land(y+movemy[i].first,x+movemy[i].second);
	}
}

int main(){
	while (1){
		ans=0;
		cin>>w>>h;
		if(w==0||h==0)break;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin>>c[i][j];
				//cout<<"AAA"<<endl;
			}
		}
		//cout<<"AAA"<<endl;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(c[i][j]){land(i,j);ans++;}
				
			}
		}
		cout<<ans<<endl;
	}
}