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 <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define EACH(i, c) for (auto &(i): c)
#define all(c) begin(c),end(c)
#define EXIST(s, e) ((s).find(e)!=(s).end())
#define SORT(c) sort(begin(c),end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

//#define LOCAL 0
//#ifdef LOCAL
//#define DEBUG(s) cout << (s) << endl
//#define dump(x)  cerr << #x << " = " << (x) << endl
//#define BR cout << endl;
//#else
//#define DEBUG(s) do{}while(0)
//#define dump(x) do{}while(0)
//#define BR
//#endif


//改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30)
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007
//ここから編集する
int W, H;

bool check(int w, int h) {
    return w >= 0 && h >= 0 && w < W && h < H;
}

void dfs(int w, int h, vector<vector<int> > &v) {
    if (check(w, h)) {
        if (v[h][w] != 1) return;
        v[h][w] = 2;
//        dfs(w+1,h,v);
//        dfs(w,h+1,v);
//        dfs(w-1,h,v);
//        dfs(w,h-1,v);
        for (int i = h - 1; i <= h + 1; i++) {
            for (int j = w - 1; j <= w + 1; j++) {
                dfs(j, i, v);
            }
        }
    } else {
        return;
    }
}

void solve(int w, int h) {
    W = w;
    H = h;
    vector<vector<int> > v(h, vector<int>(w, 0));
    rep(i, 0, h) {
        rep(j, 0, w) {
            cin >> v[i][j];
        }
    }
    int ans = 0;
    rep(i, 0, h) {
        rep(j, 0, w) {
            if (v[i][j] == 1) {
                ans++;
                dfs(j, i, v);
            }
        }
    }
    cout << ans << endl;
}

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

    while (true) {
        int w, h;
        cin >> w >> h;
        if (w == 0) break;
        solve(w, h);
    }


    return 0;
}

C++: #include <iostream>

using namespace std;

const int MAX_SIZE = 51;

int field[MAX_SIZE][MAX_SIZE];
int w, h;

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

bool ok(int y, int x) {
	return x >= 0 && x < w && y >= 0 && y < h;
}

void consume(int y, int x) {
	bool flag = false;
	for (int i = 0; i < 8; ++i) {
		int curx = x + dx[i],
			cury = y + dy[i];
		if (ok(cury, curx) && field[cury][curx] == 1) {
			field[cury][curx] = 0;
			consume(cury, curx);
		}
	}
}

int main() {
	while (true) {
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j)
			cin >> field[i][j];

		int res = 0;
		for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) {
			if (field[i][j] == 1) {
				field[i][j] = 0;
				consume(i, j);
				++res;
			}
		}
		
		cout << res << endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
#define MAX (256)

int field[MAX][MAX];
int ans = 0;

void dfs(int i, int j){
	for(int k = -1; k <= 1; k++){
		for(int l = -1; l <= 1; l++){
			if(field[i+k][j+l] == 1){
				field[i+k][j+l] = 0;
				dfs(i+k, j+l);
			}
		}
	}
}

int main(){
	int k, l;

	while(1){
		ans = 0;
		cin >> l >> k;

		if((k|l) == 0) break;

		for(int i=0; i < MAX; i++){
			for(int j=0; j < MAX; j++) field[i][j]= 0;
		}

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

		for(int i=0; i < k; i++){
			for(int j=0; j < l; j++){
				if(field[i][j] == 1){
					field[i][j] = 0;
					dfs(i, j);
					ans++;
				}
			}
		}
		
		cout << ans << endl;
	}
}

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

using namespace std;
using ll = long long;
using ld = long double;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define reps(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
	int w, h;
	int c[50][50] = {};

	void dfs(int i, int j) {
		c[i][j] = 0;

		for (int di = -1; di <= 1;di++) {
			for (int dj = -1; dj <= 1;dj++) {
				int ni = i + di;
				int nj = j + dj;

				if (0 <= ni&&ni < h && 0 <= nj&&nj < w && c[ni][nj] == 1) {
					dfs(ni, nj); 
				}
			}
		}
		return;
}

int main() {
	cin.sync_with_stdio(false);
	vector<int>ans;
	while (1) {
		cin >> w >> h;
		rep(i, h) {
			rep(j, w) {
				cin >> c[i][j];
			}
		}
		int res = 0;
		rep(i, h) {
			rep(j, w) {
				if (c[i][j] == 1) {
					dfs(i, j);
					res++;
				}
			}
		}
		ans.push_back(res);

		if (w == 0&&h == 0) {
			break;
		}
	}
	rep(i, ans.size()) {
		cout << ans[i] << endl;
	}

	return 0;
}