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>
#define rep(i, n) for (ll i = 0; i < n; ++i)
typedef long long ll;
using namespace std;
const int INF = 1e9;

int h, w;
vector<vector<int>> mas;
void dfs(int y, int x) {
    mas[y][x] = 0;

    for (int dy = -1; dy <= 1; ++dy) {
        for (int dx = -1; dx <= 1; ++dx) {
            int ny = y + dy;
            int nx = x + dx;

            if (ny < 0 || ny >= h || nx < 0 || nx >= w || mas[ny][nx] == 0)
                continue;

            dfs(ny, nx);
        }
    }
}

int main() {
    while (cin >> w >> h) {
        if (h == 0 || w == 0)
            break;
        mas.assign(h, vector<int>(w, 0));
        rep(i, h) rep(j, w) cin >> mas[i][j];

        int ans = 0;
        rep(i, h) {
            rep(j, w) {
                if (mas[i][j]) {
                    dfs(i, j);
                    ++ans;
                }
            }
        }

        cout << ans << endl;
    }

    return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#define f(i,n)for(int i=0;i<n;i++)
#define s (scanf("%d",&e),e)
int main(){
	int a=0,w,n,m,b,e,d[99];
	f(i,99){
		d[i]=0;
	}
	n=s;
	m=s;
	if(n==0)exit(0);
	f(i,m){
		w=0;
		b=0;
		f(j,n){
			s;
			if(e){
				f(k,3){
					e=d[j+k];
					if(e&&e-w){
						if(w){
							f(l,n+2){
								d[l]=d[l]==e?w:d[l];
							}
							a--;
						}
						else{
							w=e;
						}
					}
				}
				if(w==0){
					a++;
					w=i*n+j+1;
				}
			}
			else{
				w=0;
			}
			d[j]=b;
			b=w;
		}
		d[n]=b;
		w=0;
	}
	printf("%d\n",a);
	main();
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <utility>

using namespace std;

void dfs(int x,int y,vector<vector<bool> >&map){
    if(!map[x][y])return;
    map[x][y]=false;
    for(int i=-1;i<2;i++)
    for(int j=-1;j<2;j++){
        if(i==0 && j==0)continue;
        int nx=x+i;
        int ny=y+j;
        dfs(nx,ny,map);
    }
    return;
}

int main(){
    while(true){
        int w,h;
        cin >> w >> h;
        if(w==0)break;
        vector<vector<bool> > map(h+2,vector<bool>(w+2,false));
        for(int i=1;i<=h;i++)
        for(int j=1;j<=w;j++){
            int t;
            cin >> t;
            if(t==1)map[i][j]=true;
        }

        int ans=0;
        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                if(map[i][j]){
                    ans++;
/*                    for(int k=1;k<=w;k++){
                    for(int l=1;l<=h;l++){
                        if(map[l][k])cout<<'o';
                        else cout << '_';
                    }
                    cout << endl;
                    }
                    cout << endl;*/
                    dfs(i,j,map);
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
using namespace std;
int w,h;
vector<int> nx;
vector<int> ny;
bool check(int **field){
  nx.clear();
  ny.clear();
  for (int i=0;i<h;i++){
    for (int j=0;j<w;j++){
      if (field[i][j]==1){
	nx.push_back(i);
	ny.push_back(j);
	return true;
      }
    }
  }
  return false;
}
void find(int **field){
  int x[nx.size()];
  int y[ny.size()];
  int s=nx.size();
  for (int i=0;i<s;i++){
    field[nx[i]][ny[i]]=0;
    x[i]=nx[i];
    y[i]=ny[i];
  }
  nx.clear();
  ny.clear();
  for (int i=0;i<s;i++){
    if (field[x[i]+1][y[i]]==1&&x[i]+1<h){
      nx.push_back(x[i]+1);
      ny.push_back(y[i]);
    }
    if (field[x[i]-1][y[i]]==1&&x[i]-1>=0){
      nx.push_back(x[i]-1);
      ny.push_back(y[i]);
    }
    if (field[x[i]][y[i]+1]==1&&y[i]+1<w){
      nx.push_back(x[i]);
      ny.push_back(y[i]+1);
    }
    if (field[x[i]][y[i]-1]==1&&y[i]-1>=0){
      nx.push_back(x[i]);
      ny.push_back(y[i]-1);
    }
    if (field[x[i]+1][y[i]+1]==1&&x[i]+1<h&&y[i]+1<w){
      nx.push_back(x[i]+1);
      ny.push_back(y[i]+1);
    }
    if (field[x[i]+1][y[i]-1]==1&&x[i]+1<h&&y[i]-1>=0){
      nx.push_back(x[i]+1);
      ny.push_back(y[i]-1);
    }
    if (field[x[i]-1][y[i]+1]==1&&x[i]-1>=0&&y[i]+1<w){
      nx.push_back(x[i]-1);
      ny.push_back(y[i]+1);
    }
    if (field[x[i]-1][y[i]-1]==1&&x[i]-1>=0&&y[i]-1>=0){
      nx.push_back(x[i]-1);
      ny.push_back(y[i]-1);
    }
  }
  if (nx.empty()){
    return;
  }
  int *f[h];
  for (int i=0;i<h;i++)
    f[i]=field[i];
  return find(f);
}
int main(){
  while (cin>>w>>h,w|h!=0){
    int count=0;
    int field[h][w];
    for (int i=0;i<h;i++){
      for (int j=0;j<w;j++){
	cin>>field[i][j];
      }
    }
    int *f[h];
    for (int i=0;i<h;i++) 
      f[i]=field[i];
    while (check(f)){
      find(f);
      count++;
      }
    cout<<count<<endl;
  }
}