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;
#define REP(i,n) for(int i=0; i<(n); i++)
#define REP2(i,x,n) for(int i=x; i<(n); i++)
#define PRINT(x) cout<<(x)<<endl
#define INF 1000000000000 // 10^12
#define MOD 1000000007 // 10^9+7
#define PB push_back
#define MP make_pair

const int MAX_W = 50, MAX_H = 50;

int w, h, c[MAX_H][MAX_W];

bool visited[MAX_H][MAX_W];

void dfs(int, int);

int main() {
    while (true) {
        // input
        cin >> w >> h;
        
        if (w == 0 && h == 0) break;

        REP(i, h) {
            REP(j, w) cin >> c[i][j];
        }

        // init visited
        REP(i, h) {
            REP(j, w) visited[i][j] = false;
        }

        // count island by DFS 
        int cnt_island = 0;
        REP(i, h) {
            REP(j, w) {
                if (visited[i][j] == false && c[i][j] == 1) {
                    dfs(i, j);
                    cnt_island++;
                }
            }
        }

        // output
        PRINT(cnt_island);
    }
    return 0;
}

void dfs(int x, int y) {
    visited[x][y] = true;
    for (int dx = -1; dx <= 1; dx++) {
        for (int dy = -1; dy <= 1; dy++) {
            int nx = x + dx;
            int ny = y + dy;
            if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue;
            if (visited[nx][ny] || c[nx][ny] == 0) continue;
            dfs(nx, ny);
        }
    }
}

C++: 
#include <iostream>
#include <vector>

using namespace std;

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

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

int dp[55][55];
int c[55][55];

int check(int x, int y)
{
	if (dp[x][y]) return 0;
	if (c[x][y] == 0) return 0;
	dp[x][y] = 1;
	
	rep(i, 8) {
		int xx = x + dx[i];
		int yy = y + dy[i];
		int take = check(xx, yy);
	}
	return 1;
}

int main(void)
{
	while(1) {
		int w, h;
		cin >> w >> h;
		if (w == 0) break;
		rep(i, 52) rep(j, 52) c[i + 1][j + 1] = 0;
		rep(i, h) rep (j, w) cin >> c[i + 1][j + 1];
		rep(i, 52) rep(j, 52) dp[i][j] = 0;
		rep(i, 55) {
			dp[0][i] = 1;
			dp[i][0] = 1;
			dp[h + 1][i] = 1;
			dp[i][w + 1] = 1;
		}

		int ans = 0;
		rep(i, h) rep(j, w) if (c[i + 1][j + 1]) ans += check(i + 1, j + 1);
		cout << ans << endl;
	}
	return 0;
}

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

int map[MAX][MAX];
int ans, w, h;
int dx[] = { 1, 0, -1, 0, 1, 1, -1, -1 };
int dy[] = { 0, 1, 0, -1, -1, 1, 1, -1 };

void dfs(int x, int y) {
	if (x < 0 || x >= w || y < 0 || y >= h) return;
	if (map[y][x]==0) return;
	map[y][x] = 0;
	for (int i=0; i<8; i++)
		dfs(x+dx[i], y+dy[i]);
}

int main() {
	while (cin >> w >> h, w||h) {
		for (int i=0; i<h; i++)
		for (int j=0; j<w; j++)
			cin >> map[i][j];

		ans=0;
		for (int i=0; i<h; i++)
		for (int j=0; j<w; j++)
			if (map[i][j]) { ans++; dfs(j, i); }

		cout << ans << endl;
	}
	return 0;
}

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

class P
{
	public:
	int x,y;
	P(int X,int Y)
	{
		x=X;
		y=Y;
	}
};

int main()
{
	int w,h;
	while(cin>>w>>h)
	{
		if(w==0 && h==0)break;
		int ans=0;
		int field[52][52]={0};
		int flg[52][52]={0};

		for(int i=1;i<=h;i++)
		{
			for(int j=1;j<=w;j++)
			{
				cin>>field[i][j];		
			}
		}

		for(int i=1;i<=h;i++)
		{
			for(int j=1;j<=w;j++)
			{
				if(field[i][j]==0 || flg[i][j]==1)continue;
				ans++;
				stack<P> ST;
				ST.push(P(j,i));
				while(!ST.empty())
				{
					int x=ST.top().x;
					int y=ST.top().y;
					ST.pop();
					flg[y][x]=1;
					for(int y2=-1;y2<=1;y2++)
					{
						for(int x2=-1;x2<=1;x2++)
						{
							if(field[y+y2][x+x2]==0 || flg[y+y2][x+x2]==1)continue;
							ST.push(P(x+x2,y+y2));
							flg[y+y2][x+x2]=1;
						}
					}
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>

using namespace std;

int main(void) {
	int w,h;
	while(cin >> w >> h && w && h) {
		vector< vector<int> > data(w+2,vector<int>(h+2,0));
		vector< vector<bool> > check(w+2,vector<bool>(h+2,false));
		queue<int> sx,sy;
		for (int i = 1; i <= h; ++i) {
			for (int j = 1; j <= w; ++j) {
				cin >> data[i][j];
				if (data[i][j] == 1) {
					sx.push(j);
					sy.push(i);
				}
			}
		}

		int nx,ny;
		int count = 0;
		queue<int> fx,fy;
		while(!sx.empty()) {
			nx = sx.front();
			ny = sy.front();
			sx.pop();
			sy.pop();
			if (!check[ny][nx]) {
				check[ny][nx] = true;
				fx.push(nx);
				fy.push(ny);
				++count;
			}
			while (!fx.empty()) {
				nx = fx.front();
				ny = fy.front();
				fx.pop();
				fy.pop();
				if (!check[ny+1][nx] && data[ny+1][nx] == 1) {
					check[ny+1][nx] = true;
					fx.push(nx);
					fy.push(ny+1);
				}
				if (!check[ny-1][nx] && data[ny-1][nx] == 1) {
					check[ny-1][nx] = true;
					fx.push(nx);
					fy.push(ny-1);
				}
				if (!check[ny+1][nx+1] && data[ny+1][nx+1] == 1) {
					check[ny+1][nx+1] = true;
					fx.push(nx+1);
					fy.push(ny+1);
				}
				if (!check[ny+1][nx-1] && data[ny+1][nx-1] == 1) {
					check[ny+1][nx-1] = true;
					fx.push(nx-1);
					fy.push(ny+1);
				}
				if (!check[ny-1][nx-1] && data[ny-1][nx-1] == 1) {
					check[ny-1][nx-1] = true;
					fx.push(nx-1);
					fy.push(ny-1);
				}
				if (!check[ny-1][nx+1] && data[ny-1][nx+1] == 1) {
					check[ny-1][nx+1] = true;
					fx.push(nx+1);
					fy.push(ny-1);
				}
				if (!check[ny][nx+1] && data[ny][nx+1] == 1) {
					check[ny][nx+1] = true;
					fx.push(nx+1);
					fy.push(ny);
				}
				if (!check[ny][nx-1] && data[ny][nx-1] == 1) {
					check[ny][nx-1] = true;
					fx.push(nx-1);
					fy.push(ny);
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}