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++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp
// ????????¶????????£???????????????????????????????????°???????????£?????????
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int w, h, cnt;
int c[100][100], m[100][100];
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int main() {
    while(scanf("%d%d",&w,&h)) {
        if (w==0&&h==0) break;
        memset(m, -1, sizeof(m)); cnt = 0;
        for (int i=0; i<h; i++) for (int j=0; j<w; j++) scanf("%d",&c[i][j]);
        for (int i=0; i<h; i++) for (int j=0; j<w; j++) {
            if (m[i][j] == -1 && c[i][j] == 1) {
                cnt++;
                queue<P> q;
                q.push(P(i,j));
                m[i][j] = 0;
                while(!q.empty()) {
                    P p = q.front(); q.pop();
                    for (int k=0; k<8; k++) {
                        int ny = p.first + dy[k];
                        int nx = p.second + dx[k];
                        if (nx >= 0 && nx < w && ny >= 0 && ny < h && m[ny][nx] == -1 && c[ny][nx] == 1) {
                            m[ny][nx] = 0;
                            q.push(P(ny,nx));
                        }
                    }
                }
            }
        }
        printf("%d\n",cnt);
    }
}

C++: #include <iostream>
using namespace std;
const int MAX = 51;

int island[MAX][MAX],H,W;
int dx[8]={1,1,1,-1,-1,-1,0,0},dy[8]={0,1,-1,0,1,-1,-1,1};


void dfs(int x,int y){
  island[x][y] = 0;

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

int main(){

  while(cin >> H >> W ,H + W){
    for(int i = 0 ; i < W ; i++)
      for(int j = 0 ; j < H ; j++)
	cin >> island[i][j];
    
    int ans = 0;
    for(int i = 0 ; i < W ; i++){
      for(int j = 0 ; j < H ; j++){
	if(island[i][j]){
	  dfs(i,j);
	  ans++;
	}
      }
    }
    cout << ans << endl;
  }

  return 0;
}

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

int state[100][100];
bool reached[100][100];
int w,h;

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

void dfs(int x, int y) {

	if(x==0 || w<x || y==0 || h<y || state[x][y] == 0)
		return;
	if(reached[x][y] == true)
		return;

	reached[x][y]=true;

	for(int i=0; i<8; i++) {
		dfs(x+dx[i],y+dy[i]);
	}
}

int main() {

	memset(state, 0, sizeof(state));
	memset(reached, false, sizeof(reached));

	cin >> w >> h;
	for(int i=1; i<=h; i++) {
		for(int j=1; j<=w; j++) {
			cin >> state[i][j];
		}
	}
	int count=0;
	for(int i=1; i<=w; i++) {
		for(int j=1; j<=h; j++) {
			if(reached[i][j]==true || state[i][j] == 0)
				continue;
			else{
				dfs(i,j);
				count++;
			}
		}
	}
	cout << count << endl;
	return 0;
}

C++: #include <cstdio>

using namespace std;

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

void dfs(int x, int y)
{
	field[x][y] = 0;
	
	for(int dx = -1; dx <= 1; dx++){
		for(int dy = -1; dy <= 1; dy++){
			int nx = x + dx;
			int ny = y + dy;
			
			if(0 <= nx && nx < w && 0 <= ny && ny < h && field[nx][ny] == 1) dfs(nx, ny);
		}
	}
	return ;
}

void solve(){
	int res = 0;
	for(int i = 0; i < w; i++){
		for(int j = 0; j < h; j++){
			if(field[i][j] == 1){
				dfs(i, j);
				res++;
			}
		}
	}
	printf("%d\n", res);
}

int main()
{
	while(scanf("%d %d", &w, &h) != EOF){
		if(w == 0 && h == 0){ break; }
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				scanf("%d", &field[i][j]);
			}
		}
		solve();
	}
	
	return 0;
}

C++: 
using namespace std;

static const int N = 52;

void dfs(int map[][N], int pi, int pj){
	const int di[] = {-1,0,1,1,1,0,-1,-1};
	const int dj[] = {1,1,1,0,-1,-1,-1,0};

	if( map[pi][pj] == 0 ){
		return ;
	}

	map[pi][pj] = 0;

	for(int i = 0; i < sizeof(di)/sizeof(di[0]); i++){
		dfs(map, pi + di[i], pj + dj[i] );
	}
}

int main(void){
	ws(cin);
	while( 1 ){
		int count = 0;
		int n, m;
		int map[N][N] = {{0,},};

		cin >> n >> m;
		if( n == 0 && m == 0 ){
			break;
		}

		for(int i = 0; i < m; i++ ){
			for(int j = 0; j < n; j++){
				cin >> map[j + 1][i + 1];
			}
		}

		for(int i = 0; i < m; i++ ){
			for(int j = 0; j < n; j++){
				if(map[j + 1][i + 1]){
					dfs(map, j + 1, i + 1);
					count++;
				}
			}
		}

		cout << count << "\n";
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
const int MAXW = 51;
const int MAXH = 51;
int map[MAXW][MAXH];
int w,h;
int dx[] = {-1,0,1,-1,1,-1,0,1};
int dy[] = {1,1,1,0,0,-1,-1,-1};
int dfs(int x,int y){
  if(map[x][y] == 0)return 0;
  map[x][y] = 0;
  for(int i = 0;i < 9;i++){
    int nx = x+dx[i];
    int ny = y+dy[i];
    if(nx >= 0 && nx < w & ny >= 0 && ny < h)
      dfs(nx,ny);
  }
  return 1;
}

int main(){
  while(true){
    cin >> w >> h;
    if(w == 0 && h == 0)break;
    for(int i = 0;i < h;i++){
      for(int j = 0;j < w;j++){
        cin >> map[i][j];
      }
    }
    int count = 0;
    for(int i = 0;i < h;i++){
      for(int j = 0;j < w;j++){
        count += dfs(j,i);
      }
    }
    cout << count << endl;
  }
}