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++: #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);
	}
}