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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>


using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; }
#endif

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define clear(arr, val) memset(arr, val, sizeof(arr))

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T& a, const T& b) { a = max(a, b); }
template <class T> void min_swap(T& a, const T& b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double EPS = 1e-8;
const double PI = acos(-1.0);
const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };


bool c[64][64];
void destroy(int x, int y)
{
	if (!c[y][x])
		return;
	c[y][x] = false;
	for (int dy = -1; dy <= 1; ++dy)
		for (int dx = -1; dx <= 1; ++dx)
			destroy(x + dx, y + dy);
}
int main()
{
	int w, h;
	while (cin >> w >> h, w)
	{
		clear(c, 0);
		rep (y, h) rep (x, w)
			cin >> c[y + 1][x + 1];
		int res = 0;
		for (int y = 1; y <= h; ++y)
			for (int x = 1; x <= w; ++x)
				if (c[y][x])
					++res, destroy(x, y);
		cout << res << endl;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
int a[500][500];
void calc(int i,int j){
  if(a[i][j]==1){
    a[i][j]=2;
    calc(i+1,j);
    calc(i,j+1);
    calc(i+1,j+1);
    calc(i+1,j-1);
    calc(i-1,j);
    calc(i,j-1);
    calc(i-1,j-1);
    calc(i-1,j+1);
  }
  else return;
}

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

C++: #include <bits/stdc++.h>

using namespace std;

using ll = long long;

int w, h;
vector< vector<int> > c, island;

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

void dfs(int x, int y, int id) {

    island[y][x] = id;

    for(int i = 0; i < 8; i++) {

        int nx = x + dx[i];
        int ny = y + dy[i];

        if(nx < 0 || nx >= w || ny < 0 || ny >= h)
            continue;

        if(c[ny][nx] == 1 && island[ny][nx] != id)
            dfs(nx, ny, id);

    }

}

int main(){

    cin.tie(0);
    ios::sync_with_stdio(false);

    int ans;

    while(cin >> w >> h && w) {

        c.assign(h, vector<int>(w));
        island.assign(h, vector<int>(w, -1));
        ans = 0;

        for(int y = 0; y < h; y++)
            for(int x = 0; x < w; x++)
                cin >> c[y][x];

        for(int x = 0; x < w; x++)
            for(int y = 0; y < h; y++)
                if(c[y][x] == 1 && island[y][x] == -1) {
                    dfs(x, y, ans);
                    ans++;
                }

        cout << ans << endl;

    }
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

bool done[60][60];
int islands[60][60];
void dfs(int vx, int vy, int w, int h)
{
    done[vx][vy] = true;
    int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1,};
    int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1,};
    rep(i, 8)
    {
        if(islands[vx + dx[i]][vy + dy[i]] == 0) continue;
        if(vx + dx[i] < 0 || vx + dx[i] >= h) continue;
        if(vy + dy[i] < 0 || vy + dy[i] >= w) continue;
        if(done[vx + dx[i]][vy + dy[i]]) continue;
        dfs(vx + dx[i], vy + dy[i], w, h);
    }
    return;
}

int main(void)
{
    while(1)
    {
        int w, h;
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        rep(i, h)rep(j, w) done[i][j] = false;
        rep(i, h)rep(j, w) cin >> islands[i][j];
        int ans = 0;
        rep(i, h)rep(j, w)
        {
            if(islands[i][j] == 0) continue;
            if(done[i][j]) continue;
            ans++;
            dfs(i, j, w, h);
        }
        cout << ans << endl;
    }
    return 0;
}

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

int a[100][100]={};

void del(int x,int y){
  a[x][y]=0;
  for(int i=-1;i<=1;i++){
    for(int t=-1;t<=1;t++){
      if(a[x+i][y+t]==1){del(x+i,y+t);}
    }}
}

int main(){

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

  for(int i=1;i<=h;i++){
    for(int t=1;t<=w;t++){
      cin>>a[i][t];
    }}

  int count=0;
  for(int i=1;i<=h;i++){
    for(int t=1;t<=w;t++){
      if(a[i][t]==1){del(i,t); count++;}
    }}
  
  cout<<count<<endl;
  }
  
  return 0;
}

C++: #include <iostream>
using namespace std;
int p[53][53];

class point{
public:
	int x, y;
	point(int newx, int newy){
		x = newx;
		y = newy;
	}
};

void findisland(int i, int j){
	p[i][j] = 2;
	if (p[i - 1][j - 1] == 1)
		findisland(i - 1, j - 1);
	if (p[i][j - 1] == 1)
		findisland(i, j - 1);
	if (p[i + 1][j - 1] == 1)
		findisland(i + 1, j - 1);
	if (p[i - 1][j] == 1)
		findisland(i - 1, j);
	if (p[i + 1][j] == 1)
		findisland(i + 1, j);
	if (p[i - 1][j + 1] == 1)
		findisland(i - 1, j + 1);
	if (p[i][j + 1] == 1)
		findisland(i, j + 1);
	if (p[i + 1][j + 1] == 1)
		findisland(i + 1, j + 1);
}


int main()
{
	int w, h, count;
	while (cin >> w >> h, (w | h)){

		//初期化
		for (int i = 0; i < 53; i++){
			for (int j = 0; j < 53; j++){
				p[i][j] = 0;
			}
		}
		count = 0;

		//入力
		for (int i = 1; i < h + 1; i++){
			for (int j = 1; j < w + 1; j++){
				cin >> p[i][j];
			}
		}

		//探索
		for (int i = 1; i < h + 1; i++){
			for (int j = 1; j < w + 1; j++){
				if (p[i][j] == 1){
					findisland(i, j);
					count++;
				}
			}
		}
		cout << count << endl;



	}
	return 0;
}

C++: #include <iostream>

using namespace std;

int W, H;
int field[50][50];

void input(void){
	cin >> W;
	cin >> H;

	for(int i = 0;i < H;i++){
		for(int j = 0;j < W;j++){
			cin >> field[i][j];
		}
	}
}

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, ny = y + dy;
			if(0 <= nx && nx <= W && 0 <= ny && ny <= H && field[nx][ny] == 1) dfs(nx, ny);
		}
	}

	return ;
}

int main(){
	while(1){
		int res = 0;
		input();
		if(W == 0 && H == 0) break;

		for(int i = 0;i < H;i++){
			for(int j = 0;j < W;j++){
				if(field[i][j] == 1){
					dfs(i, j);
					res++;
				}
			}
		}

		cout << res << endl;
	}

	return 0;
}