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>
using namespace std;
#include<vector>

int wm,hm;
vector< vector<int> > wh(51, vector<int>(51,0));

int xp[]={-1,0,1,-1,1,-1,0,1};
int yp[]={-1,-1,-1,0,0,1,1,1};

void dfs(int x,int y){
  for(int i=0;i<8;i++){
    int nx=x+xp[i];
    int ny=y+yp[i];
    if(nx>-1&&nx<wm&&ny>-1&&ny<hm&&wh[nx][ny]==1){
      //cout<<nx<<" "<<ny<<" "<<wh[nx][ny]<<endl;
      wh[nx][ny]=0;
      dfs(nx,ny);
    }
  }
}

int main(){
  while(1){
    //int wm,hm;
    int aa,bb;
    cin>>aa>>bb;
    wm=bb;
    hm=aa;
    if(wm==0&&hm==0)break;
    //vector< vector<int> > wh(wm, vector<int>(hm));
    for(int i=0;i<wm;i++){
      for(int j=0;j<hm;j++){
        int a;
        cin>>a;
        wh[i][j]=a;
      }
    }
    int ans=0;
    for(int i=0;i<hm;i++){
      for(int j=0;j<wm;j++){
        if(wh[i][j]==1){
          //cout<<"*"<<i<<" "<<j<<endl;
          wh[i][j]=0;
          dfs(i,j);
          ans++;
        }
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
int box[51][51];
int main(){
  int w,h,i,I,a;
  while(1){
    a=0;
    cin>>w>>h;
    if(w==0&&h==0) break;
    for(i=0;i<=h+1;i++){
      for(I=0;I<=w+1;I++){
	box[i][I]==0;
      }
    }
    for(i=1;i<=h;i++){
      for(I=1;I<=w;I++){
	cin>>box[i][I];
      }
    }
    for(i=1;i<=h;i++){
      for(I=1;I<=w;I++){
	if(box[i][I]==1){
	  kazu(i,I);
	  a++;
	}
      }
    }
    cout<<a<<endl;
  }
  return 0;
}
  void kazu(int i,int I){
    if(box[i][I+1]==1) {
      box[i][I+1]=0;
      kazu(i,I+1);
    }
    if(box[i][I-1]==1) {
      box[i][I-1]=0;
      kazu(i,I-1);
    }
    if(box[i+1][I]==1) {
      box[i+1][I]=0;
      kazu(i+1,I);
    }
    if(box[i+1][I+1]==1) {
      box[i+1][I+1]=0;
      kazu(i+1,I+1);
    }
    if(box[i+1][I-1]==1) {
      box[i+1][I-1]=0;
      kazu(i+1,I-1);
    }
    if(box[i-1][I+1]==1) {
      box[i-1][I+1]=0;
      kazu(i-1,I+1);
    }
    if(box[i-1][I-1]==1) {
      box[i-1][I+1]=0;
      kazu(i-1,I+1);
    }
    if(box[i-1][I]==1) {
      box[i-1][I]=0;
      kazu(i-1,I);
    }

  }

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

using namespace std;


vector<vector<bool>> seen;
vector<int> dx;
vector<int> dy;

void dfs(int u,int v,vector<vector<int>> &c){

    seen[u][v]=true;

    for(int i=0;i<8;i++){
        if(c[u+dx[i]][v+dy[i]]==0)continue;
        if(seen[u+dx[i]][v+dy[i]])continue;
        dfs(u+dx[i],v+dy[i],c);
    }

}

int main(){

    dx={-1,0,1,-1,1,-1,0,1};
    dy={1,1,1,0,0,-1,-1,-1};

    int w=1,h=1;
    cin >> w >> h;
    while(true){
        if(h==0 && w==0)break;
        else{
            vector<vector<int>> c(h+2);
            for(int i=0;i<h+2;i++)c[i].resize(w+2);

            for(int i=0;i<w+2;i++)c[0][i]=0;

            for(int i=1;i<h+1;i++){
                c[i][0]=0;
                for(int j=0;j<w;j++){
                    cin >> c[i][j+1];
                }   
                c[i][w+1]=0;
            }
            for(int i=0;i<w+2;i++)c[h+1][i]=0;
            seen.resize(h+2);
            for(int i=0;i<h+2;i++)seen[i].assign(w+2,false);            

            int e=0;

            for(int i=1;i<h+1;i++){
                for(int j=1;j<w+1;j++){
                    if(seen[i][j]==false && c[i][j]==1){
                        dfs(i,j,c);
                        e++;
                    }
                }
            }
            cout << e << endl;
        }
        cin >> w >> h;
    }
}

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

using std::cin;
using std::cout;
using std::endl;

int main(void) {
	int w,h;
	cin >> w;
	cin >> h;

	while (w!=0||h!=0) {
		int count = 0;
		std::stack<int> st_x,st_y;
		int xn,yn;
		int c;
		bool all = false;
		std::vector< std::vector<bool> > island(h+2, std::vector<bool>(w+2));
		for (int i = 0; i < h+2; i++) {
			for (int j = 0; j < w+2; j++) {
				if (i==0||i==h+1||j==0||j==w+1) {
					island[i][j] = false;
				} else {
					cin >> c;
					if (c==1) {
						island[i][j] = true;
					} else {
						island[i][j] = false;
					}
				}
			}
		}

		while (!all) {
			all = true;
			for (int i = 1; i < h+1; i++) {
				for (int j = 1; j < w+1; j++) {
					if (island[i][j]) {
						xn = i;
						yn = j;
						all = false;
					}
				}
			}
			if (!all) {
				st_x.push(xn);
				st_y.push(yn);
				if (island[xn][yn]) {
					island[xn][yn] = false;
					count++;

					while (!st_x.empty()) {
						xn = st_x.top();
						yn = st_y.top();
						st_x.pop();
						st_y.pop();
						if (island[xn+1][yn]) {
							st_x.push(xn+1);
							st_y.push(yn);
							island[xn+1][yn] = false;
						}
						if (island[xn-1][yn]) {
							st_x.push(xn-1);
							st_y.push(yn);
							island[xn-1][yn] = false;
						}
						if (island[xn+1][yn+1]) {
							st_x.push(xn+1);
							st_y.push(yn+1);
							island[xn+1][yn+1] = false;
						}
						if (island[xn+1][yn-1]) {
							st_x.push(xn+1);
							st_y.push(yn-1);
							island[xn+1][yn-1] = false;
						}
						if (island[xn-1][yn+1]) {
							st_x.push(xn-1);
							st_y.push(yn+1);
							island[xn-1][yn+1] = false;
						}
						if (island[xn-1][yn-1]) {
							st_x.push(xn-1);
							st_y.push(yn-1);
							island[xn-1][yn-1] = false;
						}
						if (island[xn][yn+1]) {
							st_x.push(xn);
							st_y.push(yn+1);
							island[xn][yn+1] = false;
						}
						if (island[xn][yn-1]) {
							st_x.push(xn);
							st_y.push(yn-1);
							island[xn][yn-1] = false;
						}
					}
				}
			}
		}
		cout << count << endl;

		cin >> w;
		cin >> h;
	}	
		return 0;
}