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<stdio.h>
void printmap();
void draw(int x,int y);
int w,h,c[51][51];

int main(){
	int i,j;
	int count;


	scanf("%d %d",&w,&h);


	while((w!=0)&&(h!=0)){
		count=0;
	
		for(j=0;j<h;j++){
			for(i=0;i<w;i++){
				scanf("%d",&c[j][i]);
			}
		}
		//printf("\n");
		
		for(i=0;i<w;i++){
			c[h][i]=0;
		}
		for(i=0;i<h;i++){
			c[i][w]=0;
		}

		for(i=0;i<w;i++){
			for(j=0;j<h;j++){
				if(c[j][i]==1){
					count++;
					//printf("count up count=%d\n",count);
					draw(j,i);
					//printmap();
				}
			}
		}

		printf("%d\n",count);
		scanf("%d %d",&w,&h);
	}
	return 0;
}

void draw(int x,int y){
	//printf("delete %d %d\n",x,y);
	int i,j;
	c[x][y]=0;
	for(i=0;i<3;i++){
		for(j=0;j<3;j++){
			if(c[x-1+i][y-1+j]==1){
				draw(x-1+i,y-1+j);
			}
		}
	}


}

void printmap(){
	int i,j;
	for(j=0;j<h;j++){
		for(i=0;i<w;i++){
			printf("%d ",c[j][i]);
		}
		printf("\n");
	}
}

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

typedef pair<int,int> P;
int F[50][50];
int x[8]={1,1,1,0,0,-1,-1,-1};
int y[8]={1,0,-1,1,-1,1,0,-1};
int h,w;

void dfs(int j, int i){
  stack<P> S;
  S.push(P{j,i});
  F[i][j]=0;
  while(!S.empty())
  {
    P p=S.top(); S.pop();
    for(int i=0;i<8;i++)
    {
      int nx=p.first+x[i], ny=p.second+y[i];
      if(nx>=0 && nx<w && ny>=0 && ny<h && F[ny][nx]==1)
      {
        F[ny][nx]=0;
        S.push(P{nx,ny});
      }
    }
  }
}
int island(int w,int h){
  int ans=0;
  for(int i=0;i<h;i++)
  {
    for(int j=0;j<w;j++)
    {
      if(F[i][j]==1)
      {
        ans++;
        dfs(j,i);
      }
    }
  }
  return ans;
}

int main(){
    cin>>w>>h;
    while(true){
      if(w==0 && h==0) break;
      for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
          cin>>F[i][j];
        }
      }
      cout<<island(w,h)<<"\n";
      cin>>w>>h;
    }

}

C++: #include<iostream>
#include<bitset>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
ll INF = 1 << 30;


int rel[12][12];
vector<vector<int>> mp;
vector<vector<int>> vst;
int mx, my;

int adj(int x, int m) {

	if (x < 0) return 0;
	if (x >= m) return m - 1;

	return x;
}

void dfs(int x, int y) {
	int d[] = { -1,0,1 };
	int nx, ny;

	if (vst[y][x] == 1) return;

	vst[y][x] = 1;

	rep(j, 3) {
		rep(i, 3) {
			nx = adj(x + d[i], mx);
			ny = adj(y + d[j], my);
			if (mp[ny][nx] == 1 && vst[ny][nx] == 0) {
				dfs(nx, ny);
			}
		}
	}
}

int islCnt() {

	int f = 0;

	rep(i, my) {
		rep(j, mx) {

			if (mp[i][j] == 1 && vst[i][j] == 0) {
				f++;
				dfs(j, i);
			}

		}
	}

	return f;
}


int main() {


	int n, m, x, y, ans;
	ans = 0;

	while (true) {
		cin >> n >> m;

		if (n == 0 && m == 0) break;

		mp = vector<vector<int>>(m);
		vst = vector<vector<int>>(m);

		rep(i, m) {
			rep(j, n) {
				cin >> x;
				mp[i].push_back(x);
				vst[i].push_back(0);

			}
		}

		mx = mp[0].size();
		my = mp.size();

		ans = islCnt();

		cout << ans << endl;
	}

}

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

using namespace std;

struct locate{
  int x;
  int y;
};

int graph[64][64] = {{}};
int flag[64][64] = {{}};
int w,h;

int dfs(locate );


int main(){

  int sum = 0;
  locate in;

  while(1){
  cin >> w >> h ;
  if(w == 0 && h == 0) break;
  for(int i = 0;i < h;i++){
    for(int j = 0;j < w;j++){
      cin >> graph[i][j];
    }
  }
    for(int i = 0;i < h;i++){
      for(int j = 0;j < w;j++){
	if(graph[i][j] == 1 && flag[i][j] == 0){
	  sum++;
	  in.x = j;
	  in.y = i; 
	  if(dfs(in) == -1){
	    sum-= 1;
	  }
	  //	cout << "sum = " << sum << endl;
	}
      }
    }

    cout << sum << endl;
    sum = 0;
    for(int i = 0;i < 64;i++){
      for(int j = 0;j < 64;j++){
	graph[i][j] = 0;
	flag[i][j] = 0;
      }
    }
  }
  return 0;
}



int dfs(locate in){

  stack<locate> S;
  locate u,v,re;
  int  dx,dy,a = 0;

  S.push(in);

  while(!S.empty()){
    v = S.top();S.pop();
    for(dy = -1;dy <= 1;dy++){
      if(v.y + dy < 0 || v.y + dy > 63) continue;
      for(dx = -1;dx <= 1;dx++){
	if(v.x + dx < 0 || v.x + dx > 63 || (dx == 0 && dy == 0)) continue;
	//	cout << "x = " << v.x << "y = " << v.y << endl; 
	if(graph[v.y + dy][v.x + dx] == 1 && flag[v.y + dy][v.x + dx] == 0){
	  // cout << 'a' << endl;
	  flag[v.y + dy][v.x + dx] = 1;
	  re.x = v.x + dx;
	  re.y = v.y + dy;
	  a = 1;
	  dfs(re);
	}
	/*else if(graph[v.y + dy][v.x + dx] == 1 && flag[v.y + dy][v.x + dx] == 1){
	  cout << 'b' << endl;
	  a = 0;
	  return -1;
	  }*/
      }
    }
  }
  return a;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<math.h>
#include<stdio.h>
using namespace std;
#define mod (1000000000+7)
#define N (10007)
#define INF 1e16
typedef long long ll;
typedef pair<ll,ll> P;
int h,w;
int board[100][100];

bool check(int i, int j) {
	return (i >= 0) && (i < h) && (j >= 0) && (j < w) && (board[i][j] == 1);
}

void dfs(int i,int j){
	for(int dx=i-1;dx<=i+1;dx++){
		for(int dy=j-1;dy<=j+1;dy++){
			if(check(dx,dy)){
				board[dx][dy]=0;
				dfs(dx,dy);
			}
		}
	}
}

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>>board[i][j];
			}
		}
		int ans=0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(board[i][j]){
					dfs(i,j);
					ans++;
				}
			}
		}
		cout<<ans<<endl;
	}
    return 0;
}