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 "bits/stdc++.h"
#include "math.h"
 
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef pair<ll,ll> P;
typedef vector<P> vp;
#define rep(i,a,b) for(ll i=(a);i<(b);++i)
#define SIZE(a) int((a).size())
#define out(a) cout<<(a)<<endl;
const int INF=INT_MAX;
const int MAX = 510000;
const ll MOD = 1000000007;
vin dx={-1,0,1},dy={-1,0,1};
ll w,h;
void dfs(vvll g,vvll &c,ll x,ll y){
  c[x][y]=1;
  rep(k,0,3){
    ll nx=x+dx[k];
    if(nx>=h||nx<0){continue;}
    rep(l,0,3){
      ll ny=y+dy[l];
      if(ny>=w||ny<0){continue;}
      if(!c[nx][ny]&&g[nx][ny]){
        dfs(g,c,nx,ny);
      }
    }
  }
}
int main(){
  do{
    ll cnt=0;
    cin>>w>>h;
    if(w==0&&h==0){break;}
    vvll g(h,vll(w)),c(h,vll(w,0));
    rep(i,0,h){
      rep(j,0,w){
        cin>>g[i][j];
      }
    }
    rep(i,0,h){
      rep(j,0,w){
        if(!c[i][j]&&g[i][j]){
          dfs(g,c,i,j);
          cnt++;
        }
      }
    }
   out(cnt);
  }while(w!=0&&h!=0);
}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define CMAX(m, x)  m = max(m,x);
#define CMIN(m, x)  m = min(m,x);
#define itn int
typedef long long lint;
typedef long double ldouble;
const int INF = 1e9;
const lint LINF = 1e18;

int cnt = 0;
bool dfs(vector<vector<int> > &bi, vector<vector<int> > &flag,int x, int y){
    bool re = false;
    if(bi[y][x] == 1){
        if(flag[y][x] == 0){
            flag[y][x] = cnt;
            re = true;
            for(int i = -1;i <= 1; i++){
                for(int j = -1; j <= 1; j++){
                    dfs(bi,flag,x + i,y + j);
                }
            }
        }
    }
    return re;
}
int main(){
    int w, h;
    vector<int> ans;
    while(1){
        cin >> w >> h;
        if(w == 0 && h == 0)
            break;
        w += 2;
        h += 2;
        vector<vector<int> > bi(h,vector<int>(w));
        vector<vector<int> > flag(h,vector<int>(w,0));
        REP(i,h - 2){
            REP(j,w - 2)
                cin >> bi[i + 1][j + 1];
        }
        REP(i,h){
            bi[i][0] = 0;
            bi[i][w -1] = 0;
        }
        REP(i,w){
            bi[0][i] = 0;
            bi[h - 1][i] = 0;
        }
        REP(i,h){
            REP(j,w){
                if(bi[i][j] == 1 && flag[i][j] == 0){
                    cnt++;
                    if(!dfs(bi,flag,j,i)) cnt --;
                }
            }
        }
        ans.push_back(cnt);
        cnt = 0;
    }
    
    REP(i,ans.size())
        cout << ans[i] << endl;
    return 0;
}