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>
using namespace std;

void proc(vector<int> &maps,int p,int w,int h){
	if(maps[p]==1) maps[p]=2;
	else return;
	int x=p%w;
	int y=p/w;
	for(int i=-1;i<=1;++i){
		for(int j=-1;j<=1;++j){
			int nx=x+j;
			int ny=y+i;
			if(nx>=0&&ny>=0&&nx<w&&ny<h){
				proc(maps,nx+ny*w,w,h);
			}
		}
	}
}

int main(void){
	int w,h;
	while(cin>>w>>h,w&&h){
		vector<int> chizu(w*h);
		for(int i=0;i<h;++i){
			for(int j=0,a;j<w;++j){
				cin>>a;
				chizu[i*h+j]=a;
			}
		}
		int count = 0;
		for(int i=0;i<w*h;++i){
			if(chizu[i]==1){
				proc(chizu,i,w,h);
				++count;
			}
		}
		cout<<count<<endl;
	}
	return 0;
}

C++: 
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <bitset>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for (int i=(n)-1;i>=0;i--)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define REPS(i,f,n) for (int i=(f)-1;i>=(n);i--)
int dx[8] = {1, 0, 0, -1, 1, 1, -1, -1};
int dy[8] = {0, 1, -1, 0, 1, -1, 1, -1};

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

void slove(void){
	int cnt = 0;
	queue<pint> q;
	rep(i, h){
		rep(j, w){
			if (board[i][j] == 1){
				q.push(mp(i, j));
				cnt++;
				/*
				rep(k, h){
					rep(l, w){
						printf("%d", board[k][l]);
					}
					printf("\n");
				}
				*/
				while(!q.empty()){
					pint now = q.front(); q.pop();
					rep(i, 8){
						int nowy = now.fi + dy[i];
						int nowx = now.se + dx[i];
						if (0 <= nowy && nowy <= h - 1 && 0 <= nowx && nowx <= w - 1 && board[nowy][nowx] == 1){
							board[nowy][nowx] = 0;
							q.push(make_pair(nowy, nowx));
						}
					}
				}
			}
		}
	}

	printf("%d\n", cnt);
	return;
}

void scan(int w, int h){
	rep(i, h) rep(j, w) cin >> board[i][j];

	slove();
	return;
}

int main(void){
	rep(i, 1000){
		cin >> w >> h;

		if (w == 0) break;
		scan(w, h);
	}
	return 0;
}

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

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

void dfs(int x, int y, vector<vector<bool> >& canGo){
  canGo[x][y] = false;
  for(int i = 0; i < 8; i++)
    if(canGo[x + dx[i]][y + dy[i]])
      dfs(x + dx[i], y + dy[i], canGo);
  return;
}


int main(void){
  while(true){
    int w, h;
    cin >> w >> h;
    if(w == 0 && h == 0){ break; }

    int input;
    vector<vector<bool> > canGo(h + 2, vector<bool>(w + 2, false));
    for(int i = 1; i <= h; i++){
      for(int j = 1; j <= w; j++){
        cin >> input;
        if(input == 1){ canGo[i][j] = true; }
      }
    }

    int count = 0;
    for(int i = 1; i <= h; i++){
      for(int j = 1; j <= w; j++){
        if(canGo[i][j]){
          ++count;
          dfs(i, j, canGo);
        }
      }
    }

    cout << count << endl;
  }

  return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

bool used[55][55];
int h,w;
int a[55][55];
void dfs(int x,int y){
    REP(i,-1,2)REP(j,-1,2){
        int nx=x+i,ny=y+j;
        if(nx<0||nx>=h||ny<0||ny>=w)continue;
        if(used[nx][ny]||a[nx][ny]==0)continue;
        used[nx][ny]=1;
        dfs(nx,ny);
    }
}

void solve(){
    rep(i,h)rep(j,w)cin>>a[i][j];
    rep(i,h)rep(j,w)used[i][j]=false;
    int ans=0;
    rep(i,h)rep(j,w){
        if(used[i][j]||a[i][j]==0)continue;
        ++ans;
        used[i][j]=1;
        dfs(i,j);
    }
    cout<<ans<<endl;
}
int main(){
    while(cin>>w>>h,w!=0)solve();
    return 0;
}

C++: #include <stdio.h>

	int sima[51][51];
	int cnt=0;
	int cflag=0;

int count_sima(int w,int h){
	if(w<0)return 0;
	if(h<0)return 0;
	if(sima[w][h]==3)return 0;
	if(sima[w][h]==0)return 0;

		if(cflag==0){
			cnt++;
			cflag=1;
		}
		

		sima[w][h]=0;

		count_sima(w,h+1);
		count_sima(w+1,h+1);
		count_sima(w+1,h);
		count_sima(w+1,h-1);
		count_sima(w,h-1);
		count_sima(w-1,h-1);
		count_sima(w-1,h);
		count_sima(w-1,h+1);


	return 0;
}


int main(void){
	int w2,h2; //島の大きさを格納する
	int p;

	scanf("%d %d",&w2,&h2);
	while(w2!=0 && h2!=0){

		//島配列をすべてを3にする
		for(int i=0; i<51; i++){
			for(int j=0; j<51; j++){
				sima[i][j]=3;
			}
		}

		//入力された地図のデータを設定
		for(int i=0; i<h2; i++){
			for(int j=0; j<w2; j++){
				scanf("%d",&p);
				sima[i][j]=p;
			}
		}

		//ここまで入力された地図のデータを設定

		for(int i=0; i<h2; i++){
			for(int j=0; j<w2; j++){
				count_sima(i,j);
				cflag=0;
			}
		}

		printf("%d\n",cnt);
		cnt=0;

		scanf("%d %d",&w2,&h2);
	}

	return 0;
}

C++: #include <cstdio>
#define f(n)for(int i=0;i<n;i++)
#define s(w)scanf("%d",&w)
int a,n,m,d[99][99];
int u[16]={-1,-1,-1,0,0,1,1,1,-1,0,1,-1,1,-1,0,1};
void k(int x,int y){
	if(d[x][y]){f(8)k(x+u[i],y+u[i+8]);d[x][y]=0;}
}
int main(){
	a=0;
	s(n);s(m);
	f(9801)d[0][i]=0;
	f(n*m)s(d[i/n+1][i%n+1]);
	f(n*m)if(d[i/n+1][i%n+1])a++,k(i/n+1,i%n+1);
	printf("%d\n",a);
	main();
}