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 <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

#define rep(i,n) for(int i=0; i<n; i++)

short map[50][50];
int w, h;

void dfs(int x, int y) {
    map[x][y] = 0;
    int d[3] = {-1,0,1};
    rep(i, 9) {
        int xx = x+d[i%3];
        int yy = y+d[i/3];
        if( 0<=xx && xx<w && 0<=y && yy<h && map[xx][yy]==1 ) {
            dfs( xx, yy );
        }
    }
}

int main(){
    while( cin >> w >> h, w||h ) {
        int lands = 0;
        rep(y,h) rep(x,w) cin >> map[x][y];
        rep(y, h) {
            rep(x, w) {
                if( map[x][y] == 1 ) {
                    lands++;
                    dfs(x, y);
                }
            }
        }
        cout << lands << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<functional>
#include<string>
#include<cstring>
#include<cmath>
using namespace std;
int cnt;
int w,h;
char map[52][52];
void func(int y,int x)
{
    map[y][x]='0';
    int nx,ny;
    for(int dy=-1;dy<=1;dy++)
    {
        for(int dx=-1;dx<=1;dx++)
        {
            ny=y+dy;
            nx=x+dx;
            if(map[ny][nx]=='1'&&0<=ny&&0<=nx&&ny<h&&nx<w)
            {
                func(ny,nx);
            }
        }
    }
    return;
}
int main()
{
    while(cin>>w>>h,w)
    {
        cnt=0;
        for(int i=0;i<52;i++)
        {
            for(int j=0;j<52;j++)
            {
                map[i][j]='0';
            }
        }
        for(int i=0;i<h;i++)
        {
            for(int j=0;j<w;j++)
            {
                cin>>map[i][j];
            }
        }
        for(int i=0;i<h;i++)
        {
            for(int j=0;j<w;j++)
            {
                if(map[i][j]=='1')
                {
                    func(i,j);
                    cnt++;
                }
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

C++: #include<iostream>
#include<map>
#include<vector>
#include<list>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<numeric>
#include<climits>
#include<ctime>
#include<cstring>
#define REP(i,p,n) for(int i=p;i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define rep_split(tok,a_str,re) for(char *tok = strtok((char *)a_str.c_str(),re); tok != NULL; tok = strtok(NULL,re))
#define ALL(c) (c).begin(), (c).end()
#define dump(a) cerr << #a << "=" << (a) << endl

using namespace std;

int W,H;
int sima_cnt;
int sima_size;
int sima[51][51];
bool is_visit[51][51];

int dw[8] = {1,1,0,-1,-1,-1,0,1};
int dh[8] = {0,1,1,1,0,-1,-1,-1};

void saiki(int w, int h) {
	int aw=0,ah=0;
	rep(i,8) {
		aw = w + dw[i];
		ah = h + dh[i];
		if ((0<=aw && aw<W) && (0<=ah && ah<H)) {
			//次の島を訊ねる条件
			if (sima[aw][ah] && !is_visit[aw][ah]) { 
				is_visit[aw][ah] = true;
				sima_size++;
				saiki(aw,ah);
			}
			else { 
				is_visit[aw][ah] = true;
			}
		}
	}
}

int main() {
	while(cin >> W >> H) {
		if (W==0 && H==0) { break; }
		sima_cnt = 0;
		sima_size = 0;
		rep(h,51) {
			rep(w,51) {
				sima[w][h]=0;
				is_visit[w][h]=0;
			}
		}

		rep(h,H) {
			rep(w,W) {
				cin >> sima[w][h];
			}
		}

		rep(h,H) {
			rep(w,W) {
				sima_size = 0;
				if (sima[w][h] && !is_visit[w][h]) { sima_size++; }
				saiki(w,h);
				if (sima_size>0) {
					sima_cnt++;
				}
			}
		}
		cout << sima_cnt << endl;
	}

	return 0;
}

C++: #include<iostream>
using namespace std;
int w, h, c[50][50], ans = 0;
void solve(int p, int q) {
	c[p][q] = 0;
	for (int i = -1; i < 2; i++) {
		for (int j = -1; j < 2; j++) {
			if (p + i >= 0 && p + i < h && q + j >= 0 && q + j < w) {
				if (c[p + i][q + j]) {
					solve(p + i, q + j);
				}
			}
		}
	}
}
int main() {
	while (cin >> w >> h && w + h) {
		ans = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> c[i][j];
			}
		}
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (c[i][j]) {
					//cout << i << " " << j << endl;
					ans++;
					solve(i, j);
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using mii = map<int, int>;
const int INF = 1e9;
const ll LINF = 1e18;
#define MP(a,b) make_pair((a),(b))
#define MT(...) make_tuple(__VA_ARGS__)

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

#define ALL(x) (x).begin(),(x).end()
#define IN(type,a) type a;cin >> a
#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define COUT(x) cout << (x) << endl
#define DCOUT(x,n) cout << fixed << setprecision(n) << (x) << endl
int w,h;
void dfs(int y, int x, vector<vector<int>>& a, vector<vector<bool>>& check){
    if ((x<0)||(y<0)||(y>=h)||(x>=w)) return;
    if (a[y][x]==0) return;
    if (check[y][x]) return;
    check[y][x] = true;
    for (int i = -1; i<2; i++) {
        for (int j = -1; j<2; j++) {
                dfs(y+i, x+j, a, check);
        }
    }
    
}
int main() {
    while (true) {
        cin >> w >> h;
        if (w==0) {
            break;
        }
        vector<vector<int>> a(h,vector<int>(w));
        vector<vector<bool>> check(h,vector<bool>(w,false));
        int count = 0;
        REP(i, h){
            REP(j, w){
                cin >> a[i][j];
            }
        }
        
        REP(i, h){
            REP(j, w){
                if ((a[i][j]==1)&&(!check[i][j])) {
                    count++;
                    dfs(i, j, a, check);
                }
            }
        }
        COUT(count);
    }

}