Go: package main

import (
	"fmt"
)

func main() {
	var h, w int
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			return
		}
		matrix := make([][]int, h)
		Sean := make([][]bool, h)
		for i := 0; i < h; i++ {
			matrix[i] = make([]int, w)
			Sean[i] = make([]bool, w)
			for j := 0; j < w; j++ {
				fmt.Scan(&matrix[i][j])
			}
		}
		var count int
		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				if matrix[i][j] == 1 && Sean[i][j] == false {
					search(i, j, h, w, matrix, Sean)
					count++
				}
			}
		}
		fmt.Println(count)
	}
}
func search(x int, y int, h int, w int, matrix [][]int, Sean [][]bool) {
	if x < 0 || y < 0 || x >= h || y >= w || matrix[x][y] == 0 {
		return
	}
	if Sean[x][y] == true {
		return
	}

	Sean[x][y] = true

	search(x+1, y, h, w, matrix, Sean)
	search(x-1, y, h, w, matrix, Sean)
	search(x, y+1, h, w, matrix, Sean)
	search(x, y-1, h, w, matrix, Sean)
	search(x+1, y-1, h, w, matrix, Sean)
	search(x-1, y+1, h, w, matrix, Sean)
	search(x+1, y+1, h, w, matrix, Sean)
	search(x-1, y-1, h, w, matrix, Sean)
}

C++: #include <cstdio>
#include <cstring>
using namespace std;

int w, h;
int map[100][100];
int island_number[100][100];

int angle[][2] = {
	{-1, 0}, {-1, -1}, {0, -1}, {1, -1},
	{1, 0}, {1, 1}, {0, 1}, {-1, 1}
};

void dfs(int y, int x)
{
	for (int i = 0; i < 8; i++){
		if (
		y + angle[i][0] >= 0 &&
		y + angle[i][0] < h &&
		x + angle[i][1] >= 0 &&
		x + angle[i][1] < w &&
		island_number[y + angle[i][0]][x + angle[i][1]] == (y + angle[i][0]) * h + x + angle[i][1]
		){
			island_number[y + angle[i][0]][x + angle[i][1]] = island_number[y][x];
			dfs(y + angle[i][0], x + angle[i][1]);
		}
	}
}

int main()
{
	
	while (scanf("%d %d", &w, &h), w + h){
		memset(island_number, 0, sizeof(island_number));
		
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				scanf("%d", &map[i][j]);
				if (map[i][j] == 1){
					island_number[i][j] = i * h + j;
				}
				else {
					island_number[i][j] = -1;
				}
			}
		}
		
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (island_number[i][j] == i * h + j){
					dfs(i, j);
				}
			}
		}
		
		bool ex[100 * 100] = {false};
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (island_number[i][j] >= 0){
					ex[island_number[i][j]] = true;
				}
//				printf("%d ", island_number[i][j]);
			}
//			puts("");
		}
		
		int cnt = 0;
		for (int i = 0; i < w * h; i++){
			cnt += ex[i];
		}
		
		printf("%d\n", cnt);
	}
	
	return 0;
}

C++: #include <cstdio>
#include <algorithm>
using namespace std;

int w, h, c[50][52];

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

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

int main(){
    for(; scanf("%d%d", &w, &h), w||h;){
        for(int x = 0; x < h; x++){
            for(int y = 0; y < w; y++){
                scanf("%d", &c[x][y]);
            }
        }
        int ans = 0;
        for(int x = 0; x < h; x++){
            for(int y = 0; y < w; y++){
                if( c[x][y] ){
                    ++ans;
                    dfs(x, y);
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

C++: #include<bits/stdc++.h>

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


bool landfill(int x, int y, int w, int h, vector<vector<int> > &land) {
    if (x < 0 || y < 0 || x >= h || y >= w) return false;
    if (land.at(x).at(y) == 0) return false;
    land.at(x).at(y) = 0;
    return landfill(x, y + 1, w, h, land) || landfill(x, y - 1, w, h, land) ||
           landfill(x + 1, y, w, h, land) || landfill(x - 1, y, w, h, land) ||
           landfill(x - 1, y + 1, w, h, land) || landfill(x - 1, y - 1, w, h, land) ||
           landfill(x + 1, y + 1, w, h, land) || landfill(x + 1, y - 1, w, h, land);
}

int main() {
    vector<int> ans_union;
    do{
        int w, h;
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        vector<vector<int> > land(h, vector<int>(w));

        int ans = 0;
        rep(i, h) rep(j, w) {
                cin >> land.at(i).at(j);
            }

        rep(i, h) rep(j, w) {
                if (land.at(i).at(j) == 1) {
                    ans++;
                    landfill(i, j, w, h, land);
                }
            }
        ans_union.push_back(ans);
    }while(1);
    for(int i = 0; i < ans_union.size(); i++){
        cout << ans_union.at(i) << endl;
    }

    return 0;

}

C++: #include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <memory.h>
#include <functional>

using namespace std;

//FILE * in = freopen("B", "r", stdin);

typedef pair<int,int> P;

int n, m;
int cnt = 0;
int data[100][100];
int check[100][100];

int mvx[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int mvy[] = {-1, -1, -1, 0, 0, 1, 1, 1};

void input() {
	for(int i = 0; i < m; i++){
		for(int j = 0; j < n; j++) {
			scanf("%d", &data[i][j]);
		}
	}
	cnt = 0;
	memset(check, 0, sizeof(check));
}

void bfs(int x, int y) {
	queue<P> que;
	que.push(P(x,y));
	check[x][y] = 1;
	while(!que.empty()){
		P cur = que.front(); que.pop();
		for(int i = 0; i < 8; i++) {
			if(cur.first + mvx[i] >= 0 && cur.first + mvx[i] < m && cur.second + mvy[i] >= 0 && cur.second + mvy[i] < n
				&& data[cur.first + mvx[i]][cur.second + mvy[i]] && !check[cur.first+mvx[i]][cur.second+mvy[i]]){
					check[cur.first +mvx[i]][cur.second+mvy[i]] = 1;
					que.push(P(cur.first+mvx[i], cur.second+mvy[i]));
			}
		}
	}
}

void process() {
	for(int i = 0; i < m; i++) {
		for(int j = 0; j < n; j++) {
			if(data[i][j] && !check[i][j]) {
				bfs(i,j);
				++cnt;
			}
		}
	}
	printf("%d\n", cnt);
}

int main() {
	while(true) {
		scanf("%d%d", &n, &m);
		if(n == 0) break;
		input();
		process();
	}
	return 0;
}