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 <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int field[52][52];

void dfs(int y, int x)
{
  static const int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1};
  static const int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1};

  if (field[y][x] != 1)
    return;
  field[y][x] = 2;
  for (int i = 0; i < 8; ++i) {
    dfs(y+dy[i], x+dx[i]);
  }
}

int main()
{
  int w, h;
  while (scanf("%d%d", &w, &h), w|h) {
    memset(field, 0, sizeof(field));
    for (int i = 1; i <= h; ++i) {
      for (int j = 1; j <= w; ++j) {
        scanf("%d", &field[i][j]);
      }
    }
    int count = 0;
    for (int i = 1; i <= h; ++i) {
      for (int j = 1; j <= w; ++j) {
        if (field[i][j] == 1) {
          ++count;
          dfs(i, j);
        }
      }
    }
    printf("%d\n", count);
  }
}

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

using ll=long long;
#define int ll

#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)

int read(){
	int i;
	scanf("%lld",&i);
	return i;
}

int land[1000][1000]={-1};
int flag[1000][1000]={-1};
int dy[]={-1,-1,-1,0,0,1,1,1};
int dx[]={-1,0,1,-1,1,-1,0,1};
int cnt=0;

void isLand(int i,int j){
	REP(k,8){
		int newx=i+dx[k];
		int newy=j+dy[k];
		if(land[newx][newy]){
			if(flag[newx][newy]!=-1){
				flag[i][j]=flag[newx][newy];
				return;
			}
		}
	}
	cnt++;
	flag[i][j] = cnt;
}

signed main(){
	int w,h;
	while( w=read(),h=read(),w||h){
		cnt=0;
		
		REP(i,1000){
			REP(j,1000){
				land[i][j]=-1;
				flag[i][j]=-1;
			}
		}
		
		FOR(i,1,w+1){
			FOR(j,1,h+1){
				land[i][j]=read();
			}
		}
		
		FOR(i,1,w+1){
			FOR(j,1,h+1){
				if(land[i][j])isLand(i,j);
			}
		}
		
		cout << cnt << endl;
	}
	
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<numeric>
#include<vector>
#include<cstring>
#define foreach(t,p) for(t::iterator it=p.begin();it!=p.end();p++)
#define REP(i,n) for(int i=0;i<n;i++)
using namespace std;
#define MAX_W 50

bool island[MAX_W+3][MAX_W+3];

int solve(int x,int y)
{
	if(!island[x][y]) {return 0;}

	island[x][y]= false;
	solve(x-1,y-1);
	solve(x-1,y);
	solve(x-1,y+1);
	solve(x,y-1);
	solve(x,y+1);
	solve(x+1,y+1);
	solve(x+1,y);
	solve(x+1,y-1);
//	cout << x << " " << y << endl;
	return 1;
}


int main()
{
	int w,h;
	while(cin >> w >> h &&w && h)
	{
		REP(i,MAX_W+2)REP(j,MAX_W+2) island[i][j]=false;
		REP(i,h)REP(j,w){cin >> island[i+1][j+1];}
		int kosu = 0;
		REP(i,h)REP(j,w){kosu += solve(i+1,j+1);}
		cout << kosu << endl;
		
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

void dfs(int y, int x, vector<vector<int> > &C, vector<vector<int> > &used) {
    used.at(y).at(x) = 1;
    int H = C.size();
    int W = C.at(0).size();
    for (int dy = -1; dy <= 1; ++dy) {
        for (int dx = -1; dx <= 1; ++dx) {
            int ny = y + dy;
            int nx = x + dx;
            if (ny < 0 || H <= ny) continue;
            if (nx < 0 || W <= nx) continue;
            if (used.at(ny).at(nx)) continue;
            if (C.at(ny).at(nx) == 0) continue;
            dfs(ny, nx, C, used);
        }
    }
}

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

    while (true) {
        int W, H;
        cin >> W >> H;
        if (W == 0 && H == 0) break;
        vector<vector<int> > C(H, vector<int>(W));
        vector<vector<int> > used(H, vector<int>(W, 0));
        for (int y = 0; y < H; ++y) {
            for (int x = 0; x < W; ++x) {
                cin >> C.at(y).at(x);
            }
        }

        int ans = 0;
        for (int y = 0; y < H; ++y) {
            for (int x = 0; x < W; ++x) {
                if (C.at(y).at(x) == 1 && used.at(y).at(x) == 0) {
                    ans++;
                    dfs(y, x, C, used);
                }
            }
        }

        cout << ans << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <set>
#include <map>
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define REPI(i, s, e) for (int i = (s); i <= (e); i++)
#define rep(i, n) REP(i, 0, n)
#define repi(i, n) REPI(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

#define dump(x) (cout << #x << " = " << x << endl)
#define dump2(x, y) (cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl)

typedef long long ll;
typedef pair<int, int> pii;

#define MAX 50
int brd[MAX][MAX];
int h, w;

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

int dfs(int y, int x)
{
	if (!(0 <= y && y < h)) return 0;
	if (!(0 <= x && x < w)) return 0;
	if (brd[y][x] == 0) return 0;
	brd[y][x] = 0;
	rep(d, 8) dfs(y+dy[d], x+dx[d]);
	return 1;
}

int main(void)
{
	while (cin >> w >> h, w|h) {
		rep(j, h) rep(i, w) cin >> brd[j][i];

		int cnt = 0;
		rep(j, h) rep(i, w) cnt += dfs(j, i);
		cout << cnt << endl;
	}
	return 0;
}