Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

C++: #include <iostream>
#include <vector>
using namespace std;
typedef vector<vector<int> > VVI;
typedef vector<vector<bool> > VVB;

int w, h, cnt;

void labeling(VVI& map, VVB& visited, int x, int y) {
    visited[x][y] = true;
    for (int i = -1; i <= 1; ++i) {
        for (int j = -1; j <= 1; ++j) {
            if (x+i < 0 || h <= x+i || y+j < 0 || w <= y+j)
                continue;
            if (!visited[x+i][y+j] && map[x+i][y+j])
                labeling(map, visited, x+i, y+j);
        }
    }
}

int main(int argc, char *argv[])
{
    while (cin >> w >> h, w | h) {
        VVI map(h, vector<int>(w));
        VVB visited(h, vector<bool>(w, false));
        cnt = 0;
        for (int i = 0; i < h; ++i)
            for (int j = 0; j < w; ++j)
                cin >> map[i][j];
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                if (!visited[i][j] && map[i][j]) {
                    cnt++;
                    labeling(map, visited, i, j);
                }
            }
        }
        cout << cnt << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;
int dx[8]={0,1,0,-1,1,1,-1,-1};
int dy[8]={1,0,-1,0,1,-1,1,-1};

bool dfs(int sx,int sy,vector<vector<bool> >&c){
if(c[sx][sy]==false) return false;
c[sx][sy]=false;
for(int i=0;i<8;i++){
int nx=sx+dx[i];
int ny=sy+dy[i];
bool flag=dfs(nx,ny,c);
}
return true;
}

int main(void){
int h,w,temp;
while(1){
cin>>w>>h;
if(w==0) break;
vector<vector<bool> > c(w+2,vector<bool>(h+2,false)) ;
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
cin>>temp;
if(temp==1) c[i][j]=true;
}
}
int t=0;
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
bool flag=dfs(i,j,c);
if(flag) t++;
}
}
cout<<t<<endl;
}
return 0;
}

C++: #include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(a, b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define pb push_back
#define sint(x) cout <<#x<<" = "<<(x)<<endl
#define spair(p) cout <<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(i,v.size())cout<<" "<<v[i];cout<<endl
#define sset(s) cout<<#s<<":";each(i,s)cout <<" "<<i;cout<<endl

using namespace std;

typedef pair<int,int>P;

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

int flag[MAX_N][MAX_N];
int w,h;

void dfs(int u,int v)
{
    flag[u][v] = 0;
    rep(i,8){
        int nu = u + dx[i],nv = v + dy[i];
        if(0 <= nu && nu < h && 0 <= nv && nv < w && flag[nu][nv]){
            dfs(nu,nv);
        }
    }
    return;
}

int main()
{
    while(1){
        cin >> w >> h;
        if(w == 0){
            break;
        }
        rep(i,h){
            rep(j,w){
                cin >> flag[i][j];
            }
        }
        int ans = 0;
        rep(i,h){
            rep(j,w){
                if(flag[i][j] != 0){
                    dfs(i,j);
                    ans++;
                }
            }
        }
        cout << ans << "\n";
    }
}

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

void input(int **, int, int);
int howManyIslands(int **, int, int);
void howBigIslands(int **, bool **, int, int, int, int);

int main()
{
	int **island, w, h;
	while(true){
		cin >> w >> h;
		if(w==h && h==0) break;
		island = new int*[w];
		for(int i=0; i<w; i++)
			island[i] = new int[h];
		input(island, w, h);
		cout << howManyIslands(island, w, h) << endl;
		for(int i=0; i<w; i++)
			delete[] island[i];
		delete[] island;
	}
	return 0;
}

void input(int **island, int w, int h)
{
	char buf[60];
	cin.clear();cin.ignore();
	for(int i=0; i<h; i++){
		cin.getline(buf, sizeof(buf));
		int j=0, k=0;
		while(buf[j] != '\0'){
			if(buf[j] != ' '){
				switch(buf[j]){
				case '0':
					island[k][i] = 0;
					k++;
					break;
				case '1':
					island[k][i] = 1;
					k++;
					break;
				}
				if(w==k) break;
			}
			j++;
		}
	}
}

int howManyIslands(int **island, int w, int h)
{
	bool **check;
	int ans = 0;
	check = new bool*[w];
	for(int i=0; i<w; i++){
		check[i] = new bool[h];
		for(int j=0; j<h; j++)
			check[i][j] = false;
	}
	for(int i=0; i<w; i++){
		for(int j=0; j<h; j++){
			if(check[i][j])continue;
			if(island[i][j]){
				ans++;
				howBigIslands(island, check, i, j, w, h);
			} else {
				check[i][j] = true;
			}
		}
	}
	for(int i=0; i<w; i++)
		delete[] check[i];
	delete[] check;

	return ans;
}

void howBigIslands(int **island, bool **check, int x, int y, int w, int h)
{
	if(check[x][y]) return;
	enum __DIR{ UPPER_LEFT, UP, UPPER_RIGHT, LEFT, RIGHT, LOWER_LEFT,
		UNDER, LOWER_RIGHT, DIR_END};
	int periphe[DIR_END][2]={
			{-1, -1}, {0, -1}, {1, -1},
			{-1, 0}, {1, 0},
			{-1, 1}, {0, 1}, {1, 1}
	};
	check[x][y] = true;
	for(int i=0; i<DIR_END; i++){
		if(x+periphe[i][0] < 0 || w-1 < x+periphe[i][0] ||
			y+periphe[i][1] < 0 || h-1 < y+periphe[i][1]) continue;
		if(island[x+periphe[i][0]][y+periphe[i][1]]){
			if(!check[x+periphe[i][0]][y+periphe[i][1]])
				howBigIslands(island, check,
				x+periphe[i][0], y+periphe[i][1], w, h);
		}
	}
}