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;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb emplace_back
#define INF (1e9+1)
//#define INF (1LL<<59)

vector<vector<int>> v;

int w,h;
void dfs(int y, int x, vector<vector<bool>> &used){
    for(int dy=-1;dy<=1;dy++){
        for(int dx=-1;dx<=1;dx++){
            if(dy==0 && dx==0)continue;
            int ddy = y+dy, ddx = x+dx;
            if(ddx<0 || ddy<0 || ddx>=w || ddy>=h)continue;
            if(v[ddy][ddx]!=1 || used[ddy][ddx]==true)continue;
            used[ddy][ddx] = true;
            dfs(ddy,ddx,used);
        }
    }
}

int main(){
    while(cin>>w>>h&&(w||h)){
        
        v.clear();
        v.resize(h,vector<int>(w));
        rep(i,h)rep(j,w)cin>>v[i][j];
        
        int ans = 0;
        vector<vector<bool>> used(h,vector<bool>(w,false));
        rep(i,h){
            rep(j,w){
                if(used[i][j]==false && v[i][j]==1){
                    used[i][j] = true;
                    dfs(i,j,used);
                    ans++;
                }
            }
        }
        cout<<ans<<endl;
    }
}

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

constexpr int MAX_H = 55;
constexpr int MAX_W = 55;

int H, W;
int grid[MAX_H][MAX_W];
bool reached[MAX_H][MAX_W];

int dh[8] = {-1, -1, -1,  0,  0,  1,  1,  1};
int dw[8] = {-1,  0,  1, -1,  1, -1,  0,  1};

void dfs(int h, int w) {
  reached[h][w] = true;
  for (int i{}; i < 8; ++i) {
    int nh = h + dh[i];
    int nw = w + dw[i];
    if (nh < 0 || nh >= H || nw < 0 || nw >= W ||
        reached[nh][nw] || grid[nh][nw] == 0)
      continue;
    dfs(nh, nw);
  }
}

int main() {
  while (true) {
    cin >> H >> W;
    if (H == 0 && W == 0)
      break;
    for (int h{}; h < H; ++h) {
      for (int w{}; w < W; ++w) {
        reached[h][w] = false;
        cin >> grid[h][w];
      }
    }

    int island{};
    for (int h{}; h < H; ++h) {
      for (int w{}; w < W; ++w) {
        if (grid[h][w] == 0 || reached[h][w])
          continue;
        dfs(h, w);
        ++island;
      }
    }

    cout << island << endl;
  }

  return 0;
}

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

int myMap[50][50];
int w, h;
int ans;

bool safe(int x, int y){
    return (0 <= x && 0 <= y) && (x < w && y < h);
}

void del(int x, int y){
    myMap[x][y] = 0;
    if (safe(x+1, y) && myMap[x+1][y]) del(x+1, y);
    if (safe(x+1, y+1) && myMap[x+1][y+1]) del(x+1, y+1);
    if (safe(x+1, y-1) && myMap[x+1][y-1]) del(x+1, y-1);
    if (safe(x, y+1) && myMap[x][y+1]) del(x, y+1);
    if (safe(x, y-1) && myMap[x][y-1]) del(x, y-1);
    if (safe(x-1, y-1) && myMap[x-1][y-1]) del(x-1, y-1);
    if (safe(x-1, y+1) && myMap[x-1][y+1]) del(x-1, y+1);
    if (safe(x-1, y) && myMap[x-1][y]) del(x-1, y);
    return;
}

int main(){

    while (1){
	cin >> w >> h;
	if (w == 0 && h == 0) break;

	for (int i = 0; i < w; i++){
	    for (int j = 0; j < h; j++){
		cin >> myMap[i][j];
	    }
	}

	ans = 0;	
	for (int i = 0; i < w; i++){
	    for (int j = 0; j < h; j++){
		if (myMap[i][j] == 1) {
		    ans++;
		    del(i, j);
		}
	    }
	}

	cout << ans << endl;
    }
    
}

C++: #include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>

using namespace std;

#define INF 999999999

int map[52][52];
bool ch[52][52];
int ans;

void dfs(int map[52][52], bool c[52][52], int x, int y){
	//cout << x << " " << y << " " << ans << endl;
	c[x][y] = 1;
	if(map[x-1][y]==1 && c[x-1][y]==0) dfs(map,c,x-1,y);
	if(map[x-1][y-1]==1 && c[x-1][y-1]==0) dfs(map,c,x-1,y-1);
	if(map[x-1][y+1]==1 && c[x-1][y+1]==0) dfs(map,c,x-1,y+1);
	if(map[x+1][y]==1 && c[x+1][y]==0) dfs(map,c,x+1,y);
	if(map[x-1][y-1]==1 && c[x-1][y-1]==0) dfs(map,c,x-1,y-1);
	if(map[x-1][y+1]==1 && c[x-1][y+1]==0) dfs(map,c,x-1,y+1);
	if(map[x][y-1]==1 && c[x][y-1]==0) dfs(map,c,x,y-1);
	if(map[x][y+1]==1 && c[x][y+1]==0) dfs(map,c,x,y+1);
}

int main(){
	int i, j, w, h;
	while(1){
		cin >> w >> h;
		if(!w) break;
		for(i=0;i<=h+1;i++){
			for(j=0;j<=w+1;j++){
				if(i==0||i==h+1||j==0||j==w+1)map[i][j]=0;
				else cin >> map[i][j];
				ch[i][j] = 0;
			}
		}
		ans = 0;
		for(i=1;i<=h;i++){
			for(j=1;j<=w;j++){
				if(map[i][j]&&!ch[i][j]){
					dfs(map,ch,i,j);
					ans++;
				}
			}
		}
		
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
int h,w;
int G[1000][1000];
int dx[] = {-1,0,1,-1,1,-1,0,1},dy[] = {-1,-1,-1,0,0,1,1,1};

void dfs(int px,int py){
  G[px][py] = 0;
  for(int i = 0 ; i < 8 ; i++ ){
    int nx = px + dx[i],ny = py + dy[i];
    if(0 <= nx && nx < w && 0 <= ny && ny < h && G[nx][ny] == 1){
      dfs(nx,ny);
    }
  }
}

int main(){
  while( cin >> w >> h,w+h){

    for(int i = 0 ; i < h ; i++){
      for(int j = 0 ; j < w ; j++){
	cin >> G[j][i];
      }
    }

    int cnt = 0;
    for(int i = 0 ; i < h ; i++){
      for(int j = 0 ; j < w ; j++){
	if(G[j][i] == 1){
	  dfs(j,i);
	  cnt++;
	}
      }
    }
    cout << cnt << endl;
  }
  return 0;
}