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

void proc(vector<int> &maps,int p,int w,int h){
	if(maps[p]==1) maps[p]=2;
	else return;
	int x=p%w;
	int y=p/w;
	for(int i=-1;i<=1;++i){
		for(int j=-1;j<=1;++j){
			int nx=x+j;
			int ny=y+i;
			if(nx>=0&&ny>=0&&nx<w&&ny<h){
				proc(maps,nx+ny*w,w,h);
			}
		}
	}
}

int main(void){
	int w,h;
	while(cin>>w>>h,w&&h){
		vector<int> chizu(w*h);
		for(int i=0;i<h;++i){
			for(int j=0,a;j<w;++j){
				cin>>a;
				chizu[i*h+j]=a;
			}
		}
		int count = 0;
		for(int i=0;i<w*h;++i){
			if(chizu[i]==1){
				proc(chizu,i,w,h);
				++count;
			}
		}
		cout<<count<<endl;
	}
	return 0;
}

C++: 
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <bitset>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for (int i=(n)-1;i>=0;i--)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define REPS(i,f,n) for (int i=(f)-1;i>=(n);i--)
int dx[8] = {1, 0, 0, -1, 1, 1, -1, -1};
int dy[8] = {0, 1, -1, 0, 1, -1, 1, -1};

int board[51][51];
int w, h;

void slove(void){
	int cnt = 0;
	queue<pint> q;
	rep(i, h){
		rep(j, w){
			if (board[i][j] == 1){
				q.push(mp(i, j));
				cnt++;
				/*
				rep(k, h){
					rep(l, w){
						printf("%d", board[k][l]);
					}
					printf("\n");
				}
				*/
				while(!q.empty()){
					pint now = q.front(); q.pop();
					rep(i, 8){
						int nowy = now.fi + dy[i];
						int nowx = now.se + dx[i];
						if (0 <= nowy && nowy <= h - 1 && 0 <= nowx && nowx <= w - 1 && board[nowy][nowx] == 1){
							board[nowy][nowx] = 0;
							q.push(make_pair(nowy, nowx));
						}
					}
				}
			}
		}
	}

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

void scan(int w, int h){
	rep(i, h) rep(j, w) cin >> board[i][j];

	slove();
	return;
}

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

		if (w == 0) break;
		scan(w, h);
	}
	return 0;
}

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

const int dx[] = {-1, -1, -1,  0, 0,  1, 1, 1};
const int dy[] = {-1,  0,  1, -1, 1, -1, 0, 1};

void dfs(int x, int y, vector<vector<bool> >& canGo){
  canGo[x][y] = false;
  for(int i = 0; i < 8; i++)
    if(canGo[x + dx[i]][y + dy[i]])
      dfs(x + dx[i], y + dy[i], canGo);
  return;
}


int main(void){
  while(true){
    int w, h;
    cin >> w >> h;
    if(w == 0 && h == 0){ break; }

    int input;
    vector<vector<bool> > canGo(h + 2, vector<bool>(w + 2, false));
    for(int i = 1; i <= h; i++){
      for(int j = 1; j <= w; j++){
        cin >> input;
        if(input == 1){ canGo[i][j] = true; }
      }
    }

    int count = 0;
    for(int i = 1; i <= h; i++){
      for(int j = 1; j <= w; j++){
        if(canGo[i][j]){
          ++count;
          dfs(i, j, canGo);
        }
      }
    }

    cout << count << endl;
  }

  return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

bool used[55][55];
int h,w;
int a[55][55];
void dfs(int x,int y){
    REP(i,-1,2)REP(j,-1,2){
        int nx=x+i,ny=y+j;
        if(nx<0||nx>=h||ny<0||ny>=w)continue;
        if(used[nx][ny]||a[nx][ny]==0)continue;
        used[nx][ny]=1;
        dfs(nx,ny);
    }
}

void solve(){
    rep(i,h)rep(j,w)cin>>a[i][j];
    rep(i,h)rep(j,w)used[i][j]=false;
    int ans=0;
    rep(i,h)rep(j,w){
        if(used[i][j]||a[i][j]==0)continue;
        ++ans;
        used[i][j]=1;
        dfs(i,j);
    }
    cout<<ans<<endl;
}
int main(){
    while(cin>>w>>h,w!=0)solve();
    return 0;
}

C++: #include <stdio.h>

	int sima[51][51];
	int cnt=0;
	int cflag=0;

int count_sima(int w,int h){
	if(w<0)return 0;
	if(h<0)return 0;
	if(sima[w][h]==3)return 0;
	if(sima[w][h]==0)return 0;

		if(cflag==0){
			cnt++;
			cflag=1;
		}
		

		sima[w][h]=0;

		count_sima(w,h+1);
		count_sima(w+1,h+1);
		count_sima(w+1,h);
		count_sima(w+1,h-1);
		count_sima(w,h-1);
		count_sima(w-1,h-1);
		count_sima(w-1,h);
		count_sima(w-1,h+1);


	return 0;
}


int main(void){
	int w2,h2; //島の大きさを格納する
	int p;

	scanf("%d %d",&w2,&h2);
	while(w2!=0 && h2!=0){

		//島配列をすべてを3にする
		for(int i=0; i<51; i++){
			for(int j=0; j<51; j++){
				sima[i][j]=3;
			}
		}

		//入力された地図のデータを設定
		for(int i=0; i<h2; i++){
			for(int j=0; j<w2; j++){
				scanf("%d",&p);
				sima[i][j]=p;
			}
		}

		//ここまで入力された地図のデータを設定

		for(int i=0; i<h2; i++){
			for(int j=0; j<w2; j++){
				count_sima(i,j);
				cflag=0;
			}
		}

		printf("%d\n",cnt);
		cnt=0;

		scanf("%d %d",&w2,&h2);
	}

	return 0;
}

C++: #include <cstdio>
#define f(n)for(int i=0;i<n;i++)
#define s(w)scanf("%d",&w)
int a,n,m,d[99][99];
int u[16]={-1,-1,-1,0,0,1,1,1,-1,0,1,-1,1,-1,0,1};
void k(int x,int y){
	if(d[x][y]){f(8)k(x+u[i],y+u[i+8]);d[x][y]=0;}
}
int main(){
	a=0;
	s(n);s(m);
	f(9801)d[0][i]=0;
	f(n*m)s(d[i/n+1][i%n+1]);
	f(n*m)if(d[i/n+1][i%n+1])a++,k(i/n+1,i%n+1);
	printf("%d\n",a);
	main();
}