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 <algorithm>
#include <vector>
#include <string>

using namespace std;

int m[55][55]={};

void fill(int x,int y){
  m[x][y]=0;
  for(int i=-1;i<2;++i){
    for(int j=-1;j<2;++j){
      if(m[x+i][y+j]==1) fill(x+i,y+j);
    }
  }
}

int main(){
  int w,h;
  while(1){
    cin >> w >> h;
    if(w == 0 && h ==0) break;
    
    for(int i=1;i<=h;++i)
      for(int j=1;j<=w;++j)
	cin >> m[i][j];
    int cnt=0;
    for(int i=1;i<=h;++i)
      for(int j=1;j<=w;++j)
	if(m[i][j]==1){
	  fill(i,j);
	  cnt ++;
	}

    cout << cnt << endl;
  }
  return 0;
}

C++: #include<stdio.h>
#include<iostream>

int field[51][51];

void land(int x,int y){
	field[x][y]=0;

	for(int xx=-1;xx<=1;xx++){
		for(int yy=-1;yy<=1;yy++){
			if(field[x+xx][y+yy]==1) land(x+xx,y+yy);
		}
	}
	return;
}

int main(void){

	int w,h,kazu=0;

	scanf("%d %d",&w,&h);

	if(w==0||h==0) return 0;

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

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){

			if(field[i][j]==1){
				land(i,j);
				kazu++;
			}

		}
	}

	printf("%d",kazu);
	return 0;
}

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

using namespace std;

int solve(vector<vector<int>> area) {
	// 隣接しているマス(8近傍)を左上から時計回り
	const vector<int> nh = {-1, -1, -1, 0, 1, 1, 1, 0};
	const vector<int> nw = {-1, 0, 1, 1, 1, 0, -1, -1};

	const int h_size = area.size();
	const int w_size = area[0].size();

	int ret = 0;
	vector<vector<int>> visited(h_size, vector<int>(w_size, 0));

	for (int h = 0; h < h_size; ++h) {
		for (int w = 0; w < w_size; ++w) {
			if (area[h][w] == 0) continue;		// 海
			if (visited[h][w] == 1) continue;	// すでに見た陸

			++ret;		// まだ見ていない陸をカウントする
			visited[h][w] = 1;
			queue<pair<int,int>> que;		// pair<int, int> で ２つの int 型を一つの型にまとめられる (pair<int, double> とかもできる)
			que.push(make_pair(h, w));		// push() で que に要素を追加する

			// 幅優先探索はここを指します。 queue を while で回すのが一般的です
			while(que.size()) {
				pair<int, int> now = que.front();	// front() で que に先に入れた要素を取得する
				que.pop();							// pop() で front() で取れる要素を削除する

				for (int i = 0; i < 8; ++i) {
					// 今見ている場所の周囲の座標を計算する
					int next_h = now.first + nh[i];		// .first で pair の1つ目の要素を得る
					int next_w = now.second + nw[i];	// .second で pair の2つ目のの要素を得る

					if (next_h < 0 || next_w < 0 ||
						next_h >= h_size || next_w >= w_size) continue;		// エリアは (0 <= h < h_size), (0 <= w < w_size) の範囲
					if (area[next_h][ next_w] == 0) continue;
					if (visited[next_h][next_w] == 1) continue;

					visited[next_h][next_w] = 1;
					que.push(make_pair(next_h, next_w));
				}
			}
		}
	}

	return ret;
}

int main() {
	int w, h;
	vector<vector<vector<int>>> area;

	// 終了条件は w == 0 && h == 0
	while (cin >> w >> h, !(w == 0 && h == 0)) {
		area.push_back(vector<vector<int>>(h, vector<int>(w)));		// [h][w] になる二次元配列
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j) {
				cin >> area.back()[i][j];		// back() は vector の一番後ろの要素を得られます。
			}
		}
	}

	for (auto check_area : area) {
		cout << solve(check_area) << endl;
	}

	return 0;
}

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

using ll=long long;
#define int ll

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

int read(){
	int i;
	scanf("%lld",&i);
	return i;
}

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

