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>

// using namespace std;

using map_t = std::vector<std::vector<bool>>;

void fill(const map_t& map, int w, int h, int x, int y, map_t& visited) {
  if (x < 0 || w <= x || y < 0 || h <= y) {
    return;
  }
  if (!map[y][x] || visited[y][x]) {
    return;
  }
  visited[y][x] = true;
  fill(map, w, h, x - 1, y - 1, visited);
  fill(map, w, h, x + 0, y - 1, visited);
  fill(map, w, h, x + 1, y - 1, visited);
  fill(map, w, h, x - 1, y + 0, visited);
  fill(map, w, h, x + 1, y + 0, visited);
  fill(map, w, h, x - 1, y + 1, visited);
  fill(map, w, h, x + 0, y + 1, visited);
  fill(map, w, h, x + 1, y + 1, visited);
}

int solve(const map_t& map, int w, int h) {
  map_t visited(h, std::vector<bool>(w, false));
  int ret = 0;
  for (int y = 0; y < h; ++y) {
    for (int x = 0; x < w; x++) {
      if (map[y][x] && !visited[y][x]) {
        ret++;
        fill(map, w, h, x, y, visited);
      }
    }
  }
  return ret;
}

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  while (true) {
    int w, h;
    std::cin >> w >> h;
    if (w == 0 || h == 0) {
      break;
    }
    map_t map(h, std::vector<bool>(w));
    for (int y = 0; y < h; ++y) {
      for (int x = 0; x < w; x++) {
        int a;
        std::cin >> a;
        map[y][x] = a != 0;
      }
    }
    std::cout << solve(map, w, h) << std::endl;
  }
  return 0;
}

C++: #include<cstdio>
int n, m,ans;
int a[100][100];
bool b[100][100];

void dfs(int x,int y) {
	b[x][y] = true;
	for (int i = -1; i < 2; i++) {
		for (int j = -1; j < 2; j++) {
			if (0 <= x + i&&x + i < n) {
				if (0 <= y + j&&y + j < m) {
					if (a[x+i][y+j] == 1 && !b[x+i][y+j])dfs(x+i, y+j);
				}
			}
		}
	}
	return;
}

int main(void) {

	while (true) {
		scanf("%d %d", &m, &n);
		if (n == 0 && m == 0)return 0;
		for (int i = 0; i < 100; i++) {
			for (int j = 0; j < 100; j++) {
				a[i][j] = 0;
				b[i][j] = false;
			}
		}
		for (int i = 0; i < n;i++) {
			for (int j = 0; j < m; j ++) {
				scanf("%d", &a[i][j]);
			}
		}
		ans = 0;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (a[i][j] == 1 && !b[i][j]) {
					dfs(i, j);
					ans++;
				}
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}

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

int w,h;
int field[51][51];
void dfs(int x,int y);

int main(){
	while(cin>>w>>h&&w!=0&&h!=0){
		for(int y=0;y<h;y++){
			for(int x=0;x<w;x++){
				cin>>field[y][x];
			}
		}
		int count=0;
		for(int y=0;y<h;y++){
			for(int x=0;x<w;x++){
				if(field[y][x]==1){
					dfs(x,y);
					count++;
				}
			}
		}
		cout<<count<<endl;
	}

}

void dfs(int x,int y){
	field[y][x]=-1;
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			int mx=x+i;
			int my=y+j;

			if(mx<0||my<0||mx>=w||my>=h)continue;
			if(field[my][mx]==1){
				dfs(mx,my);
			}
		}
	}
}

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

char c[102][102];

int mx, my;

void dfs(int x, int y){
    if(x < 0 || y < 0 || x >= mx || y >= my){
        return;
    }
    if(c[x][y] == '0'){
        return;
    }
    c[x][y] = '0';
    dfs(x+2, y);
    dfs(x+2, y+1);
    dfs(x+2, y-1);
    dfs(x-2, y);
    dfs(x-2, y+1);
    dfs(x-2, y-1);
    dfs(x, y+1);
    dfs(x, y-1);
    return;
}

int main(){
    int W, H, cnt = 0;
    scanf("%d %d\n", &W, &H);
    while(W != 0 || H != 0){
        mx = H;
        my = W;
        for(int k = 0; k < H; k++){
            scanf("%s\n", c[k]);
        }
        for(int i = 0; i < H; i++){
            for(int j = 0; j < W; j++){
                if(c[i][j] == '1'){
                    cnt++;
                    dfs(i, j);
                }
            }
        }
        cout << cnt << endl;
        scanf("%d %d\n", &W, &H);
        cnt = 0;
    }
}