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 <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };
const int dx2[] = { -1,-1, 0,1,1, 1, 0,-1 }; const int dy2[] = { 0,-1,-1,-1,0, 1, 1, 1 };
struct UnionFind {
	vector<int> v;
	UnionFind(int n) : v(n)  { for (int i = 0; i < n; i++) v[i] = i; }
	int  find(int x)         { return v[x] == x ? x : v[x] = find(v[x]); }
	void unite(int x, int y) { v[find(x)] = find(y); }
};
int h, w;
int cell[50][50];
int main() {
	while (cin>>w>>h,h) {

		REP(i, h)
			REP(j, w)
			cin >> cell[i][j];
		int ans = 0;
		REP(i, h)
			REP(j, w)
			if (cell[i][j] == 1) {
				ans++;
				queue<pii> Q;
				Q.push(pii(i, j));
				cell[i][j] = 0;
				while (!Q.empty()){
					pii q = Q.front();Q.pop();
					int y = q.first;
					int x = q.second;
					REP(k,8) {
						if (0 <= y + dy2[k] && y + dy2[k] < h && 0 <= x + dx2[k] && x + dx2[k] < w &&cell[y + dy2[k]][x + dx2[k]]) {
							cell[y + dy2[k]][x + dx2[k]] = 0;
							Q.push(pii( y + dy2[k], x + dx2[k]));
						}
					}
				}
			}
		cout << ans << endl;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair< ll, ll > Pi;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define rep3(i,i0,n) for(int i=i0;i<(n);i++)
#define pb push_back
#define mod 1000000007
#define INF 200000000000
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()

bool compare(Pi a, Pi b) {
    if(a.second != b.second){
        return a.second > b.second;
        
    }else{
        return a.first < b.first;
    }
}
bool In_map(ll y,ll x,ll h,ll w){
    if(y<0 || x<0 || y>=h || x>=w){
        return 0;
    }else{
        return 1;
    }
}
const vector<ll> dx{1,0,-1,0,1,1,-1,-1};
const vector<ll> dy{0,1,0,-1,1,-1,1,-1};


ll cnt=0;
vector<vector<bool>> seen(110,vector<bool>(110));
vector<vector<bool>> seen2(110,vector<bool>(110));
vector<vector<bool>> G;
ll W,H;
void dfs(ll h, ll w){
    seen[h][w] = 1;
    cnt++;
    rep(dir,8){
        ll nh = h + dy[dir];
        ll nw = w + dx[dir];

        if(!In_map(nh,nw,H,W)) continue;
        if(G[nh][nw] == 0) continue;
        if(seen[nh][nw]) continue;
        dfs(nh,nw);
    }



}


int main() {
    while(1){
    seen = seen2;
    G.clear();
    cin>>W>>H;
    if(W==0 && H==0) break;
    ll num=0;
    rep(i,H){
        G.emplace_back();
        rep(j,W){
            bool c;
            cin>>c;
            G[i].pb(c);
        }
    }
    
    ll cnt=0;
    rep(i,H){
        rep(j,W){
            if(G[i][j] == 0) continue;
            if(seen[i][j]) continue;
            dfs(i,j);
            cnt++;
            
        }
    }

    cout<<cnt<<endl;
    }
    return 0;
    

}

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

#define rep(i, n) for(int i = 0; i < n; i++)

typedef long long ll;
using namespace std;
int mod = 1000000007;

int grid[52][52];
bool visited[52][52];

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

void init(){
    rep(i, 52) rep(j, 52) {grid[i][j] = 0; visited[i][j] = false;}
}

void dfs(int px, int py, int sx, int sy){
    visited[sx][sy] = true;
    rep(i, 8){
        int nx = sx+dx[i];
        int ny = sy+dy[i];
        if(grid[nx][ny] == 1 && !visited[nx][ny]){
            dfs(sx, sy, nx, ny);
        }
    }
}

int main(void){
    while(true){
        int res = 0;
        init();
        int W, H; cin >> W >> H;
        if(W == 0) break;
        rep(i, H){
            rep(j, W){
                cin >> grid[i+1][j+1];
            }
        }
        for(int i = 1; i <= H; i++){
            for(int j = 1; j <= W; j++){
                if(grid[i][j] == 1 && !visited[i][j]){
                    dfs(-1, -1, i, j);
                    res++;
                }
            }
        }
        cout << res << endl;
    }
    return 0;
}