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 <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+1][y+1]){f(8)k(x+u[i],y+u[i+8]);d[x+1][y+1]=0;}
}
int main(){
	a=0;
	s(n);s(m);
	f(10000)d[i]=0;
	f(n*m)s(d[i/n][i%n]);
	f(n*m)if(d[i/n][i%n])a++,k(i/n,i%n);
	printf("%d\n",a);
	main();
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int w,h,i,j;
int field[52][52];

void dfs(int x, int y){
    //埋め立て
    field[x][y]=0;
    
    if(field[x][y+1]==1) dfs(x,y+1);
    if(field[x][y-1]==1) dfs(x,y-1);
    
    if(field[x+1][y+1]==1) dfs(x+1,y+1);
    if(field[x+1][y-1]==1) dfs(x+1,y-1);
    if(field[x+1][y]==1) dfs(x+1,y);
    
    if(field[x-1][y+1]==1) dfs(x-1,y+1);
    if(field[x-1][y-1]==1) dfs(x-1,y-1);
    if(field[x-1][y]==1) dfs(x-1,y);
}


int main(){
    while(cin>>w>>h && w!=0 && h!=0){
        for(i=1; i<=h; i++){
            for(j=1; j<=w; j++){
                int a;
                scanf("%d",&a);
                field[i][j]=a;    
            }
        }
    
        int cnt=0;
    
        for(i=1; i<=h; i++){
            for(j=1; j<=w; j++){
                if(field[i][j]==1){
                    dfs(i,j);
                    cnt++;
                }    
            }
        }
        
        cout<<cnt<<endl;
    }
    return 0;
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp
// ????????¶????????£???????????????????????????????????°???????????£?????????
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int w, h, cnt;
int c[100][100], m[100][100];
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int main() {
    while(scanf("%d%d",&w,&h)) {
        if (w==0&&h==0) break;
        memset(m, -1, sizeof(m)); cnt = 0;
        for (int i=0; i<h; i++) for (int j=0; j<w; j++) scanf("%d",&c[i][j]);
        for (int i=0; i<h; i++) for (int j=0; j<w; j++) {
            if (m[i][j] == -1 && c[i][j] == 1) {
                cnt++;
                queue<P> q;
                q.push(P(i,j));
                m[i][j] = 0;
                while(!q.empty()) {
                    P p = q.front(); q.pop();
                    for (int k=0; k<8; k++) {
                        int ny = p.first + dy[k];
                        int nx = p.second + dx[k];
                        if (nx >= 0 && nx < w && ny >= 0 && ny < h && m[ny][nx] == -1 && c[ny][nx] == 1) {
                            m[ny][nx] = 0;
                            q.push(P(ny,nx));
                        }
                    }
                }
            }
        }
        printf("%d\n",cnt);
    }
}

C++: #include <iostream>
using namespace std;
const int MAX = 51;

int island[MAX][MAX],H,W;
int dx[8]={1,1,1,-1,-1,-1,0,0},dy[8]={0,1,-1,0,1,-1,-1,1};


void dfs(int x,int y){
  island[x][y] = 0;

  for(int i = 0 ; i < 8 ; i++){
    int nx = dx[i] + x;
    int ny = dy[i] + y;
    if(0 <= nx && nx < W && 0 <= ny && ny < H)
      if(island[nx][ny]) dfs(nx,ny);
  }
}

int main(){

  while(cin >> H >> W ,H + W){
    for(int i = 0 ; i < W ; i++)
      for(int j = 0 ; j < H ; j++)
	cin >> island[i][j];
    
    int ans = 0;
    for(int i = 0 ; i < W ; i++){
      for(int j = 0 ; j < H ; j++){
	if(island[i][j]){
	  dfs(i,j);
	  ans++;
	}
      }
    }
    cout << ans << endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int state[100][100];
bool reached[100][100];
int w,h;

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

void dfs(int x, int y) {

	if(x==0 || w<x || y==0 || h<y || state[x][y] == 0)
		return;
	if(reached[x][y] == true)
		return;

	reached[x][y]=true;

	for(int i=0; i<8; i++) {
		dfs(x+dx[i],y+dy[i]);
	}
}

int main() {

	memset(state, 0, sizeof(state));
	memset(reached, false, sizeof(reached));

	cin >> w >> h;
	for(int i=1; i<=h; i++) {
		for(int j=1; j<=w; j++) {
			cin >> state[i][j];
		}
	}
	int count=0;
	for(int i=1; i<=w; i++) {
		for(int j=1; j<=h; j++) {
			if(reached[i][j]==true || state[i][j] == 0)
				continue;
			else{
				dfs(i,j);
				count++;
			}
		}
	}
	cout << count << endl;
	return 0;
}

C++: #include <cstdio>

using namespace std;

int w, h;
int field[50][51];

void dfs(int x, int y)
{
	field[x][y] = 0;
	
	for(int dx = -1; dx <= 1; dx++){
		for(int dy = -1; dy <= 1; dy++){
			int nx = x + dx;
			int ny = y + dy;
			
			if(0 <= nx && nx < w && 0 <= ny && ny < h && field[nx][ny] == 1) dfs(nx, ny);
		}
	}
	return ;
}

void solve(){
	int res = 0;
	for(int i = 0; i < w; i++){
		for(int j = 0; j < h; j++){
			if(field[i][j] == 1){
				dfs(i, j);
				res++;
			}
		}
	}
	printf("%d\n", res);
}

int main()
{
	while(scanf("%d %d", &w, &h) != EOF){
		if(w == 0 && h == 0){ break; }
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				scanf("%d", &field[i][j]);
			}
		}
		solve();
	}
	
	return 0;
}

C++: 
using namespace std;

static const int N = 52;

void dfs(int map[][N], int pi, int pj){
	const int di[] = {-1,0,1,1,1,0,-1,-1};
	const int dj[] = {1,1,1,0,-1,-1,-1,0};

	if( map[pi][pj] == 0 ){
		return ;
	}

	map[pi][pj] = 0;

	for(int i = 0; i < sizeof(di)/sizeof(di[0]); i++){
		dfs(map, pi + di[i], pj + dj[i] );
	}
}

int main(void){
	ws(cin);
	while( 1 ){
		int count = 0;
		int n, m;
		int map[N][N] = {{0,},};

		cin >> n >> m;
		if( n == 0 && m == 0 ){
			break;
		}

		for(int i = 0; i < m; i++ ){
			for(int j = 0; j < n; j++){
				cin >> map[j + 1][i + 1];
			}
		}

		for(int i = 0; i < m; i++ ){
			for(int j = 0; j < n; j++){
				if(map[j + 1][i + 1]){
					dfs(map, j + 1, i + 1);
					count++;
				}
			}
		}

		cout << count << "\n";
	}
	return 0;
}