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<vector>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF (1<<29)

int c[52][52];
int w,h;
void del(int y,int x){
	c[y][x]=0;
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			if(c[y+i][x+j])del(y+i,x+j);
		}
	}
}
int main(){
	while(cin>>w>>h,w||h){
		memset(c,0,sizeof(c));
		for(int i=1;i<=h;i++)
			for(int j=1;j<=w;j++)cin>>c[i][j];
		int ans=0;
		for(int i=1;i<=h;i++)
			for(int j=1;j<=w;j++)
				if(c[i][j]){
					ans++;
					del(i,j);
				}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> P;

int w, h;
int data[50][50];	//data[row][col]

bool isRange(int r, int c) {
	return (r >= 0 && r < h && c >= 0 && c < w);
}

int main() {
	while (cin >> w >> h) {
		if (!w) break;
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
				cin >> data[i][j];
		
		int ans = 0;
		bool used[50][50] = {false};
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (!data[i][j] || used[i][j]) continue;
				ans++;
				
				//nurima-su (i, j) kara ikeru masu wo by bfs.
				queue<P> que;	//row, col
				que.push(P(i, j));
				while (!que.empty()) {
					int r = que.front().first;
					int c = que.front().second;
					que.pop();
					
					if (used[r][c]) continue;	//itidosika sirabenai (edakari)
					used[r][c] = true;	//visit
					
					for (int dr = -1; dr <= 1; dr++) {
						for (int dc = -1; dc <= 1; dc++) {
							if (dr == 0 && dc == 0) continue;
							if (!isRange(r + dr, c + dc) || !data[r + dr][c + dc]) continue;	//ikeruka hantei
							que.push(P(r + dr, c + dc));
						}
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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

#define rep(i,n) for(int i=0; i<n; i++)

int nx[8]={1,1,1,0,0,-1,-1,-1}, ny[8]={1,0,-1,1,-1,1,0,-1};
int w, h;
int mp[60][60];

void solve(int x, int y){
  if(x<0 || h<=x || y<0 || w<=y || mp[x][y]!=1)return;
  mp[x][y]=0;
  rep(i,8)solve(x+nx[i], y+ny[i]);
}

int main(){
  while(true){
    cin>>w>>h;
    if(w==0&&h==0)break;
    memset(mp, 0, sizeof(mp));
    rep(i,h)rep(j,w)cin>>mp[i][j];
    int cnt=0;
    rep(i,h)rep(j,w)if(mp[i][j]==1){solve(i, j); cnt++;}
    cout<<cnt<<endl;
  }
  return 0;
}

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

void add_label(multimap<int,int> &tbl, int a, int b){
	if( a == b ) return;
	if( a > b ) tbl.insert( make_pair(a, b) );
	else tbl.insert( make_pair(b, a) );
}

int main(void){
	int w, h;
	while( cin>>w>>h, w||h ) {
		vector<vector<int> > field( h );
		vector<vector<int> > labeled( h );
		for( int i=0; i<h; ++i ) {
			field[i] = vector<int>( w );
			labeled[i] = vector<int>( w );
			for( int j=0; j<w; ++j ) {
				cin>>field[i][j];
				labeled[i][j] = 0;
			}
		}
		int label = 0;
		multimap<int, int> tbl;
		for( int i=0; i<h; ++i ) {
			for( int j=0; j<w; ++j ) {
				if( field[i][j] ){
					bool f = false;
					if( i ) {
						if( j && labeled[i-1][j-1] ) {
							if(!f) labeled[i][j] = labeled[i-1][j-1];
							f = true;
						}
						if( labeled[i-1][j] ) {
							if(!f) labeled[i][j] = labeled[i-1][j];
							else add_label( tbl, labeled[i][j], labeled[i-1][j] );
							f = true;
						}
						if( j + 1 < w && labeled[i-1][j+1] ) {
							if(!f) labeled[i][j] = labeled[i-1][j+1];
							else add_label( tbl, labeled[i][j], labeled[i-1][j+1] );
							f = true;
						}
						if( j && labeled[i][j-1] ) {
							if(!f) labeled[i][j] = labeled[i][j-1];
							else add_label( tbl, labeled[i][j], labeled[i][j-1] );
							f = true;
						}
					} else {
						if( j && labeled[i][j-1] ){
							if(!f) labeled[i][j] = labeled[i][j-1];
							else add_label( tbl, labeled[i][j], labeled[i][j-1] );
							f = true;
						}
					}
					if(!f) labeled[i][j] = ++label;
				}
			}
		}
		for( auto x : tbl ) {
			int a = x.first;
			int b = x.second;
			while( tbl.find(b) != tbl.end() ) {
				b = tbl.find(b)->second;
			}
			for( int i=0; i<h; ++i ) {
				for( int j=0; j<w; ++j ) {
					if( labeled[i][j] == a ) labeled[i][j] = b;
				}
			}
		}
		
		/*
		for( int i=0; i<h; ++i ) {
			cerr<<"!";
			for( int j=0; j<w; ++j ) {
				cerr<<labeled[i][j];
			}
			cerr<<endl;
		}*/
		
		set<int> nums;
		nums.insert(0);
		for( int i=0; i<h; ++i ) {
			for( int j=0; j<w; ++j ) {
				nums.insert( labeled[i][j] );
			}
		}
		cout<<nums.size()-1<<endl;
	}
	return 0;
}

C++: #include<stdio.h>
void draw(int x,int y);
int w,h,c[51][51];
int main(){
	int i,j;
	int count;
	scanf("%d %d",&w,&h);
	while((w!=0)&&(h!=0)){
		count=0;
		for(j=0;j<h;j++){
			for(i=0;i<w;i++){
				scanf("%d",&c[j][i]);
			}
		}
		for(i=0;i<w;i++){
			c[h][i]=0;
		}
		for(i=0;i<h;i++){
			c[i][w]=0;
		}
		for(i=0;i<w;i++){
			for(j=0;j<h;j++){
				if(c[j][i]==1){
					count++;
					draw(j,i);
				}
			}
		}
		printf("%d\n",count);
		scanf("%d %d",&w,&h);
	}
	return 0;
}
void draw(int x,int y){
	int i,j;
	c[x][y]=0;
	for(i=0;i<3;i++){
		for(j=0;j<3;j++){
			if(c[x-1+i][y-1+j]==1){
				draw(x-1+i,y-1+j);
			}
		}
	}
}

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

// macro
#define rep(i,n) for(i=0;i<n;i++)
#define ll long long
#define all(v) v.begin(), v.end()

// code starts
void dfs(vector<vector<int>> &c,int nx,int ny)
{
  int i,j;
  c[nx][ny]=0;
  for(i=-1;i<=1;i++)for(j=-1;j<=1;j++)
  {
    if(i==0&&j==0)continue;
    if(c[nx+i][ny+j]==1)
    {
      dfs(c,nx+i,ny+j);
    }
  }
}

int main()
{
  int w,h;cin>>w>>h;
  while(w!=0||h!=0)
  {
    int i,j;
    vector<vector<int>> c(h+2,vector<int>(w+2,0));
    rep(i,h)rep(j,w)cin>>c[i+1][j+1];
    int ans=0;
    rep(i,h)rep(j,w)
    {
      if(c[i+1][j+1]==1)
      {
        dfs(c,i+1,j+1);
        ans++;
      }
    }
    cout<<ans<<endl;
    cin>>w>>h;
  }
}