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 unsigned long long ull;
typedef pair<ll, int> P;
typedef pair<ll, pair<ll, ll> > P3;

const ll MOD = ll(1e9 + 7);
const ll LLINF = LLONG_MAX;
const int IINF = INT_MAX;
const int MAX_N = int(1e5) + 5;
const double EPS = 1e-8;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)

int a[55][55]{};
int h,w;

void dfs(int i, int j){
    a[i][j] = 0;
    for(int di = -1;di<=1;++di){
        for(int dj = -1; dj <= 1; ++dj){
            int ni = i+di, nj = j+dj;
            if(ni<0 || ni>=h)continue;
            if(nj<0 || nj>=w)continue;
            if(a[ni][nj]==0)continue;
            dfs(ni,nj);
        }
    }
}

int main() {
    while(1){
        cin >> w >> h;
        if(h==0 && w==0)break;
        REP(i,h)REP(j,w)cin >> a[i][j];
        int ans = 0;
        REP(i,h)REP(j,w)if(a[i][j]){
            dfs(i,j);
            ans++;
        }
        cout << ans << endl;
    }
    
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int c[50][50];
bool vis[50][50];
int W,H;
void dfs(int x,int y) {
    vis[y][x]=1;
    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>=W||0>ny||ny>=H||c[ny][nx]==0||vis[ny][nx]) continue;
            dfs(nx,ny);
        }
    }
}
int main() {
    while(scanf("%d %d",&W,&H),W) {
        for(int i=0;i<H;i++) {
            for(int j=0;j<W;j++) scanf("%d",&c[i][j]);
        }
        memset(vis,0,sizeof(vis));
        int ans=0;
        for(int i=0;i<H;i++) {
            for(int j=0;j<W;j++) {
                if(c[i][j]==1&&!vis[i][j]) {
                    dfs(j,i);
                    ans++;
                }
            }
        }
        printf("%d\n",ans);
    }
}

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

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

int w, h;

void crawler(int y, int x) {
	field[y][x] = 0;
	for (int i = 0; i < 8; i++) {
		int ny = y + dy[i];
		int nx = x + dx[i];
		if (nx < 0 || ny < 0 || w <= nx || h <= ny)continue;
		if (field[ny][nx] == 1)crawler(ny, nx);
	}
}

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

C++: #include <bits/stdc++.h>
#include <cassert>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const long long MOD = 1000000007;
const long long INF = 1LL << 60;

int w, h, x, y;

void dfs(vector<vector<int>> &map, int x, int y)
{
  // 今いるところを0に置き換える
  map[x][y] = 0;

  for (int dx = -1; dx <= 1; dx++)
  {
    for (int dy = -1; dy <= 1; dy++)
    {
      // x方向にdx移動したものをnx，y方向にdy移動したものをnyとおく
      int nx = x + dx, ny = y + dy;

      // nxとnyが地図の範囲内かどうかとmap[nx][ny]が陸なのかを判定
      if (0 <= nx && nx < h && 0 <= ny && ny < w && map[nx][ny] == 1)
        dfs(map, nx, ny);
    }
  }
  return;
}

void solve(vector<vector<int>> &map)
{
  int ans = 0;
  for (int i = 0; i < h; i++)
  {
    for (int j = 0; j < w; j++)
    {
      if (map[i][j] == 1)
      {
        dfs(map, i, j);
        ans++;
      }
    }
  }
  cout << ans << endl;
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  while (1)
  {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    vector<vector<int>> map(h, vector<int>(w));
    for (int i = 0; i < h; i++)
    {
      for (int j = 0; j < w; j++)
      {
        cin >> map[i][j];
      }
    }
    solve(map);
  }
  return 0;
}

C++: #include<iostream>
#include <cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<utility>
#include<queue>

#define REP(i,n) for(int i = 0;i < n ;i++)
	
using namespace std;

int mp[60][60],dp[60][60];
int w,h,ans;
int dx[] = {1,1,0,-1,-1,-1,0,1},dy[] = {0,1,1,1,0,-1,-1,-1};

void dfs(int x,int y)
{
	REP(i,8)
	{
		int cx = x + dx[i],cy = y + dy[i];
		if(cx >= 0 && cx < h && cy >= 0 \
				&& cy < w && dp[cx][cy] == 0 && mp[cx][cy] == 1)
				{
					dp[cx][cy] = 1;
					dfs(cx,cy);
				
				}
				
	}
}
			

int main()
{
	for(;;)
	{
		ans = 0;
		REP(i,60)REP(j,60)dp[i][j]=0;
		cin >> w >> h;
		if(w == 0 && h == 0) break;
		REP(i,h)REP(j,w)cin >> mp[i][j];
		REP(i,h)REP(j,w)
		{
			if(mp[i][j] == 1 && dp[i][j] == 0)
			{
				ans++;
				dfs(i,j);
			}
		}
		
		cout << ans << endl;
		
	}
	return 0;

}