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 <bits/stdc++.h>
using namespace std;
int dx[8] = {1,0,-1,0,1,-1,-1,1};//右、上、左、下、右上、左上、左下、右下のつもり
int dy[8] = {0,1,0,-1,1,1,-1,-1};//実際は色々反転している。どうせ4か8方向調べるから関係ないとしている。
bool seen[60][60]={true};
int h,w;
void dfs(vector<vector<int> > &G,int i,int j){
    if(G[i][j]) seen[i][j] = true;
    for(int k=0;k<8;k++){
	int di = i + dx[k];
	int dj = j + dy[k];
	if(di < 0 || h <= di || dj < 0 || w <= dj)continue;
	
	if(seen[di][dj]==false) dfs(G,di,dj);
    }
}
int main(void){
    while(cin >> w >> h){
	if(w == 0 && h== 0) break;
	for(int i=0;i<h;i++)for(int j=0;j<w;j++) seen[i][j] = true;
	vector<vector<int> > G(h,vector<int>(w));
	for(int i=0;i<h;i++){
	    for(int j=0;j<w;j++){
		cin >> G[i][j];
		if(G[i][j]) seen[i][j] = false;
	    }
	}
	int t =0;
	for(int i=0;i<h;i++){
	    for(int j=0;j<w;j++){
		if(seen[i][j]) continue;
		dfs(G,i,j);
		t++;
	    }
	}
	cout << t << endl;
    }

    return 0;
}

C++: #include <iostream>
using namespace std;

int x,y;
int field[50][50];

void dfs(int w, int h){
	field[w][h]=0;
	for(int dy=-1; dy<=1; dy++){
		for(int dx=-1; dx<=1; dx++){
			int nx = w + dx;
			int ny = h + dy;
			if(nx>=0 && nx<x && ny>=0 && ny<y && field[nx][ny]==1)
				dfs(nx,ny);
		}
	}
	return;
}


int main(){
	while(1){
		int result = 0;
		cin >> x >> y;
		if(x == 0 && y == 0)
			break;
		for(int j=0; j<y; j++){
			for(int i=0; i<x; i++){
				cin >> field[i][j];
			}
		}
		for(int j=0; j<y; j++){
			for(int i=0; i<x; i++){
				if(field[i][j]==1){
					result++;
					dfs(i,j);
				}
			}
		}
		cout << result << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
struct edge {
    int to;     // 辺の行き先
    int weight; // 辺の重み
    edge(int t, int w) : to(t), weight(w) { }
};
using Graph = vector<vector<int>>;
using dou =long double;
string yes="yes";
string Yes="Yes";
string YES="YES";
string no="no";
string No="No";
string NO="NO";

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
#define rep(i, n)         for(int i = 0; i < (int)(n); i++)
#define brep(n)           for(int bit=0;bit<(1<<n);bit++)
#define erep(i,container) for (auto i : container)
#define irep(i, n)        for(int i = n-1; i >= (int)0; i--)
#define rrep(i,m,n) for(ll i = m; i < (ll)(n); i++)
#define reprep(i,j,h,w) rep(i,h)rep(j,w)
#define all(x) (x).begin(),(x).end()
#define VEC(type,name,n) std::vector<type> name(n);rep(i,n)std::cin >> name[i];
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define itn int
#define mp make_pair
#define sum(a) accumulate(all(a),0ll)
#define keta fixed<<setprecision
#define vvector(name,typ,m,n,a)vector<vector<typ> > name(m,vector<typ> (n,a))
#define vvvector(name,t,l,m,n,a) vector<vector<vector<t> > > name(l, vector<vector<t> >(m, vector<t>(n,a)));
#define vvvvector(name,t,k,l,m,n,a) vector<vector<vector<vector<t> > > > name(k,vector<vector<vector<t> > >(l, vector<vector<t> >(m, vector<t>(n,a)) ));
#define case std::cout << "Case #" <<qqq+1<<": "
#define res resize
#define as assign
#define ffor for(;;)
#define ppri(a,b) std::cout << a<<" "<<b << std::endl
#define pppri(a,b,c) std::cout << a<<" "<<b <<" "<< c<<std::endl
#define aall(x,n) (x).begin(),(x).begin()+(n)
 
typedef long long ll;
typedef pair<int,int> P;
//typedef long long T;
const int INF = 2000000000;
const ll INF64 = 922337203685477580ll;
const ll MOD = 1000000007ll;
int w,h;
void dfs(std::vector<std::vector<int> > &a,int y,int x){
    a[y][x]=0;
    rrep(i,max(0,y-1),min(y+2,h)){
        rrep(j,max(0,x-1),min(x+2,w)){
            if(y==i&&x==j)continue;
            if(a[i][j]==0)continue;
            dfs(a,i,j);
        }
    }
}
int main(){
    for(;;){
        
        std::cin >> w>>h;
        if(w==0)exit(0);
        vvector(a,int,h,w,0);
        reprep(i,j,h,w)std::cin >> a[i][j];
        int ans=0;
        reprep(i,j,h,w){
            if(a[i][j]){
                ans++;
                dfs(a,i,j);
            }
        }
        std::cout << ans << std::endl;
    }
    
    
}

C++: #include<iostream>

using namespace std;

int h, w, map[51][51];

const int vx[] = {1,0,-1,0}, vy[] = {0,1,0,-1};

void island(int _w, int _h) {
	map[_w][_h] = 2;
	for (int i = 0;i < 4;i++) {
		if (_w + vy[i] >= 1 && _w + vy[i] <= w&&_h + vx[i] >= 1 && _h + vx[i] <= h&&map[_w+vy[i]][_h+vx[i]] == 1) {
			island(_w + vy[i], _h + vx[i]);
		}
	}
}

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