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;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)   FOR(i,0,n)
#define ALL(a)     (a).begin(),(a).end()
#define VI         vector<int>
#define MOD 1000000007

typedef long long int ll;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

const ll INF=(ll)1e19;
//const int INF=(1<<30);
int h,w;
int m[50][50];

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

int dfs(int x, int y, int d){
  if(m[x][y]==0)return d;
  if(visited[x][y])return d;
  visited[x][y]=true;

  REP(i,8){
    int nx = x + dx[i];
    int ny = y + dy[i];

    if(nx < 0 || nx >= h || ny <0 || ny >= w)continue;
    if(m[nx][ny]==0)continue;
    if(visited[nx][ny])continue;

    dfs(nx, ny, d);
  }

  return d+1;
}

int main(){
  while(true){

    cin >> w >> h;
    if(h+w==0)break;
    REP(i,h)REP(j,w)cin >> m[i][j];
    REP(i,h)REP(j,w)visited[i][j]=false;

    int d = 0;
    REP(i,h)REP(j,w){
      d = dfs(i,j,d);
    }
    cout << d << endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
using Int = long long;
Int const INF = 1e10;

int w,h; int fie[50][50];
void dfs(int y,int x){
    fie[y][x]=0;
    for (int i = -1; i <= 1; i++){
        for (int j = -1; j <= 1; j++){
            int ny=y+i,nx=x+j;
            if(ny>=0&&ny<h&&nx>=0&&nx<w&&fie[ny][nx]==1)dfs(ny,nx);
        }
    }
}

int main(){
    cin.tie(0); ios::sync_with_stdio(false);

    while(true){
        cin>>w>>h; if(w+h==0)break;
        for (int i = 0; i < h; i++){
            for (int j = 0; j < w; j++) cin>>fie[i][j];
        }
        int ans=0;
        for (int i = 0; i < h; i++){
            for (int j = 0; j < w; j++){
                if(fie[i][j]==1){
                    dfs(i,j);
                    ans++;
                }
            }
        }
        cout<<ans<<'\n';
    }
}

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


int islands[50][100];
int w,h;

void dfs(int y,int x){

  islands[y][x] = 0;

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

  for(int i = 0;i < 8;i++){
    int nx = x + dx[i];
    int ny = y + dy[i];
    if(0 <= nx && nx < w && 0 <= ny && ny < h && islands[ny][nx] == 1)dfs(ny,nx);
  }

  return ;
}


int main(){

  vector<int> v;

  while(scanf("%d %d",&w,&h),w != 0 && h != 0){

    int ans = 0;

    for(int i = 0;i < h;i++){
      for(int j = 0;j < w;j++){
        scanf("%d",&islands[i][j]);
      }
    }

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


    v.push_back(ans);
  }

  for(int i = 0;i < v.size();i++){
    printf("%d\n",v[i]);
  }

  return 0;
}

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

int c[MAX][MAX];

void dfs(int w, int h, int x, int y);
int main(int argc, char const *argv[]){
    int w, h, ans;

    while (cin>>w>>h, w || h){
        ans = 0;
        for (int i = 0; i < h; i++){
            for (int j = 0; j < w; j++){
                cin>>c[i][j];
            }
        }
        for (int i = 0; i < h; i++){
            for (int j = 0; j < w; j++){
                if (c[i][j] == 1){
                    dfs(w, h, j, i);
                    ans++;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

void dfs(int w, int h, int x, int y){
    int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
    int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
    c[y][x] = 0;

    for (int i = 0; i < 8; i++){
        int nx = x + dx[i];
        int ny = y + dy[i];

        if (0 <= nx && nx < w && 0 <= ny && ny < h && c[ny][nx] == 1){
            dfs(w, h, nx, ny);
        }
    }
    return;
}

C++: #define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<sstream>
#include<algorithm>
#include<numeric>
#include<cmath>
#include<bitset>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<queue>
#include<deque>
using namespace std;

void erase_island (int ocean[][50], int h, int w, int i, int j) {
    ocean[i][j] = 0;
    for(int dy = -1; dy <= 1; dy++) {
        for(int dx = -1; dx <= 1; dx++) {
            if (i + dy >= 0 && i + dy < h && j + dx >= 0 && j + dx < w) {
                if(ocean[i+dy][j+dx] == 1)  erase_island(ocean, h, w, i+dy, j+dx);
            }
        }
    }
}

int main() {

    int w, h;
    while(cin >> w >> h && (w || h)) {
        int ocean[h][50];
        for(int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                cin >> ocean[i][j];
            }
        }
        int answer = 0;
        for(int i = 0; i < h; i++) {
            for(int j = 0; j < w; j++) {
                if (ocean[i][j] == 1) {
                    erase_island(ocean, h, w, i, j);
                    answer += 1;
                }
            }
        }
        cout << answer << endl;
    }
     
    return 0;
}