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>
using namespace std;
int w,h;
int masu[1010][1010];
int dx[] = {-1,-1,-1,0,0,1,1,1};
int dy[] = {-1,0,1,-1,1,-1,0,1};
void dfs(int y,int x){
    masu[y][x] = 0;
    for(int i=0;i<8;i++){
        int ny = y+dy[i];
        int nx = x+dx[i];
        if(ny<0||ny>=h||nx<0||nx>=w||masu[ny][nx]==0) continue;
        dfs(ny,nx);
    }
}
int main(){
    int ans;
    while(cin >> w >> h,w!=0||h!=0){
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++) cin >> masu[i][j];
        }
        ans = 0;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(masu[i][j] == 1){
                    ans++;
                    dfs(i,j);
                }
            }
        }
        cout << ans << endl;
    }
    return(0);
}

C++: #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
ll seen[55][55];
ll W, H;
void dfs(int h, int w) {
    seen[h][w] = 0;
    
    for (ll dir = 0; dir < 8; dir++) {
        ll nh = h + dx[dir];
        ll nw = w + dy[dir];
        if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
        if (seen[nh][nw] == 0) continue;
        
        
        dfs(nh, nw);
            
    }
}


int main(){
    while(1 > 0){
        ll ans = 0;
        cin >> W >> H;
        if(W == 0 && H == 0) break;

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

        for (ll i = 0; i < H; i++) {
            for (ll j = 0; j < W; j++) {
                if(seen[i][j]){
                    dfs(i,j);
                    ans++ ;
                }
            }
        }
        printf("%lld\n", ans);
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(begin, i, end) for (int i = begin; i < (int)(end); i++)
#define all(v) v.begin(), v.end()
const int MOD = 1000000007;

int w, h;
vector<int> ans;

void dfs(int x, int y, vector<vector<int>> &m, vector<vector<bool>> &seen);

int main()
{
    while (true)
    {
        cin >> w >> h;
        // 終了条件
        if ((w == 0) && (h == 0))
        {
            break;
        }
        vector<vector<int>> m(52, vector<int>(52, 0));
        vector<vector<bool>> seen(52, vector<bool>(52, false));
        // データの受け取り
        rep(1, i, h + 1)
        {
            rep(1, j, w + 1)
            {
                cin >> m.at(i).at(j);
            }
        }
        int count = 0;
        //すべての点で探索
        rep(1, i, h + 1)
        {
            rep(1, j, w + 1)
            {
                // 探索済か海のマスならとばす
                if (seen.at(i).at(j) || (m.at(i).at(j) == 0))
                {
                    continue;
                }
                // dfsによる塗りつぶし
                dfs(i, j, m, seen);
                count++;
            }
        }
        ans.push_back(count);
    }
    for (auto a : ans)
    {
        cout << a << endl;
    }
}

void dfs(int x, int y, vector<vector<int>> &m, vector<vector<bool>> &seen)
{
    // 探索済か海のマスなら探索終了
    if (seen.at(x).at(y) || (m.at(x).at(y) == 0))
    {
        return;
    }
    // 探索済の印をつける
    seen.at(x).at(y) = true;
    rep(-1, dx, 2)
    {
        rep(-1, dy, 2)
        {
            dfs(x + dx, y + dy, m, seen);
        }
    }
    return;
}