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>
#include<vector>
#include<utility>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<complex>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long

using namespace std;
int main(){
  int w,h;
  int vx[8]={1,0,-1,0,1,1,-1,-1};
  int vy[8]={0,1,0,-1,1,-1,1,-1};
  while(cin>>w>>h,w+h){
    ll ans=0;
    int Map[h][w]={};
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	cin>>Map[i][j];
    bool Map2[h][w]={};
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(Map2[i][j])continue;
	if(Map[i][j]==0)Map2[i][j]=true;
	else{
	  Map2[i][j]=true;
	  queue<pair<int,int> >que;
	  que.push(mp(i,j));
	  while(!que.empty()){
	    pair<int,int>now=que.front();
	    que.pop();
	    for(int i=0;i<8;i++){
	      int a=now.F+vx[i],b=now.S+vy[i];
	      if(!(a>=0&&a<h&&b>=0&&b<w))continue;
	      if((Map[a][b]==1)&&(!Map2[a][b])){
		Map2[a][b]=true;
		que.push(mp(a,b));
	      }else{
		Map2[a][b]=true;
	      }
	    }
	  }
	  ans++;
	}
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include <iostream>
using namespace std;
void kazoeruyo(int p,int P,int box[50][50]){
  box[p][P]=0;
    if(box[p][P+1]==1) {
      box[p][P+1]=0;
      P=P+1; 
      kazoeruyo(p,P,box);
    }
    if(box[p][P-1]==1){
      box[p][P-1]=0;
      P=P-1;
      kazoeruyo(p,P,box);
    }
    if(box[p+1][P]==1){ 
      box[p+1][P]=0;
      p=p+1;
      kazoeruyo(p,P,box);
    }
    if(box[p-1][P]==1){
      box[p-1][P]=0;
      p=p-1;
      kazoeruyo(p,P,box);
    }
    if(box[p-1][P-1]==1){
      box[p-1][P-1]=0;
      p=p-1;
      P=P-1;
      kazoeruyo(p,P,box);
    }
    if(box[p-1][P+1]==1){
      box[p-1][P+1]=0;
      p=p-1;
      P=P+1;
      kazoeruyo(p,P,box);
    }
    if(box[p+1][P+1]==1){
      box[p+1][P+1]=0;
      p=p+1;
      P=P+1;
      kazoeruyo(p,P,box);
    }
    if(box[p+1][P-1]==1){
      box[p+1][P-1]=0;
      p=p+1;
      P=P-1;
      kazoeruyo(p,P,box);
    }	
}
int main(){
  int w,h,i,I,p,P,o,t,T,y,Y;
  int box[50][50];
  while(1){
    cin>>w>>h;
    if(w==0&&h==0) break;
    for(i=0;i<h;i++){
      for(I=0;I<w;I++){
	cin>>box[i][I];
      }
    }
    o=0;
    for(i=0;i<h;i++){
      for(I=0;I<w;I++){
	if(box[i][I]==1) {
	  p=i;
	  P=I;
	  kazoeruyo(p,P,box);
	  box[p][P]=1;
	}
      }
    }
    for(i=0;i<h;i++){
      for(I=0;I<w;I++){
	if(box[i][I]==1) o++;
      }
    }
    cout<<o<<endl;
    o=0;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
 
#define loop(n, i) for(int i=0;i<n;i++)
#define all(v) v.begin(),v.end()

using namespace std;

int C[100][100] = {};
int vis[100][100] = {};

int dfs(int i, int j)
{
    int d[] = {1, 0, -1};
    if (!vis[i][j] && C[i][j]) {
        vis[i][j] = 1;
        loop (3, x) loop (3, y) dfs(i + d[x], j + d[y]);
    }
}

int main()
{
    while (1) {
        fill(C[0], C[99]+100, 0);
        fill(vis[0], vis[99]+100, 0);

        int w, h; cin >> w >> h;
        if (!w && !h) break;
        loop (h, i) loop (w, j) cin >> C[i+1][j+1];

        int ans = 0;
        loop (h, i) loop (w, j) {
            if (vis[i+1][j+1] || !C[i+1][j+1]) continue;
            ans++;
            dfs(i+1, j+1);
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <stdlib.h>

using namespace std;

int d[8][2] = {
	{-1, -1},
	{0, -1},
	{1, -1},
	{-1, 0},
	{1, 0},
	{-1, 1},
	{0, 1},
	{1, 1}
};

void func(int **a, int s, int t, int i, int j)
{
	if(a[i][j] == 0){
		return ;
	}
	a[i][j] = 0;
	
	for(int k = 0; k < 8; k++){
		if((i+d[k][0] >= 0 && i+d[k][0] < t) && (j+d[k][1] >= 0 && j+d[k][1] < s)){
			func(a, s, t, i+d[k][0], j+d[k][1]);
		}
	}
	
	return ;
}

int main(int argc, char **argv)
{
	int s, t, **a, c, t2, s2;
	
	while(1){
		cin >> s >> t;
		if(s == 0 && t == 0){
			break;
		} else if(s == 1 || t == 1){
			t2 = t+1;
			s2 = s+1;
		} else {
			t2 = t;
			s2 = s;
		}
		a = (int **)calloc(t2, sizeof(int));
		for(int i = 0; i < t; i++){
			a[i] = (int *)calloc(s2, sizeof(int));
			for(int j = 0; j < s; j++){
				cin >> c;
				a[i][j] = c;
			}
		}
		c = 0;
		
		for(int i = 0; i < t; i++){
			for(int j = 0; j < s; j++){
				if(a[i][j] == 1){
					c++;
					func(a, s, t, i, j);
				}
			}
		}
		cout << c << endl;
		
		for(int i = 0; i < t2; i++){
			free(a[i]);
		}
		free(a);
	}
	
	return 0;
}