void isLand(int i,int j){
	land[i][j] = 0;
	REP(k,8){
		int newx=i+dx[k];
		int newy=j+dy[k];
		if(land[newx][newy]==1)isLand(newx,newy);
	}
}

signed main(){
	int w,h;
	while( w=read(),h=read(),w||h){
		cnt=0;
		
		REP(i,100){
			REP(j,100){
				land[i][j]=0;
			}
		}
		FOR(i,1,w+1){
			FOR(j,1,h+1){
				land[i][j]=read();
			}
		}
		
		FOR(i,1,w+1){
			FOR(j,1,h+1){
				if(land[i][j]==1){
					cnt++;
					isLand(i,j);
				}
			}
		}
		
		cout << cnt << endl;
	}
	
}

C++: #include <cstdio>
#include <cctype>
#include <utility>
#include <vector>
using namespace std;

#define gcu getchar_unlocked
#define ii inline int in
ii(int c) {
	int n = 0;
	bool minus = false; if (c == '-') minus = true, c = gcu();
	do {n = 10 * n + (c - '0'), c = gcu();} while (c >= '0');
	//return n; }
	return minus ? -n : n; }
ii() {return in(gcu());}
inline bool scan(int &n) {int c=gcu();return c==EOF?false:(n=in(c),true);}
//void scan(string &s) {for (char c; !isspace(c = gcu()); s += c);}
#undef ii
#define pcu putchar_unlocked
template <typename T>
void out(T n) {
	static char buf[20];
	char *p = buf;
	if(n < 0) pcu('-'), n *= -1;
	if (!n) *p++ = '0';
	else while (n) *p++ = n % 10 + '0', n /= 10;
	while (p != buf) pcu(*--p); }
void out(const char *s){while(*s)pcu(*s++);}
void out(char c){pcu(c);}
//void out(string &s){for (char c: s) pcu(c);}
template <typename head, typename... tail>
void out(head&& h, tail&&... t){out(h);out(move(t)...);}
template <typename T>
//void out(vector<T> &v){for(T &x:v)out(' '),out(x);out('\n');}
void out(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);out('\n');}
#undef svo

typedef vector<vector<int>> map;

bool walk(int x, int y, map &m) {
	static const int
		dx[] = {1, 1, 0, -1, -1, -1, 0, 1},
		dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
	if (x < 0 || x >= (int) m[0].size() ||
		y < 0 || y >= (int) m.size() ||
		m[y][x] == 0)
		return false;
	m[y][x] = 0;
	int i = 0;
	for (; i < 8; i++)
		walk(x + dx[i], y + dy[i], m);
	return true;
}

int main() {
	int W, H;
	while(scan(W) && scan(H) && (W || H)) {
		map m(H, vector<int>(W));
		for (auto &v: m)
			for (int &i :v)
				i = in();
		int c = 0;
		for (int i = 0; i < H; i++)
			for (int j = 0; j < W; j++)
				if (walk(j, i, m))
					c++;
		//for (auto v : m)
		//	out(v);
		//out('\n');
		out(c, '\n');
	}
}

C++: #include <iostream>
#include <iomanip>
#include <sstream>
#include <stdio.h>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <math.h>
#include <utility>
#include <string>
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <functional>

using namespace std;

#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define ll long long
//#define scanf scanf_s

int w, h;
int isl[55][55];

int di[8] = { -1, -1, -1, 0, 0, 1, 1, 1 }; int dj[8] = { -1, 0, 1, -1, 1, -1, 0, 1 };

void dfs(int i, int j)
{
	if (isl[i][j] == 1)
	{
		isl[i][j] = 0;
		REP(k, 8) dfs(i + di[k], j + dj[k]);
	}
}

int main()
{
	while (cin >> w >> h, w)
	{
		REP(i, h)REP(j, w) cin >> isl[i][j];
		int cnt = 0;
		REP(i, h)REP(j, w)
		{
			if (isl[i][j] == 1)
			{
				cnt++;
				dfs(i, j);
			}
		}
		cout << cnt << endl;
	}
	return 0;
}