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 <queue>

using namespace std;

struct  pos {
	int x, y;
	pos(int _x, int _y) { x = _x; y = _y;}
};

int a[50][50];

void ans(int p,int q,int h,int w);

int main(){
	int H,W,count;
	while(1){
		count=0;
		cin >> H;
		cin >> W;
		if(H==0&&W==0){
			break;
		}
		for(int i=0;i<W;i++){
			for(int j=0;j<H;j++){
				cin >> a[i][j];
			}
		}
		
		for(int i=0;i<W;i++){
			for(int j=0;j<H;j++){
				if(a[i][j]==1){
					a[i][j]=0;
					ans(i,j,H,W);
					count++;
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}

void ans(int p, int q, int h, int w)
{
	queue<pos> Q;
	Q.push(pos(p, q));
	while (Q.size()) {
		pos b= Q.front();
		Q.pop();
		for (int i=-1;i<=1;i++) {
			for (int j=-1;j<=1;j++) {
				if(0<=b.x+i&&b.x+i<w&&0<=b.y+j&&b.y+j<h){
					if(a[b.x+i][b.y+j]==1){
						a[b.x+i][b.y+j]=0;
						Q.push(pos(b.x+i,b.y+j));
					}
				}
			// mawariwomiru
			}
		}
	}
}


/*
void ans(int p,int q,int h,int w){
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			if(0<=p+i&&p+i<h&&0<=q+j&&q+j<w){
				if(a[p+i][q+j]==1){
					a[p+i][q+j]=0;
					Q.push(pos(p+i,q+j));
				}
			}
		}
	}
	if(Q.size()){
		pos b=Q.front();
		Q.pop();
		ans(b.x,b.y,h,w);
	}
}*/

C++: #include <iostream>
#include <vector>
using namespace std;
const int di[8] = {-1, 0, 1, 0, -1, -1, 1, 1};
const int dj[8] = {0, 1, 0, -1, -1, 1, 1, -1};
bool ongrid(int h, int w, vector< vector<int> >& g)
{
    if (h >= 0 && h < g.size() && w >= 0 && w < g[0].size()) return true;
    return false;
}

bool dfs(int h, int w, vector< vector<int> >& g, vector< vector<bool> >& seen)
{
    seen[h][w] = true;
    if (g[h][w] == 0) return false;
    for (int i = 0; i < 8; i++)
    {
        if (ongrid(h+di[i], w+dj[i], g) && seen[h+di[i]][w+dj[i]] == false)
            dfs(h+di[i], w+dj[i], g, seen);
    }
    return true;
}

int main()
{
    while (true)
    {
        int w, h; cin >> w >> h;
        if (w == 0 && h == 0) break;
        vector< vector<int> > g(h, vector<int>(w));
        for (int i = 0; i < h; i++)
            for (int j = 0; j < w; j++)
                { int c; cin >> c; g[i][j] = c;}
        vector< vector<bool> > seen(h, vector<bool>(w, false));
        int cnt = 0;
        for (int i = 0; i < h; i++)
            for (int j = 0; j < w; j++)
                if (seen[i][j] == false)
                    if (dfs(i, j, g, seen) == true) cnt++;
        cout << cnt << endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;


int main(void){

	int w, h;

	while (cin >> w >> h, w, h){
		int c[100][100] = { 0 };
		vector<bool> island;
		island.push_back(false);

		for (int j = 1; j <= h; j++){
			for (int i = 1; i <= w; i++){
				cin >> c[j][i];
			}
		}

		int count = 1;
		for (int j = 1; j <= h; j++){
			for (int i = 1; i <= w; i++){
				if (c[j][i] != 0){

					if (c[j][i - 1] != 0) c[j][i] = c[j][i - 1];
					else if (c[j - 1][i + 1] != 0) c[j][i] = c[j - 1][i + 1];
					else if (c[j - 1][i] != 0) c[j][i] = c[j - 1][i];
					else if (c[j - 1][i - 1] != 0) c[j][i] = c[j - 1][i - 1];
					else{
						c[j][i] = count;
						count++;
						island.push_back(true);
					}

					island[c[j][i - 1]] = island[c[j - 1][i + 1]] = island[c[j - 1][i]] = island[c[j - 1][i - 1]] = false;
					island[c[j][i]] = true;

				}
			}
		}

		int result = 0;
		vector<bool>::iterator it;
		for (it = island.begin(); it != island.end(); ++it) 	if (*it == true) result++;
		
		cout << result << endl;

	}

	return 0;
}

/*
for(i=0;i<N;i++){
cin >> X[i];
}


for(i=0;i<N;i++){
for(j=0;j<N;j++){
cin >> X[i][j];
}
}
*/

C++: #include<stdio.h>

#define R(q,n) for(int q=0; q<n; q++)
size_t m[50][50],W,H,i,j,a,c,d;

void f(int x, int y)
{
	m[x][y]=0;
	R(b,9)
	{
		c = x+b/3-1;
		d = y+b%3-1;

		if(c<W&&d<H&&m[c][d])
			f(c,d);
	}
}

main()
{
	while(scanf("%d%d",&W,&H),W&H)
	{
		a=0;
		R(i,H) R(j,W)
			scanf("%d", &m[j][i]);

		R(i,H) R(j,W)
		if(m[j][i]){
				f(j,i);
				a++;
		}
		printf("%d\n", a);
	}
}

C++: #include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define rrep(i, a, b) for (int i = a; i >= (b); i--)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
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;
}
const int INF = 2e9;
const int MOD = 1e9 + 7;

int main() {
   while (true) {
      int w, h;
      cin >> w >> h;
      if (w == 0 && h == 0) break;
      vector<vector<int>> c(h, vector<int>(w)), a(h, vector<int>(w));

      rep(i, 0, h) rep(j, 0, w) cin >> c[i][j];
      int ans = 0;
      function<void(int, int)> dfs = [&](int u, int v) {
         if (a[v][u] != 0) return;
         c[v][u] = ans;
         a[v][u] = 1;
         rep(i, -1, 2) rep(j, -1, 2) {
            int x_i = u + i;
            int y_j = v + j;
            if (x_i >= 0 && x_i < w && y_j >= 0 && y_j < h && c[y_j][x_i] == 1)
               dfs(x_i, y_j);
         }
         return;
      };
      rep(i, 0, h) {
         rep(j, 0, w) {
            if (a[i][j] == 0 && c[i][j] == 1) {
               ans++;
               dfs(j, i);
            }
         }
      }
      cout << ans << endl;
   }
   return 0;
}