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>

bool find_earth(const std::vector<int>& c, int w, int h, int *x, int *y) {
  *x = -1;
  *y = -1;
  for (int i=0; i<h; i++) {
    for (int j=0; j<w; j++) {
      if (c[i*w+j]) {
        *x = i;
        *y = j;
        return true;
      }
    }
  }
  return false;
}

void spread(std::vector<int>& c, int x, int y, int w, int h) {
  c[x*w+y] = false;
  if (0 < x && c[(x-1)*w+y]) spread(c, x-1, y, w, h);
  if (x < h-1 && c[(x+1)*w+y]) spread(c, x+1, y, w, h);
  if (0 < y && c[x*w+(y-1)]) spread(c, x, y-1, w, h);
  if (y < w-1 && c[x*w+(y+1)]) spread(c, x, y+1, w, h);
  
  if (0 < x && 0 < y && c[(x-1)*w+(y-1)]) spread(c, x-1, y-1, w, h);
  if (x < h-1 && y < w-1 && c[(x+1)*w+(y+1)]) spread(c, x+1, y+1, w, h);
  if (0 < x && y < w-1 && c[(x-1)*w+(y+1)]) spread(c, x-1, y+1, w, h);
  if (0 < y && x < h-1 && c[(x+1)*w+(y-1)]) spread(c, x+1, y-1, w, h);
}

bool solve() {
  int w, h;
  std::cin >> w >> h;
  if (w == 0 && h == 0) return false;
  std::vector<int> c(w*h);
  for (int i=0; i<h; i++) {
    for (int j=0; j<w; j++) {
      std::cin >> c[i*w+j];
    }
  }
  
  int cnt = 0, x, y;
  bool found = find_earth(c, w, h, &x, &y);
  while(found) {
    spread(c, x, y, w, h);
    cnt++;
    found = find_earth(c, w, h, &x, &y);
  }
  
  std::cout << cnt << std::endl;
  
  return true;
}

int main() {
  while(solve());
  return 0;
}

C++: #include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;

#define REP(i,n) for(int i=0; i<n; i++)
#define FOR(i,a,b) for(int i=a; i<=b; i++)
#define FORR(i,a,b) for (int i=a; i>=b; i--)

typedef long long ll;

int w,h;
bool c[50][50];
int dx[9] = {-1,-1,-1,0,0,0,1,1,1} , dy[9] = {-1,0,1,-1,0,1,-1,0,1};

bool isok(int i ,int j){
	return i>=0 && i<h && j>=0 && j<w;
}

void dfs(int i, int j){
	c[i][j] = 0;
	REP(k,9){
		int ii=i+dx[k] , jj=j+dy[k];
		if (isok(ii,jj)&&c[ii][jj]){
			dfs(ii,jj);
		}
	}
}

int main(void){
	while (cin >> w >> h && w){
		REP(i,h) REP(j,w) cin >> c[i][j];
		int ans = 0;
		REP(i,h){
			REP(j,w){
				if (c[i][j]==1){
					dfs(i,j);
					ans++;
				}
			}
		}
		cout << ans << endl;
	}

	return 0;
}

C++: #include <cstdio>
#include <cstring>
using namespace std;

int w, h;
int map[100][100];
int island_number[100][100];

int angle[][2] = {
	{-1, 0}, {-1, -1}, {0, -1}, {1, -1},
	{1, 0}, {1, 1}, {0, 1}, {-1, 1}
};

void dfs(int y, int x)
{
	for (int i = 0; i < 8; i++){
		if (
		y + angle[i][0] >= 0 &&
		y + angle[i][0] < h &&
		x + angle[i][1] >= 0 &&
		x + angle[i][1] < w &&
		island_number[y + angle[i][0]][x + angle[i][1]] == (y + angle[i][0]) * h + x + angle[i][1]
		){
			island_number[y + angle[i][0]][x + angle[i][1]] = island_number[y][x];
			dfs(y + angle[i][0], x + angle[i][1]);
		}
	}
}

int main()
{
	
	while (scanf("%d %d", &w, &h), w + h){
		memset(island_number, 0, sizeof(island_number));
		
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				scanf("%d", &map[i][j]);
				if (map[i][j] == 1){
					island_number[i][j] = i * h + j;
				}
				else {
					island_number[i][j] = -1;
				}
			}
		}
		
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (island_number[i][j] == i * h + j && island_number[i][j] >= 0){
					dfs(i, j);
				}
			}
		}
		
		bool ex[100 * 100] = {false};
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (island_number[i][j] >= 0){
					ex[island_number[i][j]] = true;
				}
//				printf("%d ", island_number[i][j]);
			}
//			puts("");
		}
		
		int cnt = 0;
		for (int i = 0; i < 100 * 100; i++){
			cnt += ex[i];
		}
		
		printf("%d\n", cnt);
	}
	
	return 0;
}

C++: #include<stdio.h>
int a[50][50],w,h;
void sima(int x,int y) {
  int i,j;
  a[y][x]=0;
  for(i=-1;i<=1;i++){
    for(j=-1;j<=1;j++){
      if(x+i>=0 && x+i<w && y+j>=0 && y+j<h){
	if(a[y+j][x+i]==1) sima(x+i,y+j); 
      }
    }
  }
}
int main(){

  int i,j,count;
  while(1){
    count=0;

  scanf("%d%d",&w,&h);
  if(w==0 && h==0) break;

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

  for(i=0;i<h;i++){
    for(j=0;j<w;j++){
      if(a[i][j]==1){
	count++;
	sima(j,i);
      }
    }
  }   
  printf("%d\n",count);
  }

    return 0;
  }

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

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

int mesh[50][50];
int n, m;

struct Point
{
	int x;
	int y;
};

int main()
{
	deque<Point> stack;
	while (true)
	{
		scanf("%d %d", &n, &m);
		if (n == 0 && m == 0) break;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				int a;
				scanf("%d", &a);
				mesh[i][j] = a;
			}
		}
		int count = 0;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if (mesh[i][j] == 1)
				{
					Point pt = {j, i};
					stack.push_front(pt);
					mesh[i][j] = 0;
					while (!stack.empty())
					{
						Point point = stack.front();
						stack.pop_front();
						for (int k = 0; k < 8; k++)
						{
							if (0 <= point.y + dy[k] && point.y + dy[k] < m
								&& 0 <= point.x + dx[k] && point.x + dx[k] < n
								&& mesh[point.y + dy[k]][point.x + dx[k]] == 1)
							{
								Point pt = {point.x + dx[k], point.y+dy[k]};
								stack.push_front(pt);
								mesh[point.y + dy[k]][point.x + dx[k]] = 0;
							}
						}
					}
					count++;
				}
			}
		}
		printf("%d\n", count);
	}
}