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;

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 long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int,int,int> tpl;

#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

#define en "\n"

const double EPS = 1e-9;
const double PI  = acos(-1.0);
const int INT_INF = 2147483647;
const long long LL_INF = 1LL<<60;
const long long MOD = 1000000007;

#define CLR(a) memset((a), 0 ,sizeof(a))

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

int h,w;
int c[55][55];
bool done[55][55];
vector<pair<int,int>> v{P(-1,-1),P(-1,0),P(-1,1),P(0,-1),P(0,1),P(1,-1),P(1,0),P(1,1)};

void dfs(int x, int y){
    if(done[x][y]) return;
    done[x][y] = true;
    
    for(P p : v){
        int i = p.first, j = p.second;
        if(0<=x+i && x+i<h && 0<=y+j && y+j<w && c[x+i][y+j]) dfs(x+i,y+j);
    }
}

int main(void){

    cin >> w >> h;
    while(h!=0 || w!=0){
        CLR(c); CLR(done);
        REP(i,h)REP(j,w) cin >> c[i][j];

        int ans = 0;
        REP(i,h)REP(j,w){
            if(done[i][j] || c[i][j]==0) continue;
            dfs(i,j);
            ans++;
        }
        cout << ans << en;
        cin >> w >> h;
    }
    return 0;
}

C++: #include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define INF 1000000000
#define mod 1000000007
int H,W;
int hw[50][50];
void dfs(int x,int y){
	hw[x][y]=0;
	for(int dx=-1;dx<=1;dx++){
		for(int dy=-1;dy<=1;dy++){
			int nx=x+dx,ny=y+dy;
			if(0<=nx && nx<H && 0<=ny && ny<W && hw[nx][ny]==1) dfs(nx,ny);
		}
	}
	return;
}

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

	st:
	int ans=0;
	cin>>W>>H;
	if(H==0 && W==0) return 0;
	REP(i,H){
		REP(j,W){
			cin>>hw[i][j];
		}
	}
	REP(i,H){
		REP(j,W){
			if(hw[i][j]==1){
				ans++;
				//cout<<i<<" "<<j<<endl;
				dfs(i,j);
			}
		}
	}
	
	cout<<ans<<endl;
	goto st;
		
	return 0;
}

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

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
#define rrep(i, m, n) for(int (i)=(m); (i)<(n);  (i)++)
#define erep(i, m, n) for(int (i)=(m); (i)<=(n); (i)++)
#define  rep(i, n)    for(int (i)=0; (i)<(n);  (i)++)
#define rrev(i, m, n) for(int (i)=(n)-1; (i)>=(m); (i)--)
#define erev(i, m, n) for(int (i)=(n); (i)>=(m); (i)--)
#define  rev(i, n)    for(int (i)=(n)-1; (i)>=0; (i)--)
#define vrep(i, c)    for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++)
#define  ALL(v)       (v).begin(), (v).end()
#define pb            push_back
template<class T, class S> inline pair<T, S> mp(T x, S y){ return make_pair(x, y); }
template<class T, class S> inline bool minup(T& m, S x){ return m>(T)x ? (m=(T)x, true) : false; }
template<class T, class S> inline bool maxup(T& m, S x){ return m<(T)x ? (m=(T)x, true) : false; }

static const int    INF = 1000000000;
static const ll     MOD = 1000000007LL;
static const double EPS = 1E-12;

const int MAX_W = 50;
const int MAX_H = 50;
int w, h;
int c[MAX_W][MAX_H+1];

inline bool check(int hx, int hy){ return 0 <= hx && hx < w && 0 <= hy && hy < h; }

int main()
{
  while(cin >> h >> w, w){
    int res = 0;
    rep(i, w) rep(j, h) cin >> c[i][j];

    rep(i, w) rep(j, h) if(c[i][j]){
      stack<pii> st;
      st.push(mp(i, j));
      c[i][j] = 0;
      res += 1;
      while(!st.empty()){
        pii here = st.top();  st.pop();
        int hx = here.first;
        int hy = here.second;
        erep(dx, -1, 1) erep(dy, -1, 1) if(dx || dy){
          int nx = hx + dx;
          int ny = hy + dy;
          if(!check(nx, ny)) continue;
          if(!c[nx][ny]) continue;
          st.push(mp(nx, ny));
          c[nx][ny] = 0;
        }
      }
    }

    cout << res << endl;
  }

  return 0;
}

C++: #include <iostream>

int h, w;
bool island[51][51];

bool func(int y, int x){
	if(!island[y][x]) return false;
	island[y][x] = false;

	for(int i = -1; i <= 1; ++i){
		for(int j = -1; j <= 1; ++j){
			if(i == 0 && j == 0) continue;
			if(y + i < 0 || y + i >= h || x + j < 0 || x + j >= w) continue;
			if(!island[y + i][x + j]) continue;
			func(y + i, x + j);
		}
	}

	return true;
}

int main(){
	while(std::cin >> w >> h, h + w){
		for(int i = 0; i < h; ++i)
			for(int j = 0; j < w; ++j){
				std::cin >> island[i][j];
			}

		int cnt = 0;
		for(int i = 0; i < h; ++i){
			for(int j = 0; j < w; ++j){
				cnt += func(i, j);
			}
		}

		std::cout << cnt << std::endl;
	}

	return 0;
}