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 <queue>
#include <utility>

using namespace std;
using Point = pair<int,int>;

int map[52][52];

void BFS(const Point &p0);

int main(){
  int w,h;
  while(cin>>w>>h&&w>0&&h>0){

    for(int i=1;i<h+1;++i){
      for(int j=1;j<w+1;++j){
        cin >> map[i][j];
      }
    }

    int num = 0;
    for(int i=1;i<h+1;++i){
      for(int j=1;j<w+1;++j){
        if(map[i][j]){
          BFS(Point(i,j));
          num++;
        }
      }
    }
    cout << num << endl;
  }

  return 0;
}

void BFS(const Point &p0){
  queue<Point> q;
  q.push(p0);
  map[p0.first][p0.second] = 0;
  while(!q.empty()){
    auto p = q.front();q.pop();
    int y = p.first , x = p.second;

    for(int i=-1;i<=1;++i){
      for(int j=-1;j<=1;++j){
        if(i==0&&j==0)continue;
        if(map[i+y][j+x]){
          q.push(Point(i+y,j+x));
          map[i+y][j+x] = 0;
        }
      }
    }
  }
}

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

struct pos{
    int x;
    int y;
};

int main(){
    int w, h;
    while(cin >> w >> h, w != 0 && h != 0){
        int field[52][52] = {{0}};
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= w; j++){
                cin >> field[i][j];
            }
        }

        stack<pos> st;
        int island = 0;
        int dx[] = {-1,  0, 1, 0, -1,  1, 1, -1};
        int dy[] = {0 , -1, 0, 1, -1, -1, 1,  1};
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= w; j++){
                //島を探す
                if(field[i][j] == 1){
                    pos p;
                    p.y = i;
                    p.x = j;
                    st.push(p);
                    field[i][j] = 0;
                    while(!st.empty()){
                        pos stTop = st.top();
                        st.pop();
                        for(int k = 0; k < 8; k++){
                            p = stTop;
                            p.y += dy[k];
                            p.x += dx[k];
                            if(field[p.y][p.x] == 1){
                                st.push(p);
                                field[p.y][p.x] = 0;
                            }
                        }
                    }
                    island++;
                }

            }
        }

        cout << island << endl;
    }
}

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

using namespace std;

int w;
int h;
bool a[100][100];
int counter = 0;
int hor[3] = {-1,0,1};
int ver[3] = {-1,0,1};
int cnt = 0;
int i;
int j;
int i2;
int j2;

void dfs(int row,int col){
  if(row>=h || col>=w || row<0 || col<0 || a[row][col]==0){
    return;
  }
  a[row][col] = 0;
  for(int i2=0;i2<3;i2++){
    for(int j2=0;j2<3;j2++){
      dfs(row+hor[i2],col+ver[j2]);
    }
  }
}
int main() {
  while(1){
    cnt = 0;
    cin >>w;
    cin >>h;
    if(w==0 || h==0) break;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
      cin>>a[i][j];
      }
    }
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
        if(a[i][j]==1){
          cnt++;
          dfs(i,j);
        }
      }
    }
    printf("%d\n",cnt);
  }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <sstream>
#include <cmath>
#include <set>
#include <iomanip>
#include <deque>
#include <stdio.h>
using namespace std;

#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define RREP(i,n) for(int (i)=(int)(n)-1;i>=0;i--)
#define REMOVE(Itr,n) (Itr).erase(remove((Itr).begin(),(Itr).end(),n),(Itr).end())
#define PB_VEC(Itr1,Itr2) (Itr1).insert((Itr1).end(),(Itr2).begin(),(Itr2).end())
#define UNIQUE(Itr) sort((Itr).begin(),(Itr).end()); (Itr).erase(unique((Itr).begin(),(Itr).end()),(Itr).end())
#define LBOUND(Itr,val) lower_bound((Itr).begin(),(Itr).end(),(val))
#define UBOUND(Itr,val) upper_bound((Itr).begin(),(Itr).end(),(val))
typedef long long ll;

vector< vector<int> > M;
int h,w;

bool dfs(int y, int x){
    if(y>=h||y<0||x>=w||x<0)return false;
    if(M[y][x]==0)return false;
    M[y][x]=0;
    for(int i=-1;i<=1;i++){
        for(int j=-1;j<=1;j++){
            dfs(y+i,x+j);
        }
    }
    return true;
}

int ans(){
    int res=0;
    REP(i,h)REP(j,w){
        if(dfs(i,j))res++;
    }
    
    return res;
}

int main(){
    
    while(true){
        cin>>w>>h;
        if(h==0&&w==0)break;
        
        M.clear();
        M.resize(h);
        REP(i,h){
            REP(j,w){
                int t; cin>>t;
                M[i].push_back(t);
            }
        }
        
        cout<<ans()<<endl;
        
    }
    
    return 0;
}

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

using namespace std;

struct UFTree{
	vector<int> p;
	UFTree(int n):p(n,-1){}
	void Unite(int a,int b){
		int ra=Find(a),rb=Find(b);
		if(ra!=rb){
			if(-p[ra]<-p[rb])
				swap(ra,rb);
			p[ra]+=p[rb];
			p[rb]=ra;
		}
	}
	int Find(int n){
		return p[n]<0?n:p[n]=Find(p[n]);
	}
};

int main()
{
	for(int w,h;cin>>w>>h,w|h;){
		int grid[50][50];
		for(int i=0;i<h;i++)
			for(int j=0;j<w;j++)
				cin>>grid[i][j];
		UFTree uft(h*w);
		for(int i=0;i<h;i++)
			for(int j=0;j<w;j++){
				if(!grid[i][j])
					continue;
				int di[]={0,1,1,1},dj[]={1,-1,0,1};
				for(int k=0;k<4;k++){
					int ni=i+di[k],nj=j+dj[k];
					if(0<=ni && ni<h && 0<=nj && nj<w && grid[ni][nj])
						uft.Unite(i*w+j,ni*w+nj);
				}
			}
		int res=0;
		for(int i=0;i<h;i++)
			for(int j=0;j<w;j++)
				if(grid[i][j] && uft.p[i*w+j]<0)
					res++;
		cout<<res<<endl;
	}
	return 0;
}