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 FOR( i, m, n )  for( int (i) = (m); (i) < (n); (i)++ )
#define REP( i, n )     FOR( i, 0, n )
#define ALL( a )        (a).begin(), (a).end()             

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

int field[50][50] = {};

int w, h;

void dfs( int x, int y );

int main() {

	while( cin >> w >> h, w ) {
		REP( i, h ) {
			REP( j, w ) {
				cin >> field[j][i];
			}
		}

		int cnt = 0;
		REP( i, h ) {
			REP( j, w ) {
				if( field[j][i] == 1 ) {
					cnt++;
					dfs( j, i );
				}
			}
		}

		cout << cnt << endl;

	}

}

void dfs( int x, int y ) {
	field[x][y] = 0;
	REP( i, 8 ) {
		if( x + dx[i] >= 0 && x + dx[i] < w &&
			y + dy[i] >= 0 && y + dy[i] < h &&
			field[x + dx[i]][y + dy[i]] == 1 ) {

			dfs( x + dx[i], y + dy[i] );

		}

	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0; i<n; i++)
typedef long long ll;
typedef long double ld;
typedef long long unsigned int llu;
ll MOD = 1000000007;
ll INF = 1000000009;

int c[55][55];
bool v[55][55];

void solve(){

    int w,h;
    vector<int> a;

    while(true){
        cin >> w >> h;
        if(w==0&&h==0) break;
        rep(i,h){
            rep(j,w){
                v[i][j]=false;
            }
        }
        rep(i,h) v[i][w]=true;
        rep(j,w) v[h][j]=true;
        v[h][w]=true;

        rep(i,h){
            rep(j,w){
                cin >> c[i][j]; 
                if(c[i][j]==0) v[i][j]=true;
            }
        }

        int ans=0;
            
            rep(i,h){
                rep(j,w){
                    if(!v[i][j]) {
                        stack<pair<int,int>> st;
                        st.push(make_pair(i,j));
                        while(!st.empty()){
                            int ti = st.top().first;
                            int tj = st.top().second;
                            st.pop();
                            v[ti][tj]=true;
                            
                            if(v[ti+1][tj]==false) st.push(make_pair(ti+1,tj));
                            if(v[ti][tj+1]==false) st.push(make_pair(ti,tj+1));
                            if(ti>0) {if(v[ti-1][tj]==false) st.push(make_pair(ti-1,tj));}
                            if(tj>0) {if(v[ti][tj-1]==false) st.push(make_pair(ti,tj-1));}
                            if(v[ti+1][tj+1]==false) st.push(make_pair(ti+1,tj+1));
                            if(ti>0) {if(v[ti-1][tj+1]==false) st.push(make_pair(ti-1,tj+1));}
                            if(tj>0) {if(v[ti+1][tj-1]==false) st.push(make_pair(ti+1,tj-1));}
                            if(ti>0&&tj>0) {if(v[ti-1][tj-1]==false) st.push(make_pair(ti-1,tj-1));}
                            
                        }
                        ans++;
                    }
                }
            }

        a.emplace_back(ans);
        


    }

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

}

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

C++: #include <iostream>
#include <vector>

using namespace std;



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

int W, H;
int field[50][50];

void dfs(int field[50][50], int sx, int sy) {

    field[sy][sx] = 0;

    for (int i = 0; i < 8; i++) {
        int nx = sx + dx[i];
        int ny = sy + dy[i];

        if (0 <= nx && nx < W && 0 <= ny && ny < H && field[ny][nx] == 1) {
            dfs(field, nx, ny);
        }
    }
}

int main(void) {
    
    while (true) {
        cin >> W >> H;

        if (W == 0 && H == 0) break;
        
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                cin >> field[i][j];
            }
        }

        int count = 0;

        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                if (field[i][j] == 1) {
                    dfs(field, j, i);
                    count++;
                }
            }
        }

        cout << count << endl;
    }

    return 0;
}

C++: #define	_USE_MATH_DEFINES
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
#include <queue>
#include <stack>
#include <list>
using namespace std;
int x,y;
int o[51][51];
queue<int> q;
void tansaku(int j,int i){
	if(j<1||j>x||i<1||i>y||o[i][j]==0||o[i][j]==2){
		return ;
	}
	if(o[i][j]==1){
		if(q.empty())
		q.push(1);
		o[i][j]=2;
	}
	
	tansaku(j+1,i);
	tansaku(j-1,i);
	tansaku(j,i+1);
	tansaku(j,i-1);
	tansaku(j+1,i+1);
	tansaku(j-1,i-1);
	tansaku(j+1,i-1);
	tansaku(j-1,i+1);
}
int main(){

	while(cin>>x>>y,x!=0||y!=0){
		int count=0;
		for(int i=1;i<=y;i++){
			for(int j=1;j<=x;j++){
				cin>>o[i][j];
			}
		}
		for(int i=1;i<=y;i++){
			for(int j=1;j<=x;j++){
				tansaku(j,i);
				if(!q.empty()){
				count+=q.front();
				}
				q.pop();
			}
		}
		
		cout<<count<<endl;
	}

}