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 <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>

using namespace std;

inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}

typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EACH(t,i,c) for(t::iretator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

const double EPS = 1e-10;

int seamap[52][52];
int num = 0;

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

		if(seamap[x-1][y-1]) {
			dfs(x-1, y-1);
		}
		if(seamap[x][y-1]) {
			dfs(x, y-1);
		}
		if(seamap[x+1][y-1]) {
			dfs(x+1, y-1);
		}
		if(seamap[x-1][y]) {
			dfs(x-1, y);
		}
		if(seamap[x+1][y]) {
			dfs(x+1, y);
		}
		if(seamap[x-1][y+1]) {
			dfs(x-1, y+1);
		}
		if(seamap[x][y+1]) {
			dfs(x, y+1);
		}
		if(seamap[x+1][y+1]) {
			dfs(x+1, y+1);
		}
	}
}


int main() {
	int w, h;

	REP(i, 52) {
		REP(j, 52) {
			seamap[i][j] = 0;
		}
	}

	while(cin >> w >> h, (w!=0 && h!=0)) {
		FOR(i, 0, 52) {
			FOR(j, 0, 52) {
				seamap[i][j] = 0;
			}
		}

		FOR(i, 1, h+1) {
			FOR(j, 1, w+1) {
				int p;
				cin >> p;
				seamap[i][j] = p;
			}
		}

		num = 0;

		FOR(i, 1, 50) {
			FOR(j, 1, 50) {
				if(seamap[i][j]) {
					num++;
					dfs(i, j);
				}
			}
		}

		cout << num << endl;
	}
}

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

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
using ll = long long;
string char_to_string(char val) {
  return string(1, val);
}
int char_to_int(char val) {
  return val - '0';
}
template<class T> inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template<class T> inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int vector_finder(std::vector<ll> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance( vec.begin(), itr );
  if (index != vec.size()) { // 発見できたとき
    return 1;
  }
  else { // 発見できなかったとき
    return 0;
  }
}

int w, h;
vector<vector<int>> c;
int ans;

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

void dfs(int _x, int _y) {
  // 訪問したマスは0に変えて
  c[_x][_y] = 0;
  // 次行くマスを決めて
  for(int i = 0; i < 8; ++i) {
    if(_x + dx[i] < 0 || _x + dx[i] >= h || _y + dy[i] < 0 || _y + dy[i] >= w) continue;
    if(c[_x + dx[i]][_y + dy[i]] == 1) dfs(_x + dx[i], _y + dy[i]);
  }
}

int main() {
  while(true) {
    cin >> w >> h;
    c.resize(h);
    if(w == 0 && h == 0) break;
    REP(k, h) {
      REP(m, w) {
        int C; cin >> C;
        c[k].push_back(C);
      }
    }
    ans = 0;
    REP(i, h) {
      REP(k, w) {
        if(c[i][k] == 1) {
          dfs(i, k);
          ans++;
        }
      }
    }
    cout << ans << endl;
    c.clear();
  }
}

C++: #include <iostream>

using namespace std;

int map[50][50];

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

void dfs(int x, int y,int w, int h){
	if(x<0 || y<0 || w<=x || h<=y || map[y][x] == 0) return;
	else{
		map[y][x] = 0;
		for(int i=0;i<8;++i){
			dfs(x+dx[i],y+dy[i],w,h);
		}
	}
}

int solve(int w,int h){
	int ans;
	
	int i,j,k;
	ans = 0;
	for(i=0;i<h;++i){
		for(j=0;j<w;++j){
			if(map[i][j]==1){
				map[i][j] = 0;
				for(int k=0;k<8;++k){
					dfs(j+dx[k],i+dy[k],w,h);
				}
				ans++;
			}
		}
	}
	return ans;
}

int main(void){
	int w,h;
	int i,j;
	
	while(cin>>w>>h,w){
		for(i=0;i<h;++i){
			for(j=0;j<w;++j){
				cin >> map[i][j];
			}
		}
		
		cout << solve(w,h) << endl;
	}
	
}

C++: #include "bits/stdc++.h"

using namespace std;

typedef long long ll;

const ll INF=1e13+7;
const int dx[]={0,1,0,-1,1,-1,-1,1};
const int dy[]={1,0,-1,0,1,1,-1,-1};

template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}
template <class T, class Y>T GCD(T a, Y b){if(a<b){int c=a;a=b;b=c;}if (a % b == 0){return b;}return GCD(b, a % b);}
#define rep(i,n) for(int i=0;i<(n);i++)

int Place[60][60];
bool seen[60][60];
int W,H;

void DFS(int h,int w){
    seen[h][w]=true;

    for(int i=0;i<8;i++){
        int nw=w+dx[i];
        int nh=h+dy[i];

        if(nw<0||nw>=W||nh<0||nh>=H)continue;
        if(Place[nh][nw]==0)continue;

        if(seen[nh][nw]==true)continue;

        DFS(nh,nw);
    }
}

int Input(){
    int Co=0;
    cin>>W>>H;
    if(W==0&&H==0) return -1;
    rep(i,H){
        rep(j,W)cin>>Place[i][j];
    }
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            if(Place[i][j]==1&&seen[i][j]==false){
                Co++;
                DFS(i,j);
            }
        }
    }
    return Co;
}

int main(){
    vector<int> answers(0);
    int ans=0,num=0;
    while(1){
        memset(seen,false,sizeof(seen));
        ans=Input();
        if(ans==-1)break;
        answers.push_back(ans);
        num++;
    }
    rep(i,num)cout<<answers[i]<<endl;
    return 0;
}

//a=97,z=122