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>

#define ll long long

using namespace std;

int w,h;

int f[100][100]={};

int solve(int i,int j,int num){
    if(i<0 || j < 0 || i >= h || j >= w )
        return 0;
    if(f[i][j]!=1)
        return 0;
    
    f[i][j]=num;
    solve(i-1,j-1,num);
    solve(i-1,j,num);
    solve(i-1,j+1,num);
    solve(i,j-1,num);
    solve(i,j+1,num);
    solve(i+1,j-1,num);
    solve(i+1,j,num);
    solve(i+1,j+1,num);
    return 0;
}


int main(){

    while(1){
        scanf("%d %d",&w,&h);
        if(w == 0 && h==0)break;

        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                scanf("%d",&f[i][j]);
            }
        }
        int ans = 1;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(f[i][j]==1){
                    ans++;
                    solve(i,j,ans);
                }
            }
        }
        cout << ans-1 << endl;
    }

}

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

int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}, dy[] = {1, -1, 0, 0, 1, -1, -1, 1}, W, H;
bool mem[50][50];

int dfs(int x, int y)
{
  if(!mem[y][x])
    return 0;
  mem[y][x] = 0;
  for(int i = 0; i < 8; i++)
  {
    int tx = x + dx[i];
    int ty = y + dy[i];
    if(tx >= 0 && tx < W && ty >= 0 && ty < H && mem[ty][tx])
      dfs(tx, ty);
  }
  return 1;
}

int main()
{
loop:
  int ans = 0;
  for(int y = 0; y < 50; y++)
    for(int x = 0; x < 50; x++)
      mem[y][x] = 0;
  cin >> W >> H;
  if(!W)
    return 0;
  for(int y = 0; y < H; y++)
    for(int x = 0; x < W; x++)
      cin >> mem[y][x];
  for(int y = 0; y < H; y++)
    for(int x = 0; x < W; x++)
      ans += dfs(x, y);
  cout << ans << endl;
  goto loop;
}

C++: #include<iostream>
#include<math.h>
#include<vector>
using namespace std;

int w,h;
int c[60][60];
int dx[]={1,1,0,-1,-1,-1,0,1};
int dy[]={0,1,1,1,0,-1,-1,-1};
bool seen[60][60];

void func(int i,int j){
    seen[i][j]=true;

    for(int n=0;n<8;++n){
        int ii=i+dy[n];
        int jj=j+dx[n];

        if(ii<0||jj<0||ii>=h||jj>=w)continue;
        if(c[ii][jj]==0)continue;
        if(seen[ii][jj])continue;
        func(ii,jj);
    }
}

int main(){
    while(cin>>w>>h && w!=0){
    int num=0;
    for(int i=0;i<h;++i){
        for(int j=0;j<w;++j){
            cin>>c[i][j];
            seen[i][j]=false;
        }    
    }

    for(int i=0;i<h;++i){
        for(int j=0;j<w;++j){
            if(c[i][j]==1&&!seen[i][j]){
                func(i,j);
                num++;
            }
        }    
    }
    cout<<num<<endl;}
    return 0;
}

C++: #include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <functional>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <tuple>
#include <cassert>
#include <exception>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<P> vp;
#define rep(i,a,n) for(ll i = (a);i < (n);i++)
#define per(i,a,n) for(ll i = (a);i > (n);i--)
#define lep(i,a,n) for(ll i = (a);i <= (n);i++)
#define pel(i,a,n) for(ll i = (a);i >= (n);i--)
#define clr(a,b) memset((a),(b),sizeof(a))
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define sz size()
#define print(X) cout << (X) << endl
static const int INF = 1e+9+7;
ll n,m,l;
string s,t;
int d[200010],dp[1010][1010];
double w[1000],v[1000];
double box[200010];

int mv[3] = {0,-1,1};

void dfs(int x,int y){
  if(dp[x][y] == -1)return;
  dp[x][y] = -1;
  rep(i,0,3){
    rep(j,0,3){
      if(mv[i] == 0 && mv[j] == 0)continue;
      if(dp[x+mv[i]][y+mv[j]] == 1)dfs(x + mv[i],y + mv[j]);
    }
  }
}

int main(){
  while(1){
    cin >> m >> n;
    if(!n && !m)break;
    clr(dp,-1);
    lep(i,1,n){
      lep(j,1,m){
        cin >> dp[i][j];
      }
    }
    ll ans = 0;
    lep(i,1,n){
      lep(j,1,m){
        if(dp[i][j] == 1){
          ans++;
          dfs(i,j);
        }
      }
    }
    print(ans);
  }
  return 0;
}