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>
using namespace std;
struct Point
{
	int x;
	int y;
	Point(int x = 0, int y = 0) :x(x), y(y) {}
};
const int MAX = 50;
int map[MAX][MAX];
bool book[MAX][MAX];
void depthFSearch(int h, int w, Point start)
{
	int next[9][2] = { { -1,-1 },{ -1,0 },{ -1,1 },{ 0,-1 },{ 0,0 },{ 0,1 },{ 1,-1 },{ 1,0 },{ 1,1 } };
	for (int k = 0; k <= 8; k++)
	{
		int nx = start.x + next[k][0];
		int ny = start.y + next[k][1];
		if (nx < 0 || nx >= h || ny < 0 || ny >= w || !map[nx][ny])
			continue;
		if (!book[nx][ny] && map[nx][ny])
		{
			book[nx][ny] = true;
			Point p = { nx,ny };
			depthFSearch(h, w, p);
		}
	}
	return;
}
int main()
{
	while (true)
	{
		int w, h;
		cin >> w >> h;
		if (!w && !h)
			break;
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
			{
				cin >> map[i][j];
				book[i][j] = false;
			}
		int sum = 0;
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
			{
				if (!book[i][j] && map[i][j])
				{
					book[i][j] = true;
					Point start(i, j);
					depthFSearch(h, w, start);
					sum++;
				}
			}
		cout << sum << endl;
	}
}

C++: #ifndef _WIN32
#include<iostream>
#endif

using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>

#define FOR(i,bg,ed) for(int i=(bg);i<(ed);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

typedef vector<int> V;
typedef vector<V> VV;

VV wrap(VV vs,int c) {
	int R = vs.size();
	int C = vs[0].size();
	VV ret(R + 2, V(C + 2, c));
	REP(i, R)REP(j, C)ret[i + 1][j + 1] = vs[i][j];
	return ret;
}
const int dr[] = { 0,0,-1,1 ,1,1,-1,-1};
const int dc[] = { -1,1,0,0, -1,1,-1,1};
VV g;
void dfs(int r,int c) {
	if (g[r][c] == 0)return;
	//cout << "(" << r << "," << c << ")";
	g[r][c] = 0;
	REP(i, 8) {
		int nr = r + dr[i];
		int nc = c + dc[i];
		dfs(nr, nc);
	}
}
int main() {
	int R, C;
	while (cin >> C >> R, C + R) {
		VV in(R,V(C));
		REP(i, R)REP(j,C)cin >> in[i][j];
		g = wrap(in,0);
		int res = 0;
		REP(i, R)REP(j, C) {
			if (g[i + 1][j + 1]) {
				res++;
				dfs(i + 1, j + 1);// cout << endl;
			}
		}

		cout << res << endl;
	}


}

C++: #define rep(i, n) for(int i = 0; i < n; i++)
#define ck(a, b) (0<=a && a<b)
using namespace std;
int dx[]={-1,0,1,-1,1,-1,0,1}, dy[]={-1,-1,-1,0,0,1,1,1};
int w, h; int il[50][50];

int main(){
	while(cin>>w>>h, w||h){
		int c = 0;
		rep(y, h)rep(x, w)cin>>il[y][x];
		rep(y, h)rep(x, w){
			if(il[y][x]){
				int cked[50][50] = {0};
				cked[y][x] = 1;
				bool finish = 0;
				while(!finish){
					finish = 1;
					rep(i, h)rep(j, w){
						if(cked[i][j]){
							rep(d, 8){
								if(ck(j+dx[d],w) && ck(i+dy[d],h) &&
									il[i+dy[d]][j+dx[d]] &&
									!cked[i+dy[d]][j+dx[d]]
								){
									cked[i+dy[d]][j+dx[d]] = 1; finish = 0;
								}
							}
						}
					}
				}
				c++;
				rep(i, h)rep(j, w)if(cked[i][j])il[i][j]=0;
			}
		}
		cout<<c<<endl;
	}
	return 0;
}

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

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

void check(int x,int y,int lnum){
  map[y][x]=lnum;
  if (x>0){
    if (map[y][x-1]==1) check(x-1,y,lnum);
    if (y>0){
      if (map[y-1][x-1]==1) check(x-1,y-1,lnum);
    }
    if (y<h-1){
      if (map[y+1][x-1]==1) check(x-1,y+1,lnum);
    }
  }
  if(x<w-1){
    if (map[y][x+1]==1) check(x+1,y,lnum);
    if (y>0){
      if (map[y-1][x+1]==1) check(x+1,y-1,lnum);
    }
    if (y<h-1){
      if (map[y+1][x+1]==1) check(x+1,y+1,lnum);
    }
  }
  if (y>0){
    if (map[y-1][x]==1) check(x,y-1,lnum);
  }
  if (y<h-1){
    if (map[y+1][x]==1) check(x,y+1,lnum);
  }
}

int main(){
  cin >> w >> h;
  while (w!=0||h!=0){
    label=2;
    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){
	  //cout << "(" << map[i][j] << ")";
	  check(j,i,label++);
	}
      }
    }
    //cout << "(" << label << ")";

    cout << (label-2) << endl;

    cin >> w >> h;
 }
  return 0;
}

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

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

using namespace std;
int field[50][50];
int w, h;

void dfs(int i, int j){
    field[i][j] = 0;
    for(int di=-1; di<=1; di++){
        for(int dj=-1; dj<=1; dj++){
            int ni = i+di;
            int nj = j+dj;
            if(field[ni][nj]==1 && 0 <= ni && ni < h &&
                    0 <= nj && nj < w){
                dfs(ni,nj);
            }
        }
    }
    return;
}

int main(){
    while(cin >> w >> h){
        if(w==0 && h==0) break;
        REP(i,h){
            REP(j,w){
                cin >> field[i][j];
            }
        }

        int cnt = 0;
        REP(i,h){
            REP(j,w){
                if(field[i][j] == 1){
                    dfs(i,j);
                    cnt++;
                }
            }
        }
        cout << cnt << endl;
    }

    return 0;
}

C++: # include <iostream>

const int MAX_W = 50;
const int MAX_H = 50;

int w, h;

int c[MAX_H][MAX_W];

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

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

int solve() {
	int sum = 0;
	for (int i = 0; i < h; ++i) {
		for (int j = 0; j < w; ++j) {
			if (c[i][j] == 1) {
				dfs(j, i);
				++sum;
			}
		}
	}
	return sum;
}

int main() {
	while (1) {
		std::cin >> w >> h;
		if ((w + h) == 0)
			break;
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j)
				std::cin >> c[i][j];
		}
		std::cout << solve() << std::endl;
	}
	return 0;
}