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 <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <utility>
#include <numeric>
#include <stack>
#include <queue>
using namespace std;
#define ll long long
#define INF 1000000007
int h,w;
int m[50][50];
void dfs(int k,int n){
    m[k][n]=0;
    for(int i=-1;i<=1;i++){
        for(int j=-1;j<=1;j++){
            if(i==0&&j==0)continue;
            int ni=k+i;
            int nj=n+j;
            if(0<=ni&&ni<h&&0<=nj&&nj<w&&m[ni][nj]==1)dfs(ni,nj);
        }
    }
    return;
}
int count(){
    int re=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(m[i][j]==1){
                dfs(i,j);
                re++;
            }
        }
    }
    return re;
}
int main(){
    while(1){
        cin>>w>>h;
        if(h==0&&w==0)break;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin>>m[i][j];
            }
        }
        cout<<count()<<endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int w,h,mp[50][50],
	dy[8]={-1,-1,0,1,1,1,0,-1},
	dx[8]={0,1,1,1,0,-1,-1,-1};
void func(int x,int y){
	mp[y][x]=0;
	for(int i=0;i<8;i++){
		int xx=x+dx[i],yy=y+dy[i];
		if(xx>=0&&xx<w&&yy>=0&&yy<h&&mp[yy][xx]) func(xx,yy);
	}
}
int main(void){
	while(1){
		cin>>w>>h;
		if(w==0&&h==0)break;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++) cin>>mp[i][j];
		}
		int cnt=0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(mp[i][j]==1){
					cnt++;
					func(j,i);
				}
			}
		}
		cout<<cnt<<endl;
	}
}

C++: #include <bits/stdc++.h>
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,s,e) for (int i=s;i<(e);i++)
#define All(v) (v).begin(),(v).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define int long long
using namespace std;
typedef long long llint;
typedef pair<int, int> P;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e18 * 5;

struct UnionFind
{
    vector< int > data;
    
    UnionFind(int sz){
        data.assign(sz, -1);
    }
    
    bool unite(int x, int y){
        x = root(x), y = root(y);
        if(x == y) return false;
        if(data[x] > data[y]) swap(x, y);
        data[x] += data[y];
        data[y] = x;
        return true;
    }
    
    int root(int k){
        if(data[k] < 0) return(k);
        return (data[k] = root(data[k]));
    }
    
    int size(int k){
        return -data[root(k)];
    }
};

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

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

    int w, h;
    cin >> w >> h;
    while(!(w==0 && h==0)){
        vector< vector<int> > c(h+2, vector<int>(w+2));
        REP(i, h) c[i][0] = 0;
        REP(i, h) c[i][w+1] = 0;
        REP(j, w) c[0][j] = 0;
        REP(j, w) c[h+1][j] = 0;
        FOR(i, 1, h+1)FOR(j, 1, w+1) cin >> c[i][j];

        int num = 10, unite = 0;
        UnionFind UF(500);
        FOR(i, 1, h+1)FOR(j, 1, w+1){
            int tmp = 0;
            if(c[i][j] == 0) continue;
            if(c[i][j] == 1) c[i][j] = ++num;
            for(auto y: dy)for(auto x: dx){
                if(x==1 && y==1) continue;
                if(c[i+y][j+x] == 0) continue;
                if(c[i+y][j+x] == 1){
                    c[i+y][j+x] = c[i][j];
                    tmp++;
                }else if(UF.root(c[i+y][j+x]) != UF.root(c[i][j])){
                    unite++;
                    UF.unite(c[i+y][j+x], c[i][j]);
                }
            }
        }

        //FOR(i, 1, h+1)FOR(j, 1, w+1) cout << c[i][j] << (j==w ? "\n":" ");

        cout << num - 10 - unite << "\n";
        cin >> w >> h;
    }
    return 0;
}

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

int wm,hm;
vector< vector<int> > wh(50, vector<int>(50,0));

int xp[]={-1,0,1,-1,1,-1,0,1};
int yp[]={-1,-1,-1,0,0,1,1,1};

void dfs(int x,int y){
  for(int i=0;i<8;i++){
    int nx=x+xp[i];
    int ny=y+yp[i];
    if(nx>-1&&nx<wm&&ny>-1&&ny<hm&&wh[nx][ny]==1){
      //cout<<nx<<" "<<ny<<" "<<wh[nx][ny]<<endl;
      wh[nx][ny]=0;
      dfs(nx,ny);
    }
  }
}

int main(){
  while(1){
    //int wm,hm;
    int aa,bb;
    cin>>aa>>bb;
    wm=bb;
    hm=aa;
    if(wm==0&&hm==0)break;
    //vector< vector<int> > wh(wm, vector<int>(hm));
    for(int i=0;i<wm;i++){
      for(int j=0;j<hm;j++){
        int a;
        cin>>a;
        wh[i][j]=a;
      }
    }
    int ans=0;
    for(int i=0;i<hm;i++){
      for(int j=0;j<wm;j++){
        if(wh[i][j]==1){
          //cout<<"*"<<i<<" "<<j<<endl;
          wh[i][j]=0;
          dfs(i,j);
          ans++;
        }
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}