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;

using ll = long long;

class UnionFind {
public:
    vector<int> data;
    UnionFind(int size) : data(size, -1) { }
    bool connect(int x, int y) {
        x = root(x); y = root(y);
        if (x != y) {
            if (data[y] < data[x]) swap(x, y);
        data[x] += data[y]; data[y] = x;
        }
        return x != y;
    }
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x) {
        return -data[root(x)];
    }
};
int dx[] = {1, 1, 1, 0, -1, -1, -1, 0};
int dy[] = {1, 0, -1, -1, -1, 0, 1, 1};

int main()
{
    int h,w;
    while(cin >> h >> w){
        if(h == 0)return 0;
        map<pair<int,int>, int> se;
        int cnt = 0;
        for(int x = 0;x < w;x++){
            for(int y = 0;y < h;y++){
                int c;cin >> c;
                if(c){
                    se[make_pair(x, y)] = cnt;
                    cnt++;
                }
            }
        }
        UnionFind Uni(cnt);
        for(auto a : se){
            int x = a.first.first;
            int y = a.first.second;
            for(int i = 0;i < 8;i++){
                if(se.find(make_pair(x + dx[i], y + dy[i])) != se.end()){
                    Uni.connect(a.second, se[make_pair(x + dx[i], y + dy[i])]);
                }
            }
        }
        set<int> s;
        for(int i = 0;i < cnt;i++){
            s.insert(Uni.root(i));
        }
        std::cout<<s.size()<<endl;
    }
}

C++: #include<iostream>
#include<vector>
using namespace std;
int needx[]={-1,0,1,0,1,1,-1,-1};
int needy[]={0,1,0,-1,1,-1,1,-1};
//??¶?????¨???false????????????true???????????¢??°//
bool land(int x,int y,vector<vector<bool> >& cango){
    if(cango[x][y]){
        cango[x][y]=false;
        for(int i=0;i<8;i++){
            int nextx=x+needx[i];
            int nexty=y+needy[i];
            if(cango[nextx][nexty])
                bool flag=land(nextx,nexty,cango);
            }
    return true;
     }else{
return false;
    }
}
int main(void){
    int input;
	int w,h;
	while(1){
cin>>w>>h;
if(h==0)break;
    vector<vector<bool> > cango(h+2,vector<bool>(w+2,false));
    for(int i=1;i<=h;i++){
        for(int j=1;j<=w;j++){
            cin >>input;
            if(input==1){
cango[i][j]=true;
            }
        }
    }
  
    int t=0;
for(int i=1;i<=w;i++){
    for(int j=1;j<=h;j++){
        if(land(i,j,cango)){
            t++;
        }
    }
}
cout <<t<<endl;
	}
return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
bool d[51][51];
int a,b,s,_x,_y,dx[8]={0,-1,-1,-1,0,1,1,1},dy[8]={1,1,0,-1,-1,-1,0,1};
#define r(z,x) for(int z=0;z<x;z++)
void bfs(int x,int y){
    if(0>x||x>=b||0>y||y>=a)return;
    d[x][y]=0;
    r(i,8)if(d[_x=x+dx[i]][_y=y+dy[i]])bfs(_x,_y);
}
int main(){
    while(cin>>a>>b,a||b){
        s=0;
        r(i,b)r(j,a)cin>>d[i][j];
        r(i,b)r(j,a)if(d[i][j])bfs(i,j),s++;
        cout<<s<<endl;
    }
}

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

int w, h;
bool map[50][50];
int dx[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
int dy[8] = { 0, 1, 1, 1, 0, -1, -1, -1 };
int ans = 0;

void solve(int x, int y){
	map[x][y] = 0;

	for (int i = 0; i < 8; i++){
		int X = x + dx[i];
		int Y = y + dy[i];
		if (0 <= X&&X < w && 0 <= Y&&Y < h&&map[X][Y] != 0) solve(X, Y);
	}
}

int main(){
	while (cin >> w >> h, w + h != 0){
		ans = 0;
		for (int y = 0; y < h; y++){
			for (int x = 0; x < w; x++){
				cin >> map[x][y];
			}
		}

		for (int y = 0; y < h; y++){
			for (int x = 0; x < w; x++){
				if (map[x][y] == 1){
					solve(x, y);
					ans++;
				}
			}
		}
		cout << ans << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int H,W,k[100][100],n[100][100],color=1,i,j,l,m,o,r;
int main(){while(true){memset(n,0,sizeof(n));r=0;cin>>H>>W;if(H==0){break;}for(i=0;i<H;i++){for(j=0;j<W;j++){cin>>k[i][j];}}
for(i=0;i<H;i++){for(j=0;j<W;j++){if(r==1){color++;}r=0;if(k[i][j]==1 && n[i][j]==0){n[i][j]=color;for(l=0;l<20;l++){
	for(m=0;m<H;m++){for(o=0;o<W;o++){if(n[m][o]==color){
		for(int p=-1;p<=1;p++){for(int q=-1;q<=1;q++){if(m-p>=0 && m-p<H){if(o-q>=0 && o-q<W){n[m][o]=color;r=1;}}}}
	}}}
}}}}
cout<<color<<endl;}
}