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>
using namespace std;
using ll=long long;
typedef pair<int,int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int w,h;
int nx,ny;
void dfs(vector<vector<int>> &G,int a,int b){
    G.at(a).at(b)=0;
    
    for(int k=-1;k<=1;k++){
        for(int s=-1;s<=1;l++){
            nx=a+k;
            ny=b+s;
        if(0<=nx&& nx<h && 0<=ny&& ny<w && G.at(nx).at(ny)==1) dfs(G,nx,ny);
        }
    }
    return ;
}



int main(){
    int w,h;
    cin>>w>>h;
    vector<vector<int>>G(h,vector<int>(w));
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>G.at(i).at(j);
        }
    }
    int count=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(G.at(i).at(j)==1){
                dfs(G,i,j);
                count++;
            }
        }
    }
cout<<count<<endl;
}

C++: #include<iostream>
#include<vector>
using namespace std;

int H,W;
vector<vector<int> > field;
void dfs(int h,int w){
    field[h][w]=0;
    for(int dh=-1;dh<=1;++dh){
        for(int dw=-1;dw<=1;dw++){
            int nh=h+dh,nw=w+dw;
            if(nh<0 || nh>=H || nw<0 || nw>=W)continue;
            if(field[nh][nw]==0)continue;
            dfs(nh,nw);
        }
    }
}

int main(){
    while(cin>>W>>H){
        if(H==0 || W==0)break;
        field.assign(H,vector<int>(W,0));
        for(int h=0;h<H;h++)for(int w=0;w<W;w++)cin>>field[h][w];
        int count=0;
        for(int h=0;h<H;h++){
            for(int w=0;w<W;w++){
                if(field[h][w]==0)continue;
                dfs(h,w);
                ++count;
            }
            
        }
        cout<<count<<endl;
    }
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
const ll MOD = 1000000007;
const ll INF = 9999999999;
template<class T> inline bool chmin(T& a, T b){if (a>b){a = b; return true;}return false;}
template<class T> inline bool chmax(T& a, T b){if (a<b){a = b; return true;}return false;}
template<class T> inline T powerM(T a,T b){if (b==0) return 1;
  T tmp = powerM(a,b/2); if (b%2==0) return tmp*tmp%MOD; else return tmp*tmp%MOD*a%MOD; }


int H,W;
vector<vector<int>> field;
vector<int> di = {0, 1, 0, -1, 1, 1, -1, -1};
vector<int> dj = {1, 0, -1, 0, 1, -1, 1, -1};

void dfs(int nowi, int nowj){
  field[nowi][nowj] = 0;
  for (int i=0; i<di.size(); i++){
    int ni = nowi + di[i];
    int nj = nowj + dj[i];
    if (ni < 0 || ni >= H || nj < 0 || nj >= W) continue;
    if (field[ni][nj]) dfs(ni,nj);
  }
}

int main() {
  cin >>W >>H;
  while (W * H){
    field.assign(H, vector<int>(W, 0));
    rep(i, H) rep(j, W) 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]) dfs(i,j), res++;
      }
    }

    cout <<res <<endl;

    cin >>W >>H;
  }

}

C++: #include <iostream>
#include <vector>
#include <queue>

using namespace std;

int main(void) {
	int w,h;
	while(cin >> w >> h && w && h) {
		vector< vector<int> > data(w+2,vector<int>(h+2));
		vector< vector<bool> > check(w+2,vector<bool>(h+2,false));
		queue<int> sx,sy;
		for (int i = 0; i < h + 2; ++i) {
			for (int j = 0; j < w + 2; ++j) {
				if (i == 0 || i == h+1 || j == 0 || j == w+1) {
					data[i][j] = 0;
				}
				else {
					cin >> data[i][j];
					if (data[i][j] == 1) {
						sx.push(j);
						sy.push(i);
					}
				}
			}
		}

		int nx,ny;
		int count = 0;
		queue<int> fx,fy;
		while(!sx.empty()) {
			nx = sx.front();
			ny = sy.front();
			sx.pop();
			sy.pop();
			if (!check[ny][nx]) {
				check[ny][nx] = true;
				fx.push(nx);
				fy.push(ny);
				++count;
			}
			while (!fx.empty()) {
				nx = fx.front();
				ny = fy.front();
				fx.pop();
				fy.pop();
				//cout << ny << " " << nx << endl;
				if (!check[ny+1][nx] && data[ny+1][nx] == 1) {
					check[ny+1][nx] = true;
					fx.push(nx);
					fy.push(ny+1);
				}
				if (!check[ny-1][nx] && data[ny-1][nx] == 1) {
					check[ny-1][nx] = true;
					fx.push(nx);
					fy.push(ny-1);
				}
				if (!check[ny+1][nx+1] && data[ny+1][nx+1] == 1) {
					check[ny+1][nx+1] = true;
					fx.push(nx+1);
					fy.push(ny+1);
				}
				if (!check[ny+1][nx-1] && data[ny+1][nx-1] == 1) {
					check[ny+1][nx-1] = true;
					fx.push(nx-1);
					fy.push(ny+1);
				}
				if (!check[ny-1][nx-1] && data[ny-1][nx-1] == 1) {
					check[ny-1][nx-1] = true;
					fx.push(nx-1);
					fy.push(ny-1);
				}
				if (!check[ny-1][nx+1] && data[ny-1][nx+1] == 1) {
					check[ny-1][nx+1] = true;
					fx.push(nx+1);
					fy.push(ny-1);
				}
				if (!check[ny][nx+1] && data[ny][nx+1] == 1) {
					check[ny][nx+1] = true;
					fx.push(nx+1);
					fy.push(ny);
				}
				if (!check[ny][nx-1] && data[ny][nx-1] == 1) {
					check[ny][nx-1] = true;
					fx.push(nx-1);
					fy.push(ny);
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}