Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)
var route [][]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func dfs(h, w int) {
	if route[h][w] == 0 {
		return
	}

	route[h][w] = 0
	for _, x := range []int{-1, 0, 1} {
		for _, y := range []int{-1, 0, 1} {
			if route[h+x][w+y] == 0 {
				continue
			}
			dfs(h+x, w+y)
		}
	}
}

func main() {
	writer := bufio.NewWriter(os.Stdout)
	for {
		w, h := scanInt(), scanInt()
		if w == 0 && h == 0 {
			break
		}

		route = make([][]int, h+2)
		route[0] = make([]int, w+2)
		for i := 1; i <= h; i++ {
			route[i] = make([]int, w+2)
			for j := 1; j <= w; j++ {
				route[i][j] = scanInt()
			}
		}
		route[h+1] = make([]int, w+2)

		total := 0
		for i := 1; i <= h; i++ {
			for j := 1; j <= w; j++ {
				if route[i][j] == 1 {
					total++
					dfs(i, j)
				}
			}
		}
		fmt.Fprintln(writer, total)
	}
	writer.Flush()
}

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);
	}
}

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

int H,W;
vector<vector<int>> field;

void dfs(int h, int w){
    field[h][w] = 0;

    for (int dh = -1; dh <= 1; ++dh) {
        for (int dw = -1; dw <= 1; ++dw) {
            int nh = h + dh, nw = w + dw;

            if(nh < 0 || nh  >= H || nw < 0 || nw >= W) continue;
            if(field[nh][nw] == 0) continue;

            dfs(nh, nw);
        }
    }
}

int main() {
    while (cin >> W >> H){
        if(H == 0 and W == 0) break;

        field.assign(H,vector<int>(W, 0));
        for (int h = 0; h < H; ++h) {
            for (int w = 0; w < W; ++w) {
                cin >> field[h][w];
            }
        }

        int count = 0;
        for (int h = 0; h < H; ++h) {
            for (int w = 0; w < W; ++w) {
                if(field[h][w] == 0) continue;
                dfs(h, w);
                count++;
            }
        }
        cout << count << endl;
    }
}