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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
using P = pair<int,int>;

/* 大文字を小文字に変換 */
char tolower(char c) {return (c + 0x20);}
/* 小文字を大文字に変換 */
char toupr(char c) {return (c - 0x20);}

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"

// イテレータ要素のインデックス
distance(A.begin(), itr);
*/

const int MAX_H = 55, MAX_W = 55;
const int INF = 1000100100;

int color[MAX_H][MAX_W], c[MAX_H][MAX_W];
int w, h;


void init(){
    rep(i,MAX_H){
        rep(j,MAX_W){
            color[i][j] = INF;
            c[i][j] = 0;
        }
    }
}

void dfs(int hi, int wi, int col){
    if(color[hi][wi] == INF) color[hi][wi] = col;

    for(int i=-1; i<=1; i++){   // 高さ
        for(int j=-1; j<=1; j++){   // 幅
            int nh = hi+i, nw = wi+j;
            if(nh<0 || nw<0 || nh>=h || nw>=w) continue;
            if(c[nh][nw] == 1 && color[nh][nw] == INF) dfs(nh, nw, col);
        }
    }
}

int main()
{
    while(true){
        cin >> w >> h;
        if(w == 0 && h == 0) break;

        init();

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

        int col = 0;
        rep(i,h){
            rep(j,w){
                if(color[i][j] == INF && c[i][j] == 1){
                    dfs(i, j, col);
                    ++col;
                }
            }
        }
        set<int> s;
        rep(i,h){
            rep(j,w){
                if(color[i][j] != INF) s.insert(color[i][j]);
            }
        }
        cout << s.size() << endl;
    }
}

C++: #include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end())
#define VRSORT(v) sort(v.rbegin(), v.rend())
#define ll long long
#define pb(a) push_back(a)
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

typedef vector<unsigned int>vec;
typedef vector<vec> mat;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};

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

struct Edge{int to,weight; Edge(int t, int w) : to(t), weight(w) {}};
using DirectedGraph = vector<vector<Edge>>;
using UndirectedGraph = vector<vector<int>>;

int H,W;
char field[51][51];
bool seen[51][51];

void dfs(int h,int w){
    if(seen[h][w]) return;

    seen[h][w] = true;

    REP(i,8){
        int nh=h+dy[i];
        int nw=w+dx[i];

        if(nh<0 || nh>=H || nw<0 || nw>=W || field[nh][nw]=='0') continue;
        dfs(nh,nw);
    }
    return;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(1){
        cin>>W>>H;
        if(!H && !W) return 0;
        REP(i,H) REP(j,W) cin>>field[i][j];
        
        memset(seen,0,sizeof(seen));

        int ans=0;
        REP(i,H) REP(j,W){
            if(field[i][j]=='1' && !seen[i][j]){
                //cout<<i<<":"<<j<<endl;
                dfs(i,j);
                ans++;
            }
        }

        cout<<ans<<endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
static const int N = 50;

int W, H;
bool visited[N][N];
int map[N][N];

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

void dfs(int h, int w) {
    visited[h][w] = true;

    for (int i = 0; i < 8; ++i) {
        int nh = h + dx[i];
        int nw = w + dy[i];

        if (nh < 0 || nh > H-1 || nw < 0 || nw >W-1) continue;
        if (visited[nh][nw]) continue;
        if (map[nh][nw] == 0) continue;

        dfs(nh, nw);
    }
}

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

        for (int h = 0; h < H; ++h) {
            for (int w = 0; w < W; ++w) {
                cin >> map[h][w];
            }
        }

        memset(visited, 0, sizeof(visited));

        int count = 0;
        for (int h = 0; h < H; ++h) {
            for (int w = 0; w < W; ++w) {
                if (visited[h][w] || map[h][w] == 0) continue;
                dfs(h, w);
                ++count;
            }
        }

        cout << count << endl;
    }


    return 0;
}