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>
using namespace std;
#define rep(i,n) for(int i = 0; i < n;i++)
int w, h;
int map[50][50];
int dx[8] = { -1,0,1,-1,1,-1,0,1 }, dy[8] = { -1,-1,-1,0,0,1,1,1 };
void dfs(int y, int x) {
	map[y][x] = 0;
	for (int i = 0; i < 8;i++) {
		int nx = x + dx[i];
		int ny = y + dy[i];
		if (nx < 0 || ny >= h || ny < 0 || nx >= w) continue; 
		if(map[ny][nx] == 1) {
			dfs(ny, nx);
		}
	}
}
void dfss() {
	int count = 0;
	rep(i, h) {
		rep(j, w) {
			if (map[i][j] == 1) {
				count++; 
				dfs(i, j);
			}
		}
	}
	cout << count << endl;
}
int main() {
	for (;;) {
		cin >> w >> h;
		if (w == 0 && h == 0)
			break;
		rep(i, h) {
			rep(j, w) {
				cin >> map[i][j];
			}
		}
		dfss();
	}
}

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