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 <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i < (n);i++)
#define ALL(obj) (obj).begin(),(obj).end()
#define SCANF(i) int i;scanf("%d",&i)
using namespace std;
using P = pair<int,int>;
const long long INF = 1LL << 60;
const int IINF=100000000;
const int MOD = (int)1e9 + 7;
typedef long long ll;
//約数列挙
vector<long long> divisor(long long n) {
    vector<long long> ret;
    for (long long i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i * i != n) ret.push_back(n / i);
        }
    }
    //sort(ret.begin(), ret.end()); // 昇順に並べる
    return ret;
}
vector<int> dx={1,0,-1,0};vector<int> dy={0,-1,0,1};
int h,w;
vector<vector<int>> grid;
vector<vector<bool>> seen;
int xx[8] = {0,-1,-1,-1,0,1,1,1};
int yy[8] = {1,1,0,-1,-1,-1,0,1};
void dfs(int x,int y){
    seen[x][y] = true;
    for(int i=0;i<8;i++){
        int nx = x+xx[i];int ny = y+yy[i];
        if(nx<0||nx>h-1||ny<0||ny>w-1)continue;
        if(!seen[nx][ny] && grid[nx][ny]==1){
            dfs(nx,ny);
        }
    }
}
signed main () {
    while(cin >> w >> h){
        if(h+w==0)break;
        int cnt = 0;
        grid.assign(h,vector<int>(w));
        seen.assign(h,vector<bool>(w,false));
        REP(i,h)REP(j,w)cin >> grid[i][j];
        REP(i,h)REP(j,w){
            if(grid[i][j]==1 && !seen[i][j]){
                dfs(i,j);
                cnt++;
            }
        }
        cout << cnt << endl;
    }
}

C++: // Time-stamp: <2012-06-26 20:15:18 naga>

#include<iostream>
#include<cstdio>
using namespace std;
int maps[52][52];
int w,h;
void init(){
  for(int i = 0; i < 52; i++){
    for(int j = 0; j < 52; j++){
      maps[i][j] = 0;
    }
  }
  return;
}

void search(int x, int y){
  maps[y][x] = 0;
  if(maps[y - 1][x] && y > 0){
    search(x, y -1);
  }
  if(maps[y - 1][x - 1] && x > 0 && y > 0){
    search(x - 1, y - 1);
  }
  if(maps[y][x - 1] && x > 0){
    search(x - 1, y);
  }
  if(maps[y + 1][x - 1] && y < h && x > 0){
    search(x - 1, y + 1);
  }
  if(maps[y + 1][x] && y < h){
    search(x, y + 1);
  }
  if(maps[y + 1][x + 1] && y < h && x < w){
    search(x + 1, y + 1);
  }
  if(maps[y][x + 1] && x < w){
    search(x + 1, y);
  }
  if(maps[y - 1][x + 1] && y > 0 && x < w){
    search(x + 1, y - 1);
  }
  return;
}


int main(){
  int count;
  while(true){
    cin >> w >> h;
    if( !w && !h){
      break;
    }
    count = 0;
    init();
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
	cin >> maps[i][j];
      }
    }
    
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
	if(maps[i][j]){
	  count++;
	  search(j,i);
	}
      }
    }

    printf("%d\n", count);
  }
  return 0;
}

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

bool search(vector<vector<bool>>& isvisit, vector<vector<int>> c, int i, int j){
    if(!(i >= 0 && i < isvisit.size() && j >= 0 && j < isvisit[i].size())){
        return false;
    }else if(isvisit[i][j]){
        return false;
    }else{
        isvisit[i][j] = true;
        if(c[i][j] == 0) return false;

        for(int ii = -1; ii <= 1; ii += 2){
            search(isvisit, c, i+ii, j);
            search(isvisit, c, i, j+ii);
            search(isvisit, c, i+ii, j+ii);
            search(isvisit, c, i+ii, j-ii);
        }
        return true;
    }
}

int main(){
    int w, h;
    while(true){
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        vector<vector<int>> c(h, vector<int>(w));
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++) cin >> c[i][j];
        }
        int ans = 0;
        vector<vector<bool>> isvisit(h, vector<bool>(w, false));
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                if(search(isvisit, c, i, j)) ans++;
            }
        }
        cout << ans << endl;
    }
    
    return 0;
}

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

int main() {
	int n;
	while (cin >> n, n) {

		vector<vector<int>>list(n, vector<int>(n, -1));
		int number;

		for (int i = 0;i < n;i++) {
			cin >> number;
			for (int j = 0;j < number;j++) {
				int codename;
				cin >> codename;
				list[i][codename - 1] = 1;
			}
		}//list[a][b]:a know b

		cin >> number;
		
		vector<int>ans(n,1);
		int leaked;
		for (int i = 0;i < number;i++) {
			cin >> leaked;
			for (int j = 0;j < n;j++) {
				if (list[j][leaked - 1] <= 0) ans[j] = 0;
			}
		}//who is imposible
		int sum = 0,telltale;

		for (int i = 0;i < n;i++) {
			sum += ans[i];
			if (ans[i] == 1) telltale = i + 1;
		}

		if (sum == 1)cout << telltale << endl;
		else cout << -1 << endl;

	}
	return 0;
}

C++: #include <iostream>

using namespace std;
int c[51][51];
int w ,h;

bool input(){
	cin >> h >> w;
	if(w==0) return false;
	for(int i = 0;i <51;i++){
		for(int j = 0;j <51 ;j++){
			c[i][j] = 2;
		}
	}

	for(int i = 0;i < w ;i++){
		for(int j = 0; j < h ;j++){
			cin >> c[i][j];
//			cout <<c[i][j];
		}
	}
	return true;
}

void dfs(int x,int y){
	c[x][y] = 0;
	for(int i = -1; i <=1 ;i++){
		for(int j = -1; j <=1 ;j++){
			int nx = x + i;
			int ny = y + j;
//			cout <<nx << ' ' << ny << endl;
			if(0 <= nx && nx < w && 0 <= ny && ny < h && c[nx][ny] == 1)dfs(nx,ny);
		}
	}
}

void solve(){
	int res = 0;
/*	for(int i=0;i < w ;i++){
		for (int j=0;j<h;j++){
			cout <<c[i][j];
		}
		cout <<endl;
	}
*/
	for(int i=0; i < h;i++){
		for(int  j=0;j < w;j++){
			if(c[i][j] ==1){
				res++;
				dfs(i,j);
			}
		}
	}
	cout << res << endl;
}

int main(){
	while(input())solve();
}