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>
using namespace std;

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

void rec(int y,int x){
  if(y<0 || x<0 || y>=h || x>=w)return;
  if(c[y][x]==0)return;
  c[y][x] = 0;
 
  for(int i=0;i<8;i++){
    rec(y+dy[i],x+dx[i]);
  }
}
 
int main(){
  while(cin >> w >> h,w){
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++)cin >> c[i][j];
    }

    int cnt = 0;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(c[i][j]){
	  rec(i,j); cnt++;
	}
      }
    }

    cout << cnt << endl;
  }
}

C++: #include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod=1000000007;
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;
int h,w,a[50][50];
int dy[8]={-1,-1,-1,0,0,1,1,1},dx[8]={-1,0,1,-1,1,-1,0,1};
ll uni[2500];
ll root(ll u){
    if(uni[u]<0)return u;
    return uni[u]=root(uni[u]);
}

void connect(int a,int b){
    a=root(a);
    b=root(b);
    if(a==b)return;
    if(uni[a]>uni[b]){
        a^=b;
        b^=a;
        a^=b;
    }
    uni[a]+=uni[b];
    uni[b]=a;
    return;
}
ll size(ll u){
    return -uni[root(u)];
}

int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    vector<int> v;
    while(1){
    	cin>>w>>h;
    	if(!(h||w))break;
    	for(int i=0;i<h;i++){
    		for(int j=0;j<w;j++){
    			cin>>a[i][j];
    			uni[j*h+i]=(a[i][j])?-1:0;
    		}
    	}
    	for(int i=0;i<h;i++){
    		for(int j=0;j<w;j++){
    			if(a[i][j]){
    				for(int k=0;k<8;k++){
    					int x=dx[k]+j;
    					int y=dy[k]+i;
    					if(x<0||x>=w||y<0||y>=h)continue;
    					if(a[y][x]){
    						connect(j*h+i,x*h+y);
    					}
    				}
    			}
    		}
    	}
    	int ans=0;
    	for(int i=0;i<h;i++){
    		for(int j=0;j<w;j++){
    			if(a[i][j]&&uni[j*h+i]<0)ans++;
    		}
    	}
    	v.pb(ans);
    }
    for(auto& i:v)cout << i<<endl;
    return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main(){
	int w,h;
	while(1){
		cin>>w>>h;if(w==0&&h==0)break;
	int map[55][55]={};
	int W=0;
	for(int i=0;i<h;i++)
		for(int j=0;j<w;j++)
		{int x;cin>>x;if(x==1){W++;map[i+1][j+1]=W;}}

	int MAP[55][55]={};int c=0;
	for(int i=0;i<h+1;i++)
		for(int j=0;j<w+1;j++)
		{if(map[i][j]>0){c=map[i][j];

	int G[10]={map[i-1][j-1],map[i-1][j],map[i-1][j+1],map[i][j-1],map[i][j],map[i][j+1],map[i+1][j-1],map[i+1][j],map[i+1][j+1]};
			if(map[i-1][j-1]>0&&c>map[i-1][j-1])c=map[i-1][j-1];
			if(map[i-1][j]>0&&c>map[i-1][j])c=map[i-1][j];
			if(map[i-1][j+1]>0&&c>map[i-1][j+1])c=map[i-1][j+1];
			if(map[i][j-1]>0&&c>map[i][j-1])c=map[i][j-1];
			if(map[i][j+1]>0&&c>map[i][j+1])c=map[i][j+1];
			if(map[i+1][j-1]>0&&c>map[i+1][j-1])c=map[i+1][j-1];
			if(map[i+1][j]>0&&c>map[i+1][j])c=map[i+1][j];
			if(map[i+1][j+1]>0&&c>map[i+1][j+1])c=map[i+1][j+1];
			for(int I=0;I<53;I++)
				for(int J=0;J<53;J++)
					for(int H=0;H<9;H++)
					{if(map[I][J]==G[H]&&map[I][J]>0)map[I][J]=c;}
	}
		}
	bool C[2502]={};
	for(int i=0;i<h+1;i++){//cout<<endl;
		for(int j=0;j<w+1;j++){//cout<<map[i][j]<<" ";
			if(map[i+1][j+1]>0)C[map[i+1][j+1]]=true;
		}
	}
	int R=0;
	for(int i=0;i<2501;i++)if(C[i]==true)R++;
			cout<<R<<endl;

	}
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD=1e9+7;
const ll INF=1e18;
const double pi=acos(-1);
const double eps=1e-10;

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

int main(){
    while(1){
        int w, h; cin>>w>>h;
        if(w == 0) return 0;

        vector<vector<int>> c(h, vector<int>(w));
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                cin>>c[i][j];
            }
        }

        int ans=0;
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                if(c[i][j]){
                    ans++;
                    c[i][j] = 0;

                    queue<P> que;
                    que.push(P(i, j));
                    while(que.size()){
                        int cy, cx;
                        tie(cy, cx) = que.front();
                        que.pop();

                        for(int k=0; k<8; k++){
                            int ny = cy + dy[k];
                            int nx = cx + dx[k];

                            if(0 <= ny && ny < h && 0 <= nx && nx < w){
                                if(c[ny][nx]){
                                    c[ny][nx] = 0;
                                    que.push(P(ny, nx));
                                }
                            }
                        }
                    }
                }
            }
        }

        cout << ans << endl;
    }
}