Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;

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

int H, W;
void search(int h, int w, vector<vector<int>> &c, vector<vector<bool>> &reached) {
    reached.at(h).at(w) = true;

    for (int direction = 0; direction < 8; ++direction) {
        int nh = h + dx[direction];
        int nw = w + dy[direction];
        if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
        if (c.at(nh).at(nw) == 0) continue;
        if (reached.at(nh).at(nw) == true) continue;
        search(nh, nw, c, reached);
    }
}

int main() {
    while (true) {
        int ans = 0;
        cin >> W >> H;
        if (H == 0 && W == 0) return 0;
        vector<vector<int>> c(H, vector<int>(W));
        vector<vector<bool>> reached(H, vector<bool>(W));
        rep(i,H) rep(j,W) cin >> c.at(i).at(j);

        rep(i,H) rep(j,W) {
            if (c.at(i).at(j) == 1 && reached.at(i).at(j) == false) {
                ++ans;
                search(i, j, c, reached);
            }
        }
        cout << ans << endl;
    }
}

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;

}