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 <fstream>
#include <cstdlib>
#include <math.h>
#include <utility>
#include <algorithm>
#include <functional>
#include <vector>
#include <numeric>
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vb = vector<bool>;
using Graph = vector<vector<int>>;

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

vb seen;

void dfs(const Graph &G, int v) {
    seen[v] = true;
    for(auto next_v : G[v]) {
        if (seen[next_v]) continue;
        dfs(G, next_v);
    }
}

int main () {
	cin.tie(0);
   	ios::sync_with_stdio(false);

    int w, h;
    while(cin >> w >> h, w*h) {
        int map[h+1][w+1];
        REP(j, w+1) {
            map[0][j] = 0;
        }
        REP(i, h) {
            map[i+1][0] = 0;
            REP(j, w) {
                cin >> map[i+1][j+1];
            }
        }
        
        Graph G(w*h);
        FOR(i, 1, h+1) {
            FOR(j, 1, w+1) {
                if (map[i][j] == 1) {
                    int p1 = (i-1)*w+(j-1);
                    int p2;
                    if (map[i][j-1] == 1) {
                        p2 = (i-1)*w+(j-2);
                        G[p1].push_back(p2);
                        G[p2].push_back(p1);
                    }
                    if (map[i-1][j-1] == 1) {
                        p2 = (i-2)*w+(j-2);
                        G[p1].push_back(p2);
                        G[p2].push_back(p1);
                    }
                    if (map[i-1][j] == 1) {
                        p2 = (i-2)*w+(j-1);
                        G[p1].push_back(p2);
                        G[p2].push_back(p1);
                    }
                    if (map[i-1][j+1] == 1) {
                        p2 = (i-2)*w+j;
                        G[p1].push_back(p2);
                        G[p2].push_back(p1);
                    }
                }
            }
        }

        seen.assign(w*h, false);
        int cnt = 0;
        REP(i, w*h) {
            if (seen[i]) continue;
            if (map[i/w+1][i%w+1] == 0) {
                seen[i] = true;
                continue;
            }
            cnt++;
            dfs(G, i);
        }
        cout << cnt << "\n";
    }
}

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

typedef std::pair<int, int> P;
int vx[] = { 0, 1, 0, -1, 1, 1, -1, -1 }, vy[] = { 1, 0, -1, 0, 1, -1, 1, -1 };
int w, h;
int map[51][51];

void bfs(int x, int y){

	map[y][x] = 0;

	std::queue<P>que;
	que.push(P(x, y));

	while (!que.empty()){

		P p = que.front();
		que.pop();

		for (int i = 0; i < 8; i++){
			int nx = p.first + vx[i], ny = p.second + vy[i];

			if (0 <= nx&&nx < w && 0 <= ny&&ny < h&&map[ny][nx] != 0){
				map[ny][nx] = 0;
				que.push(P(nx, ny));
			}
		}
	}

	return;
}

int main(){

	while (std::cin >> w >> h, w || h){

		int cnt = 0;

		memset(map, 0, sizeof(map));

		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				std::cin >> map[i][j];
			}
		}

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

		std::cout << cnt << std::endl;
	}

	return 0;
}

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

int H, W;
int field[51][51];

//動的計画法
void dfs(int h, int w){
    field[h][w] = 0;

    //八方向を１つずつ探索
    for(int dh = -1; dh <= 1; dh++){
        for(int dw = -1; dw <= 1; dw++){
            int nh = h + dh;
            int nw = w + dw;

            //場外アウトまたは０ならば、それ以降（再帰的に探索する部分）は実行せずループの最初に戻る
            if(nh < 0 || nh > H || nw < 0 || nw > W || field[nh][nw] == 0) continue;

            //再帰的に探索（探索しているマスを基準にしてその八方向にあるマスを探索していく）
            dfs(nh, nw);
        }
    }
}

int main(void){
    while(true){
        cin >> W >> H;
        if(W + H == 0) break;

        for(int i = 0; i < H; i++){
            for(int j = 0; j < W; j++){
                cin >> field[i][j];
            }
        }

        //探索開始
        int c = 0; //島の個数
        for(int i = 0; i < H; i++){
            for(int j = 0; j < W; j++){
                if(field[i][j] == 0) continue; //島の一部でないならば、それ以降スルー
                dfs(i, j); //そのマスが陸であるならば、その八方向を動的計画法で探索していく
                c++;
            }
        }
        cout << c << endl;
    }
}