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 <iostream>
#include <map>
#include <queue>
#include <vector>

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

using namespace std;
vector <vector<int> > seamap, visit;

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

void bfs(int x, int y){
    queue <pair <int, int> > Q;
    visit[x][y] = 1;
    Q.push(pair<int,int>(x, y));
    while(!Q.empty()){
        pair<int,int> cur = Q.front();
        Q.pop();
        REP(i,8){
            nx = cur.first + dx[i];
            ny = cur.second + dy[i];
            if(seamap[nx][ny] && !visit[nx][ny]){
                visit[nx][ny] = 1;
                Q.push(pair<int,int>(nx, ny));
            }
        }
    }
}

int main(){
    int w,h;
    while(cin >> w >> h && !(w == 0 && h == 0)){
        int island = 0;
        seamap.resize(w + 2);
        REP(i,w + 2){
            seamap[i] = vector<int>(h + 2, 0);
        }
        visit = seamap;
        FOR(y,1,h + 1){
            FOR(x,1,w + 1){
                cin >> seamap[x][y];
            }
        }
       FOR(x,1,w + 1){
            FOR(y,1,h + 1){
                if(seamap[x][y] && !visit[x][y]){
                    island++;
                    bfs(x,y);
                }
            }
        }
        cout << island << endl;
    }
}

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

int direction_x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int direction_y[8] = {1, 1, 0, -1, -1, -1, 0, 1};

int h,w;
IslandsMap islands_map;
CheckMap check_map;

void dfs(int x, int y) {
    int dx, dy;
    check_map[y][x] = true;
    rep(i, 8) {
        dx = x + direction_x[i];
        dy = y + direction_y[i];
        if(dx >= 0 && dx < w && dy >= 0 && dy < h &&
           check_map[dy][dx] == false && islands_map[dy][dx] == 1) {
            dfs(dx, dy);
        }
    }
}

int countIslands() {
    int ans = 0;
    rep(y, h) {
        rep(x, w) {
            if(check_map[y][x] == false && islands_map[y][x] == 1) {
                dfs(x, y);
                ans++;
            }
        }
    }
    return ans;
}

int main() {
    // 制約見て！！！デカかったらlong longにすること！
    int c;
    while(true) {
        cin >> w >> h;
        if(w == 0 && h == 0)
            break;

        islands_map.resize(h);
        rep(i, h) {
            islands_map[i].resize(w);
            rep(j, w) {
                cin >> c;
                islands_map[i][j] = c;
            }
        }

        check_map.resize(h);
        rep(i, h) {
            check_map[i].resize(w);
            check_map[i].assign(w,false);
        }
        cout << countIslands() << endl;
    }
}

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));
		vector< vector<bool> > check(w+2,vector<bool>(h+2,false));
		queue<int> sx,sy;
		for (int i = 0; i < h + 2; ++i) {
			for (int j = 0; j < w + 2; ++j) {
				if (i == 0 || i == h+1 || j == 0 || j == w+1) {
					data[i][j] = 0;
				}
				else {
					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;
}