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<vector>
#include <unordered_map>  
#include <algorithm>
#include <functional>
#include <map>
#include <string>
#include <sstream>
#include <cmath>
#include <float.h>
using namespace std;

bool checked[50][50];
int island[50][50];
int count1 = 0;
int w, h;

void checkIsland(int x, int y) {
	if (x < 0 || x >= w || y >= h || y < 0) { return; }
	if (!island[x][y]) return;
	checked[x][y] = true;
	checkIsland(x + 1, y);
	checkIsland(x + 1, y + 1);
	checkIsland(x + 1, y - 1);
	checkIsland(x - 1, y + 1);
	checkIsland(x, y + 1);
}

int main() {
	cin >> w >> h;
	for (int i = 0; i < w; ++i) {
		for (int j = 0; j < h; ++j) {
			checked[i][j] = false;
			cin >> island[i][j];
		}
	}
	for (int i = 0; i < w; ++i) {
		for (int j = 0; j < h; ++j) {
			if (checked[i][j]) continue;
			if (island[i][j]) {
				count1++;
				checkIsland(i, j);
			}
			else {
				checked[i][j] = true;
			}
		}
	}
	cout << count1 << endl;
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int field[64][64];
int cnt;
int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int w, h;

void dfs(int x, int y);

int main()
{
	int x, y;

	while (true){
		cnt = 0;

		scanf("%d %d", &w, &h);
		if (w == 0){
			break;
		}

		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]){
					cnt++;
					dfs(j, i);
				}
			}
		}

		printf("%d\n", cnt);
	}

	return (0);
}

void dfs(int x, int y)
{
	field[y][x] = 0;

	for (int i = 0; i < 8; i++){
		int nx = x + dx[i];
		int ny = y + dy[i];

		if (field[ny][nx] && nx >= 0 && ny >= 0 && nx < w && ny < h){
			dfs(nx, ny);
		}
	}
}

C++: #include <iostream>
#define R 1
#define U 0
using namespace std;
int w,h;
int c[60][60];

void rec(int x,int y){
  if( x<0 || x>=h ) return;
  if( y<0 || y>=w ) return;
  if( c[x][y]==U )  return;

  c[x][y]=U;
  for( int i=-1;i<=1;i++ ){
    for( int j=-1;j<=1;j++ ){
      if( i==j && i==0 ) continue;
      rec( x+i,y+j );
    }
  }
}

int main(){
  while( cin>>w>>h && (w|h) ){
    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]==R ){
	  ans++;
	  rec( i,j );
	}
      }
    }
    cout << ans << endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> P3;
typedef pair<P ,P> PP;
constexpr ll MOD = ll(1e9) + 7;
constexpr int IINF = INT_MAX;
constexpr ll LLINF = LLONG_MAX;
constexpr int MAX_N = int(2e5) + 5;
constexpr double EPS = 1e-9;
constexpr int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i > 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), (v).end()

int h, w, field[55][55];

void dfs(int i, int j){
    field[i][j] = 0;

    for(int di=-1;di<=1;di++){
        for(int dj=-1;dj<=1;dj++){
            int ni = i + di, nj = j + dj;
            if(ni < 0 || ni >= h) continue;
            if(nj < 0 || nj >= w) continue;
            if(field[ni][nj] == 1){
                dfs(ni, nj);
            }
        }
    }
}

int main() {
    while(cin >> w >> h, w){
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin >> field[i][j];
            }
        }
        int ans = 0;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(field[i][j] == 1){
                    dfs(i,j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <set>
#include <algorithm>
using namespace std;
int group[55][55];
int parent(int n){
	int i=n/55,j=n%55;
	if(group[i][j]==-1)	return -1;
	return group[i][j]=(group[i][j]==n?group[i][j]:parent(group[i][j]));
}
void merge(int s,int t){
	int si=s/55,sj=s%55;
	s=parent(group[si][sj]);
	int ti=t/55,tj=t%55;
	t=parent(group[ti][tj]);
	group[t/55][t%55]=s;
}
bool isInside(int i,int j,int w,int h){
	if(i<0||h<=i)	return false;
	if(j<0||w<=j)	return false;
	return true;
}
const int dx[]={1,0,-1};
const int dy[]={1,0,-1};
int main(){
	while(true){
		int w,h;	cin>>w>>h;
		if(w==0)	break;
		for(int i=0;i<h;i++)	for(int j=0;j<w;j++)	group[i][j]=-1;
		for(int i=0;i<h;i++)	for(int j=0;j<w;j++){
			int c;	cin>>c;
			if(c==1)	group[i][j]=55*i+j;
		}
		for(int i=0;i<h;i++)	for(int j=0;j<w;j++){
			if(group[i][j]>=0){
				for(int x=0;x<3;x++)	for(int y=0;y<3;y++){
					int ni=i+dy[y],nj=j+dx[x];
					if(!isInside(ni,nj,w,h))	continue;
					if(group[ni][nj]>=0)	merge(55*i+j,55*ni+nj);
				}
			}
		}
		set<int> ret;
		for(int i=0;i<h;i++)	for(int j=0;j<w;j++)	ret.insert(parent(group[i][j]));
		ret.erase(-1);
		cout<<ret.size()<<endl;
	}
	return 0;
}