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>

using namespace std;

int W,H;

bool table[50][50];


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

void dfs(int x,int y){
	table[x][y]=0;
	for(int i=0;i<8;i++){
		if(0<=x+dx[i]&&x+dx[i]<W&&0<=y+dy[i]&&y+dy[i]<H&&table[x+dx[i]][y+dy[i]])
			dfs(x+dx[i],y+dy[i]);
	}
	return ;
}


int main(void){
	while(cin >> W >> H,W|H){
		for(int y=0;y<H;y++)
			for(int x=0;x<W;x++)
				cin >> table[x][y];
		int num=0;
		for(int y=0;y<H;y++)
			for(int x=0;x<W;x++)
				if(table[x][y])
					num++,dfs(x,y);
		cout << num << endl;
	}
	return 0;
}

C++: #include<stdio.h>
#include<iostream>

int field[51][51];
int w,h;

void land(int x,int y){
	field[x][y]=0;

	for(int yy=-1;yy<=1;yy++){
		for(int xx=-1;xx<=1;xx++){
			if(x+xx>=0&&x+xx<=w&&y+yy>=0&&y+yy<=h){
			   if(field[x+xx][y+yy]==1) land(x+xx,y+yy);
			}
		}
	}
	return;
}

int main(void){

	int kazu=0;

	scanf("%d %d",&w,&h);

	if(w==0||h==0) return 0;

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			scanf("%d",&field[j][i]);
		}
	}

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){

			if(field[j][i]==1){
				land(j,i);
				kazu++;
			}

		}
	}

	printf("%d",kazu);
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<queue>

using namespace std;

int ans = 0;
bool dfs(vector< vector<int> > &island,int x,int y){
	int dx[8] = {-1,-1,-1, 0, 0, 1, 1, 1};
	int dy[8] = {-1, 0, 1,-1, 1,-1, 0, 1};
	if (island[x][y] == 0) return false;
	island[x][y] = 0;
	for (int i = 0; i< 8; i++){
		if(0 <= x + dx[i] and x + dx[i] < island.size() and 0 <= y + dy[i] and y + dy[i] < island[0].size()){
			dfs(island,x + dx[i],y + dy[i]);
		}
	}
	return true;
}

int main(){
	while(1){
		int w,h; cin >> w >> h;
		int ans = 0;
		if (w + h == 0) break;
		vector< vector<int> > island(h, vector<int>(w,0));
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				cin >> island[i][j];
			}
		}

		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (dfs(island,i,j)){
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
typedef pair<int,int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int w,h;
int nx,ny;
vector<vector<int>> G;
void dfs(int a,int b){
     if(0>a|| a>=h || 0>b|| b>=w) return ;
     if( G.at(a).at(b)==0) return ; 
     G.at(a).at(b)=0;
    for(int k=-1;k<=1;k++){
        for(int s=-1;s<=1;s++){
            nx=a+k;
            ny=b+s;
            dfs(nx,ny);
        }
    }
}
 
 
 
int main(){
    cin>>w>>h;
    G.resize(h,vector<int>(w));
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>G.at(i).at(j);
        }
    }
    int count=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(G.at(i).at(j)==1){
                count++;
                dfs(i,j);
            }
        }
    }
cout<<count<<endl;
}

C++: #include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };
const int dx2[] = { -1,-1, 0,1,1, 1, 0,-1 }; const int dy2[] = { 0,-1,-1,-1,0, 1, 1, 1 };
struct UnionFind {
	vector<int> v;
	UnionFind(int n) : v(n)  { for (int i = 0; i < n; i++) v[i] = i; }
	int  find(int x)         { return v[x] == x ? x : v[x] = find(v[x]); }
	void unite(int x, int y) { v[find(x)] = find(y); }
};
int h, w;
int cell[50][50];
int main() {
	while (cin>>w>>h,h) {

		REP(i, h)
			REP(j, w)
			cin >> cell[i][j];
		int ans = 0;
		REP(i, h)
			REP(j, w)
			if (cell[i][j] == 1) {
				ans++;
				queue<pii> Q;
				Q.push(pii(i, j));
				cell[i][j] = 0;
				while (!Q.empty()){
					pii q = Q.front();Q.pop();
					int y = q.first;
					int x = q.second;
					REP(k,8) {
						if (0 <= y + dy2[k] && y + dy2[k] < h && 0 <= x + dx2[k] && x + dx2[k] < w &&cell[y + dy2[k]][x + dx2[k]]) {
							cell[y + dy2[k]][x + dx2[k]] = 0;
							Q.push(pii( y + dy2[k], x + dx2[k]));
						}
					}
				}
			}
		cout << ans << endl;
	}
}