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 <iostream>
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <math.h>
using namespace std;

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

int w,h,cnt,l[50][50];

void f(int x,int y){
	l[y][x]=0;
	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&&l[ny][nx]==1)f(nx,ny);
	}
}

int main(){
	while(cin>>w>>h,w||h){
		cnt=0;
		for(int i=0;i<w*h;i++)cin>>l[i/w][i%w];
		for(int i=0;i<w*h;i++)if(l[i/w][i%w]==1){cnt++;f(i%w,i/w);}
		cout<<cnt<<endl;
	}
}

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

using namespace std;

int d[8][2] = {
	{-1, -1},
	{0, -1},
	{1, -1},
	{-1, 0},
	{1, 0},
	{-1, 1},
	{0, 1},
	{1, 1}
};

void func(int **a, int s, int t, int i, int j)
{
	if(a[i][j] == 0){
		return ;
	}
	a[i][j] = 0;
	
	for(int k = 0; k < 8; k++){
		if((i+d[k][0] >= 0 && i+d[k][0] < t) && (j+d[k][1] >= 0 && j+d[k][1] < s)){
			func(a, s, t, i+d[k][0], j+d[k][1]);
		}
	}
	
	return ;
}

int main(int argc, char **argv)
{
	int s, t, **a, c, t2, s2;
	
	while(1){
		cin >> s >> t;
		if(s == 0 && t == 0){
			break;
		} else if(s == 1 || t == 1){
			t2 = t+1;
			s2 = s+1;
		} else {
			t2 = t;
			s2 = s;
		}
		a = (int **)calloc(t2, sizeof(int));
		for(int i = 0; i < t; i++){
			a[i] = (int *)calloc(s2, sizeof(int));
			for(int j = 0; j < s; j++){
				cin >> c;
				a[i][j] = c;
			}
		}
		c = 0;
		
		for(int i = 0; i < t; i++){
			for(int j = 0; j < s; j++){
				if(a[i][j] == 1){
					c++;
					func(a, s, t, i, j);
				}
			}
		}
		cout << c << endl;
		
		for(int i = 0; i < s2; i++){
			free(a[i]);
		}
		free(a);
	}
	
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cctype>
#include<utility>
#include<string>
#include<cmath>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;


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

void dfs(vector<vector<int> > &map,int x,int y){
    /*for(int i = 0;i < h; i++){
        for(int j = 0; j < w; j++){
            cout << map[i][j] << " " ;
        }
        cout << endl;
    }*/
    //今いるところを-100に置き換える
    map[x][y] = -100;
    //移動方向をループ
    for(int dx = -1;dx <= 1;dx++){
        for(int dy = -1;dy <= 1;dy++){
            //x方向にdx,ｙ方向にdy移動した場所を(nx,ny)とする
            int nx = x + dx, ny = y + dy;
            if(0 <= nx && nx < h && 0 <= ny && ny < w && map[nx][ny] == 1){
                dfs(map,nx,ny);
            }
        }
    }
    return;
}

int solve(vector<vector<int> > &map,int h,int w){

    /*for(int i = 0;i < h; i++){
        for(int j = 0; j < w; j++){
            cout << map[i][j] << " " ;
        }
        cout << endl;
    }*/

    int ans = 0;
    for(int i = 0;i < h;i++){
        for(int j = 0;j < w;j++){
            if(map[i][j] == 1){
                //Wが残っているならばその場所からdfsを始める
                dfs(map,i,j);
                //ここで1つの水たまりが見つかったことになるので水たまりの個数を１つ足す
                ans++;
            }
        }
    }
    return ans;
}

int main(){

    
    

    while(cin >> w >> h){

        if(w == 0 && h == 0){
            break;
        }
        int ans = 0;
        vector<vector<int> > map(h,vector<int>(w));//周波数ベクトル列データ用の2次元配列

        for(int i = 0;i < h; i++){
            for(int j = 0; j < w; j++){
                cin >> map[i][j];
                //cout << map[i][j] << " " ;
            }
            //cout << endl;
        }
        //cout << endl;

        ans = solve(map,h,w);

        cout << ans << endl;
    }

   


    

    return 0;
}

C++: #include<iostream>
#include<cstring>
using namespace std;
bool used[55][55];
int n,m;

void check(int x,int y){
	if(x<0 || n<=x || y<0 || m<=y)return;
	if(used[x][y])return;
	used[x][y]=true;
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			check(x+i,y+j);
		}
	}
}

int main(){
	int i,j,k;
	int a,b,c;
	while(cin>>m>>n){
		if(!n)break;
		memset(used,false,sizeof(used));
		for(i=0;i<n;i++){
			for(j=0;j<m;j++){
				cin>>a;
				if(a==0)used[i][j]=true;
			}
		}
		a=0;
		for(i=0;i<n;i++){
			for(j=0;j<m;j++){
				if(!used[i][j])check(i,j),a++;
			}
		}
		cout<<a<<endl;
	}
}