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++: #pragma warning(disable:4996)
#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for (int i = 0; i<n; i++)
#define ALL(a) begin(a), end(a)
#define TPL template
#define TNM typename

using ll = long long;
using ull = unsigned long long;
TPL<TNM T> using vec = vector<T>;
TPL<TNM T> using vec2 = vec<vec<T>>;
TPL<TNM T> using vec3 = vec<vec2<T>>;
TPL<TNM T> using vec4 = vec<vec3<T>>;
TPL<TNM T> using vec5 = vec<vec4<T>>;

TPL<TNM V, TNM H> void resize(vector<V>& v, const H h) { v.resize(h); }
TPL<TNM V, TNM H, TNM ... T> inline void resize(vector<V>& v, const H& h, const T ... t) { v.resize(h); for (auto& _v : v) resize(_v, t ...); }
TPL<TNM V, TNM T> void fill(V& x, const T& val) { x = val; }
TPL<TNM V, TNM T> inline void fill(vector<V>& vec, const T& val) { for (auto& v : vec) fill(v, val); }
TPL<TNM T> inline void UNIQUE(vector<T>& vec) { sort(ALL(vec)); vec.erase(unique(ALL(vec)), vec.end()); }

class YukiIO {
	bool f = false; //改行が残っている否か
	char* buff;
	int sbuff;
	string format;
public:
	YukiIO(int s_, int pre_) { sbuff = s_; buff = new char[s_]; format = "%." + to_string(pre_) + "lf"; }
	~YukiIO() { delete buff; }
	//--------------input---------------
	inline void get(int &i_) { f = true; scanf("%d", &i_); }
	inline void get(long long &l_) { f = true; scanf("%lld", &l_); }
	inline void get(double &d_) { f = true; scanf("%lf", &d_); }
	inline void get(float &f) { f = true; scanf("%f", &f); }
	inline void get(char &c_) {
		if (f)scanf("%*c%c", &c_);
		else scanf("%c", &c_);
		f = true;
	}
	inline void get(char* s_) { f = true; scanf("%s", s_); }
	inline void get(string &s_) { f = true; scanf("%s", buff); s_ = string(buff); }
	template <typename HEAD, typename ...TAIL>inline void get(HEAD& head, TAIL& ...tails) {
		get(head);
		get(tails...);
	}
	template <typename T> inline void get(vector<T>& v_) {
		for (int i = 0; i < v_.size(); ++i) get(v_[i]);
	}
	inline void getl(string& s_) {
		if (f) { char dm; scanf("%c", &dm); }
		fgets(buff, sbuff, stdin);
		s_ = string(buff);
		f = false;
	}
	template <typename ...ARGS> inline void getf(const char* format, ARGS& ... args) {
		if (f)scanf("%*c%[^\n]", buff);
		else scanf("%[^\n]", buff);
		sscanf(buff, format, &args...);
		f = true;
	}
	//--------------outut---------------
	inline void print(bool b_) { printf("%s", b_ ? "true" : "false"); }
	inline void print(int i_) { printf("%d", i_); }
	inline void print(long long l_) { printf("%lld", l_); }
	inline void print(double d_) { printf(format.data(), d_); }
	inline void print(float f) { printf("%f", f); }
	inline void print(char c_) { printf("%c", c_); }
	inline void print(char* s_) { printf("%s", s_); }
	inline void print(string &s_) { printf("%s", s_.data()); }
	inline void print(const char* s_) { printf("%s", s_); }
	template <typename T> inline void print(vector<T>& v) {
		print(v[0]);
		for (int i = 1; i < v.size(); ++i) {
			putchar(' '); print(v[i]);
		}
	}
	inline void println() { puts(""); }
	template <typename T> inline void println(vector<T>& v) { for (int i = 0; i < v.size(); ++i) println(v[i]); }
	template <typename HEAD, typename ...TAIL>inline void print(HEAD head, TAIL ...tails) {
		print(head);
		print(tails...);
	}
	template <typename ...ARGS>inline void println(ARGS ...args) {
		print(args...);
		puts("");
	}
};

YukiIO io(114514, 12);

inline bool range1d(int x, int W) { return (x >= 0) && (W > x); }
inline bool range2d(int x, int y, int W, int H) { return (x >= 0) && (y >= 0) && (W > x) && (H > y); }
const int dx[] = { 0, 1, 0, -1, 1, -1, -1, 1 };
const int dy[] = { -1, 0, 1, 0, 1, 1, -1, -1 };

int W, H;

int dfs(vec2<int>& I, int x, int y) {
	if (I[y][x] == 0)return 0;
	I[y][x] = 0;
	REP(i, 8) {
		int nx = x + dx[i];
		int ny = y + dy[i];
		if (range2d(nx, ny, W, H)) {
			dfs(I, nx, ny);
		}
	}
	return 1;
}

int main(void) {
	while (1) {
		io.get(W, H);
		if (W + H == 0)break;
		vec2<int> I;
		resize(I, H, W);
		REP(y, H) {
			REP(x, W) {
				io.get(I[y][x]);
			}
		}
		int ans = 0;
		REP(y, H) {
			REP(x, W) {
				ans += dfs(I, x, y);
			}
		}
		io.println(ans);
	}
	return 0;
}

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

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define ll long long int
#define INF 1000000007
#define llINF 1000000000000000007
#define v(i) vector<i>
#define vv(i) vector<vector<i>>
// cin.eof() 入力個数の問題
// int num = atoi(string.c_str()); 文字列の整数変換
bool visited[51][51];
int h, w;
int A[] = {1, 1, 1, 0, 0, -1, -1, -1};
int B[] = {0, 1, -1, 1, -1, 0, 1, -1};
void dfs(int x, int y, vv(int) & c) {
    if (x < 0 || x >= h || y < 0 || y >= w || c[x][y] == 0)
        return;
    if (visited[x][y])
        return;
    visited[x][y] = true;
    //cout<<"(x,y)=="<<x<<","<<y<<endl;
    rep(i, 8) {
        dfs(x + A[i], y + B[i], c);
    }
}

int main() {
   while (1) {
        cin >> w >> h;
        if(w==0 && h==0) return 0;
        vv(int) c;
        rep(i, h) {
            c.push_back({});
            rep(j, w) {
                int a;
                cin >> a;
                c[i].push_back(a);
            }
        }
        rep(i, 51) {
            rep(j, 51) {
                visited[i][j] = false;
            }
        }
        int ans = 0;
        rep(i, h) {
            rep(j, w) {
                if (visited[i][j] || c[i][j] == 0)
                    continue;
                dfs(i, j, c);
                //cout<<"dfs"<<endl;
                ans++;
            }
        }
        cout << ans << endl;
    }
    return 0;
}