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<string.h>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i,n) for(i=0;i<n;i++)
using namespace std;
typedef pair<int, int> pii;

int main()
{
  int i,j,k;
  int h,w;
  int dx[8]={0,1,0,-1,1,1,-1,-1};
  int dy[8]={1,0,-1,0,1,-1,1,-1};

  while(1){

    cin>>w>>h;
    if(w==0 && h==0)break;

    int sum=0;
    int c[55][55]={};

    for(i=1;i<=h;i++)for(j=1;j<=w;j++)cin>>c[i][j];

    while(1){

      queue<pii> q;
      rep(i,h+1)rep(j,w+1)
	if(c[i][j]==1)goto START;
      break;//????????????

    START :
      q.push(pii(i,j));//y,x
      sum++;
      
      while(q.size()){
	i=q.front().first;
	j=q.front().second;
	q.pop();
	c[i][j]=0;
	rep(k,8){
	  int y=i+dy[k];
	  int x=j+dx[k];
	  if(c[y][x]==1){
	    q.push(pii(y,x));
	    c[y][x]=0;
	  }
	}
      }

    }
    cout<<sum<<endl;

  }
}

C++: #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define invrepr(i,a,b) for(int i=b-1;i>=a;i--)
#define invrep(i,n) invrepr(i,0,n)
#define repitr(itr,a) for(auto itr=a.begin();itr!=a.end();++itr)
const ll MOD=1e9+7;

int main() {
    ios_base::sync_with_stdio(false);

    while (1) {
        int w,h;
        cin >> w >> h; 
        if (w==0 && h==0) break;
        vector<vector<char>> s(h+2,vector<char>(w+2,'0'));
        rep(i,h) {
            rep(j,w) cin >> s[i+1][j+1];
        }
        int ans=0;
        vector<int> di={-1,-1,-1,0,0,1,1,1},dj={-1,0,1,-1,1,-1,0,1};
        repr(i,1,h+1) {
            repr(j,1,w+1) {
                if (s[i][j]=='1') {
                    queue<pair<int,int>> q;
                    q.push(make_pair(i,j));
                    s[i][j]='0'; 
                    while (!q.empty()) {
                        pair<int,int> p=q.front();
                        q.pop();
                        rep(k,8) {
                            int ni=p.first+di[k],nj=p.second+dj[k];
                            if (s[ni][nj]=='1') {
                                q.push(make_pair(ni,nj));
                                s[ni][nj]='0';
                            }
                        }
                    }
                    ++ans;
                }
            }
        }
        cout << ans << endl;
    }
    
    return 0;
}

C++: #include <iostream>
#include<vector>
using namespace std;

int w,h;
int c[50][50];
vector<vector<int> > visited;
int dx[]={1,1,0,-1,-1,-1,0,1};
int dy[]={0,-1,-1,-1,0,1,1,1};

bool dfs(int x,int y){
    if(visited[x][y]) return false;
    visited[x][y]=1;

    for(int d=0;d<8;d++){
        int i=x+dx[d];
        int j=y+dy[d];
        if(0<=i&&i<h&&0<=j&&j<w){
            if(c[i][j]){
                dfs(i,j);
            }  
        }
    }
    return true;
}

int main(){
    visited.assign(50,vector<int>(50,0));
    cin>>w>>h;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>c[i][j];
        }
    }
    int cnt=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(c[i][j]){
                cnt+=dfs(i,j);
            }
        }
    }

    cout<<cnt<<endl;
    return 0;
}

C++: #include <iostream>
using namespace std;
void erase(int island[52][52],int x,int y){
	island[x][y]=0;
	for(int m=-1;m<=1;m++){
		if(island[x+m][y]==1){
			erase(island,x+m,y);
		}
		if(island[x][y+m]==1){
			erase(island,x,y+m);
		}
		if(island[x+m][y+m]==1){
			erase(island,x+m,y+m);
		}
		if(island[x-m][y+m]==1){
			erase(island,x-m,y+m);
		}
	}
}
int main(){
	int w,h;
	while(cin>>w>>h){
		if(w==0 && h==0) break;
		int island[52][52]={0};
		int num,count=0;		
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin>>num;
				island[i+1][j+1]=num;
			}
		}
		for(int i=1;i<h+1;i++){
			for(int j=1;j<w+1;j++){
				if(island[i][j]==1){
					erase(island,i,j);
					count++;
				}
			}
		}
		cout<<count<<endl;
	}
	return 0;
}