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 <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<int,int> pint;

int c[51][51];
bool used[51][51];
const int dx[]={-1,-1,-1,0,0,1,1,1},dy[]={1,0,-1,1,-1,1,0,-1};
int main(){
    int w,h;
    while(cin>>w>>h,w|h){
        int cnt=0;
        memset(used,0,sizeof(used));
        rep(i,h)rep(j,w) cin>>c[i][j];
        rep(i,h)rep(j,w)if(!used[i][j]&&c[i][j]==1){
            ++cnt;
        queue<pair<pint,int> > q;
        q.push({{j,i},0});
        used[i][j]=true;
        while(!q.empty()){
            pair<pint,int> pi=q.front();q.pop();
            rep(k,8){
                int tx=pi.first.first+dx[k],ty=pi.first.second+dy[k];
                if(tx>=0&&ty>=0&&tx<w&&ty<h&&c[ty][tx]==1&&!used[ty][tx]){
                    q.push({{tx,ty},pi.second+1});
                    used[ty][tx]=true;
                }
            }
        }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

C++: #include <cstdio>
#include <utility>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#define REP(i,n) for(int i=0;i<n;i++)
typedef long long ll;
using namespace std;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;

int main() 
{
	
	int w,h;
	while(cin >> w >> h && !(w==0&&h==0))
	{
		int a[50][50]={};
		REP(i,h){REP(j,w){cin >> a[j][i];}}
		
		int id = 10; //島のid 初期値は適当
		
		//w=0の時
		if(a[0][0]!=0){a[0][0]=id;}
		for(int i=1;i<h;i++){
			if(a[0][i]!=0 && a[0][i-1]==0){id++;a[0][i]=id;}
			else if(a[0][i]!=0){a[0][i]=a[0][i-1];}
		}
		//w>0の時
		for(int i=1;i<w;i++)
		{
			//h=0の時
			if(a[i][0]!=0){
				if(a[i-1][0]!=0 || a[i-1][1]!=0){a[i][0]=max(a[i-1][0],a[i-1][1]);}
				else{id++;a[i][0]=id;}
			}
			//h>0の時
			for(int j=1;j<h;j++)
			{
				if(a[i][j]!=0)
				{
					int tmp = max(a[i][j-1],a[i-1][j-1]);
					tmp = max(tmp,a[i-1][j]);
					tmp = max(tmp,a[i-1][j+1]);
					if(tmp==0){id++;a[i][j]=id;}
					else{
						a[i][j] = tmp;
						if(a[i][j-1]!=0 && a[i][j-1]!=tmp)REP(k,w){REP(l,h){if(a[k][l]==a[i][j-1])a[k][l]=tmp;}}
						if(a[i-1][j-1]!=0 && a[i-1][j-1]!=tmp)REP(k,w){REP(l,h){if(a[k][l]==a[i-1][j-1])a[k][l]=tmp;}}
						if(a[i-1][j]!=0 && a[i-1][j]!=tmp)REP(k,w){REP(l,h){if(a[k][l]==a[i-1][j])a[k][l]=tmp;}}
						if(a[i-1][j+1]!=0 && a[i-1][j+1]!=tmp)REP(k,w){REP(l,h){if(a[k][l]==a[i-1][j+1])a[k][l]=tmp;}}
					}
				}
			}
		}
		
		int b[2500]={};
		REP(i,w){REP(j,h){b[i+w*j]=a[i][j];}}
		sort(b,b+h*w);
		reverse(b,b+h*w);
		
		int ans(0);
		if(b[0]>1){ans++;}
		for(int i=1;i<w*h;i++){if(b[i]!=b[i-1] && b[i]>1)ans++;}
		
		cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<queue>
#include<utility>
using namespace std;
void search(int, int);
int c[60][60] = {}, map[60][60] = {};
void mapDraw(int,int);
queue< pair<int, int> > q;
int w, h;

int main() {

	while (1) {
		
		//initialize
		int count = 0;
		for (int i = 0; i < 60; i++) {
			for (int j = 0; j < 60; j++) {
				c[i][j] = 0;
				map[i][j] = 0;
			}
		}


		cin >> w >> h;
		if (w == 0 && h == 0) break;
		//input
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> c[i][j];
			}
		}

		//search
		//unresearched:0, land:2, sea:1
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (map[i][j] == 0) {
					map[i][j] = c[i][j] + 1;
					if (c[i][j] == 1) {
						q.push(make_pair(i, j));
						count++;
					}
				}

				pair<int, int> cur = make_pair(i, j);
				while (!q.empty()) {
					cur = q.front();
					int x = cur.second;
					int y = cur.first;
					q.pop();

					//cerr << "( " << y << ", " << x << " )now"<< endl;

					for (int a = -1; a <= 1; a++) {
						for (int b = -1; b <= 1; b++) search(y + a, x + b);
					}
				}
			}
		}

		cout << count << endl;
	}


	return 0;
}

void search(int i, int j) {
	if (i < h&&i >= 0 && j < w&&j >= 0) {
		if (map[i][j] == 0) {
			map[i][j] = c[i][j] + 1;
			if (c[i][j] == 1) q.push(make_pair(i, j));
		}
	}
	//mapDraw(w, h);
}

void mapDraw(int w, int h) {
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			cerr << map[i][j];
		}
		cerr << endl;
	}
	cerr << endl;
}

C++: #include<iostream>
#include<vector>
#include <queue>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rep2(i,a,b) for(int i=a;i<=b;i++)
#define pii pair<int,int>
int w,h;

int main(){
    while(1){
        w=h=0;
        cin>>w>>h;
        int ans=0;
        if(w==0&&h==0)break;
        vector<vector< int > > c(60,vector<int>(60));
        rep2(i,1,h)rep2(j,1,w){
            cin>>c[i][j];
        }
        vector< vector< int> > visited(60,vector<int>(60));
        rep2(i,1,h){
            rep2(j,1,w){
                if(visited[i][j]||!c[i][j])continue;
                queue<pii> q;
                q.push(pii(i,j));
                visited[i][j]=1;
                while(!q.empty()){
                    pii P=q.front();
                    //cout<<P.first<<" "<<P.second<<endl;
                    q.pop();
                    rep2(ii,-1,1){
                        rep2(jj,-1,1){
                            if(c[P.first+ii][P.second+jj]&&!visited[P.first+ii][P.second+jj]){
                                q.push(pii(P.first+ii,P.second+jj));
                                visited[P.first+ii][P.second+jj]=1;
                            }
                        }
                    }
                }
                if(c[i][j])ans++;
            }
        }
        cout<<ans<<endl;
    }

}