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>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <string>
#include <algorithm>
#include <string>
#include <climits>

#define REP(i, n) for (int (i) = 0; (i) < (int)(n); (i)++)
#define REG(i, a, b) for (int (i) = ((int)(a)); (i) < ((int)(b)); i++)
#define ALL(V) (V).begin(), (V).end()
#define PRINT(STR) cout << (STR) << endl

typedef long long ll;
using namespace std;

const int SEA = 0;
const int GROUND = 1;

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

void dfs(int y, int x) {
	if (x < 0 || x >= w) return;
	if (y < 0 || y >= h) return;
	if (board[y][x] == 0) return;
	board[y][x] = 0;
	dfs(y - 1, x);
	dfs(y - 1, x + 1);
	dfs(y, x + 1);
	dfs(y + 1, x + 1);
	dfs(y + 1, x);
	dfs(y + 1, x - 1);
	dfs(y, x - 1);
	dfs(y - 1, x - 1);
}

void calc() {
	int count = 0;
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			if (board[i][j] == GROUND) {
				count++;
				dfs(i, j);
			}
		}
	}
	PRINT(count);
}

int main() {
	cin.tie(0); //対話型には使えない
	ios::sync_with_stdio(false); //printfと共には使えない
	while (cin >> w >> h, w + h != 0) {
		REP(i, h) {
			REP(j, w) {
				char c;
				cin >> c;
				if(c == '0') {
					board[i][j] = SEA;
				} else {
					board[i][j] = GROUND;
				}
			}
		}
		calc();
	}
	return 0;
}

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

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

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

//void printmap() {
//	for (int i = 0 ; i < h; i++) {
//		for (int j = 0; j < w; j++)
//			cout << map[i][j] << " ";
//		cout << endl;
//	}
//}

void oneIsland(int x, int y) {
	for (int i = 0; i < 8; i++) {
		int nextX = x + dx[i];
		int nextY = y + dy[i];
		if (x < 0 || w <= x) continue;
		if (y < 0 || w <= y) continue;
		if (map[nextY][nextX] == 0) continue;
		map[nextY][nextX] = 0;
		oneIsland(nextX, nextY);
	}
}

int main() {
	while (1) {
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> map[i][j];
			}
		}
		
		int count = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (map[i][j] == 1) {
					map[i][j] = 0;
					oneIsland(j, i);
					//printmap();
					count++;
				}
			}
		}
		cout << count <<endl;
	}
}

C++: #include <iostream>

using namespace std;

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

// 0: 海, 1: 陸
int board[50][50] = {{0}};

void DFS(int i, int j, int w, int h) {
  if (board[i][j] == 0) return;
  board[i][j] = 0;  // mark as visited

  for (int d = 0; d < 8; ++d) {
    const int ny = i + dy[d];
    const int nx = j + dx[d];
    if (ny >= 0 && ny < h && nx >= 0 && nx < w) {
      DFS(ny, nx, w, h);
    }
  }
}

int main() {
  int w, h;

  for (;;) {
    cin >> w >> h;
    if (w == 0 && h == 0) {
      break;
    }
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        cin >> board[i][j];
      }
    }

    int ans = 0;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (board[i][j] == 1) {
          DFS(i, j, w, h);
          ++ans;
        }
      }
    }
    cout << ans << endl;
  }
  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+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(9999)d[i/99][i%99]=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>
using namespace std;

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

// »Ýn(x,y)
void dfs(int x, int y){
	// ¡¢éÆ±ëð-1Éu«·¦é
	field[y][x] = -1;

	// Ú®·é8ûüð[v
	for(int dx = -1 ; dx <= 1 ; dx++ ){
		for(int dy = -1 ; dy <= 1 ; dy++ ){
			// xûüÉdx, yûüÉdy, Ú®µ½êð(mx,my)Æ·é
			int mx = x + dx;
			int my = y + dy;
			// ÍÍOÉo½Æ«ÍÌÖ
			if( mx < 0 || my < 0 || mx >= w || my >= h ) continue;
			// ¤n¾Á½çÄAÄÑoµ
			if( field[my][mx] == 1 ){
				dfs( mx , my );
			}
		}
	}
}

int main(){
	while( cin >> w >> h , w||h ){
		for(int y=0 ; y < h ; y++ ){
			for(int x=0 ; x < w ; x++ ){
				cin >> field[y][x];
			}
		}

		int ans = 0;
		for(int y=0 ; y < h ; y++ ){
			for(int x=0 ; x < w ; x++ ){
				if( field[y][x] == 1 ){
					dfs( x , y );
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <iostream>
using namespace std;
int sea[51][51], w, h;
const int di[8] = {-1, 1, 0, 0, 1, -1, -1, 1};
const int dj[8] = {0, 0, -1, 1, 1, 1, -1, -1};

void dfs(int i, int j) {
  sea[i][j] = 0;
  for (int z = 0; z < 8; z++) {
    int ti = i + di[z], tj = j+ dj[z];
    if (ti < 0 || tj < 0 || ti >= h || tj >=w) continue;
    if (sea[ti][tj]) dfs(ti, tj);
  }
}

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

int main (int argc, char const* argv[]) {
  while (cin>>w>>h, w||h) {
    solve();
  }
  return 0;
}

C++: #include <cstdio>
#define f(i,n)for(int i=0;i<n;i++)
#define s (scanf("%d",&e),e)
int main(){int a=0,w,n,m,b,e,d[99];f(i,99)d[i]=0;n=s;m=s;if(n==0)return 0;f(i,m){w=b=0;f(j,n){s;if(e)f(k,3){e=d[j+k];if(e&&e-w){if(w){f(l,n+2)d[l]=d[l]==e?w:d[l];a--;}else w=e;}if(w==0)a++,w=i*n+j+1;}d[j]=b;b=w;}d[n]=b;}printf("%d\n",a);main();}