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;
using vi=vector<int>;
using vvi=vector<vi>;
using vs=vector<string>;
using msi=map<string,int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
vvi f;
int ans=0;
void search(int x,int y){
    vi v={1,0,-1,0,1,1,-1,-1,1};
    rep(i,8){
        if(f[y+v[i]][x+v[i+1]]==1){
            f[y+v[i]][x+v[i+1]]=0;
            search(x+v[i+1],y+v[i]);
        }
    }
}

int main(void){
    int w,h;
    while(cin>>w>>h,w){
        ans=0;
        f=vvi(h+2,vi(w+2,0));
        rep(i,h)rep(j,w){
            cin>>f[i+1][j+1];
        }
        rep(i,h)rep(j,w){
            if(f[i+1][j+1]==1){
                search(j+1,i+1);
                ans++;
            }
        }
        cout<<ans<<endl;
    }
}

C++: #include<iostream>
using namespace std;
int w,h,i,j;
int map[52][52];

void rec(int i,int j){
 
  if(map[i][j]==0)return;
  map[i][j]=0;
  rec(i+1,j);
  rec(i-1,j);
  rec(i,j-1);
  rec(i,j+1);
  rec(i-1,j-1);
  rec(i-1,j+1);
  rec(i+1,j-1);
  rec(i+1,j+1);
}
int main(){
  
  
  while(1){
    cin >> w >> h;
    if(w==0 && h==0)break;
    
    for(i=1;i<=h;i++){
      for(j=1;j<=w;j++){
	cin >> map[i][j];
	
      }
    }
    int count=0;
    for(int k=1;k<=h;k++){
      for(int l=1;l<=w;l++){
	if(map[k][l]==1){
	  count++;
	  rec(k,l);
	}
      }
    }
    cout << count << endl;
  }
  
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define pb push_back
const int maxn = 10000;
const int INF32 = 1'050'000'000;
const long long INF64 = 4'000'000'000'000'000'000;
const int MOD7 = 1'000'000'007;
const int MOD9 = 1'000'000'009;

ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }

int h, w;
using Graph = vector<vector<int>>;
Graph G;
vector<pair<int,int>> vis;
int cnt;

void dfs(int x, int y)
{
	G[x][y] = cnt;
	for(int dx = -1; dx <= 1; dx++){
		for(int dy = -1; dy <= 1; dy++){
			int nx = x + dx;
			int ny = y + dy;
			if(nx<0 || nx>=h || ny<0 || ny>=w || 
				G[nx][ny]!=1)
				continue;
			dfs(nx,ny);
		}
	}
}

int main()
{
	int time = 0;
	while(1){
		cin >> w >> h;
		if(h==0&&w==0) break;
		G.clear();
		G.resize(h);
		rep(i,h) G[i].resize(w);
		rep(i,h)rep(j,w){//input
			cin >> G[i][j];
		}
		cnt = 1;
		rep(i,h)rep(j,w){
			if(G[i][j]==1){ 
				cnt++;
				dfs(i,j);
			}
		}
		// rep(i,h)rep(j,w){
		// 	if(j!=w-1) printf("%d ", G[i][j]);
		// 	else printf("%d\n", G[i][j]);
		// }
		time++;
		cout << cnt-1 << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
typedef pair<int,int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int w,h;
int nx,ny;
void dfs(vector<vector<int>> &G,int a,int b){
     if(0>nx|| nx>=h || 0>ny|| ny>=w) return ;
     if( G.at(nx).at(ny)==0) return ; 
     G.at(a).at(b)=0;
    for(int k=-1;k<=1;k++){
        for(int s=-1;s<=1;s++){
            nx=a+k;
            ny=b+s;
            dfs(G,nx,ny);
        }
    }
    return ;
}
 
 
 
int main(){
    cin>>w>>h;
    vector<vector<int>>G(h,vector<int>(w));
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>G.at(i).at(j);
        }
    }
    int count=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(G.at(i).at(j)==1){
                count++;
                dfs(G,i,j);
            }
        }
    }
cout<<count<<endl;
}

C++: #include <bits/stdc++.h>
using namespace std;

vector<vector<bool>> seen;
int H, W;
int d_h[8] = {1,1,1,0,0,-1,-1,-1};
int d_w[8] = {1,0,-1,1,-1,1,0,-1};

void dfs(const vector<vector<int>> &m, int h, int w){
    seen[h][w] = true;

    //cout << h << ' ' << w <<  ' ' << m[h][w] << endl;
    
    for(int i = 0; i < 8; i++){
        if(1 <= h+d_h[i] && h+d_h[i] <= H && 1 <= w+d_w[i] && w+d_w[i] <= W && (!seen[h+d_h[i]][w+d_w[i]]) && m[h+d_h[i]][w+d_w[i]] == 1){
            dfs(m, h+d_h[i], w+d_w[i]);
        }
    }
}

int main(){
    while(1){
        cin >> W >> H;
        seen.assign(H+1, vector<bool>(W+1, false));
        if(H == 0 && W == 0) break;
        vector<vector<int>> m(H+1, vector<int>(W+1,0));
        for(int i = 1; i <= H; i++){
            for(int j = 1; j <= W; j++){
                cin >> m[i][j];
            }
        }

        int cnt = 0;
        for(int i = 1; i <= H; i++){
            for(int j = 1; j <= W; j++){
                if(!seen[i][j] && m[i][j] == 1) {
                    //cout << i << ' ' << j << endl;
                    cnt++;
                    dfs(m, i, j);
                }
            }
        }
        cout << cnt << endl;
    }
}