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 <string>
#include <vector>

using namespace std;

void del(int x, int y);

vector< vector<int> > arr;
int w, h;

int main(){
	
	while(cin >> w >> h, w | h){
		int ans = 0;
		arr.resize(h);
		for(int i = 0; i < h; i++){
			arr[i].resize(w);
		}
		
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin >> arr[i][j];
			}
		}
		
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				if(arr[i][j] == 1){
					del(i, j);
					ans++;
				}
			}
		}
				
		
		cout << ans << endl;
		arr.clear();
	}
	
	return 0;
	
}

void del(int x, int y){
	arr[x][y] = 0;	
	
	if(x + 1 < h && arr[x + 1][y] == 1){
		del(x + 1, y);
	}
	if(y + 1 < w && arr[x][y + 1] == 1){
		del(x, y + 1);
	}
	if(x - 1 > -1 && arr[x - 1][y] == 1){
		del(x - 1, y);
	}
	if(y - 1 > -1 && arr[x][y - 1] == 1){
		del(x, y - 1);
	}
	if(x + 1 < h && y + 1 < w && arr[x + 1][y + 1] == 1){
		del(x + 1, y + 1);
	}
	if(x - 1 > -1 && y + 1 < w && arr[x - 1][y + 1] == 1){
		del(x - 1, y + 1);
	}
	if(x - 1 > -1 && y - 1 > -1 && arr[x - 1][y - 1] == 1){
		del(x - 1, y - 1);
	}
	if(x + 1 < h && y - 1 > -1 && arr[x + 1][y - 1] == 1){
		del(x + 1, y - 1);
	}
	
	return;
}

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

int field[52][52];
int vw[8] = {-1,0,1,-1,1,-1,0,1};
int vh[8] = {-1,-1,-1,0,0,1,1,1};
void search(int i,int j)
{
	field[i][j] = 0;
	for(int k=0;k<8;k++){
		if(field[i+vw[k]][j+vh[k]] == 1)
			search(i+vw[k],j+vh[k]);
	}
}

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

		int count = 0;
		for(int i=1;i<=h;i++){
			for(int j=1;j<=w;j++){
				if(field[i][j] == 1){
					count++;
					search(i,j);
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}

C++: #include<cstdio>
using namespace std;
void func(int,int);
int a,b;
int sima[51][51];
int cnt;
int main(void)
{
	int i,j;
	scanf("%d",&a);
	scanf("%d",&b);
	for(i=0;i<a;i++){
		for(j=0;j<b;j++){
			scanf("%d",&sima[i][j]);
		}
	}
	for(i=0;i<a;i++){
		for(j=0;j<b;j++){
			if(sima[i][j]==1){
				func(i,j);
				cnt++;
			}
		}
	}
	printf("%d\n",cnt);
	return 0;
}
void func(int y,int x)
{
	if(y-1>=0){
		if(sima[y-1][x]==1){
			func(y-1,x);
		}
	}
	if(x-1>=0){
		if(sima[y][x-1]==1){
			func(y,x-1);
		}
	}
	if(y+1<=a){
		if(sima[y+1][x]==1){
			func(y+1,x);
		}
	}
	if(x+1<=b){
		if(sima[y][x+1]==1){
			func(y,x+1);
		}
	}
	if(y-1>=0 && x+1<=b){
		if(sima[y-1][x+1]==1){
			func(y-1,x+1);
		}
	}
	if(y-1>=0 && x-1>=0){
		if(sima[y-1][x-1]==1){
			func(y-1,x-1);
		}
	}
	if(y+1<=a && x+1<=b){
		if(sima[y+1][x+1]){
			func(y+1,x+1);
		}
	}
	if(y+1<=a && x-1>=0){
		if(sima[y+1][x-1]){
			func(y+1,x-1);
		}
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

int w, h;
vector< vector<int> > c;

void Dfs(int i, int j) {
  c[i][j] = 0;
  for (int di = -1; di <= 1; ++di) {
    for (int dj = -1; dj <= 1; ++dj) {
      if (di == 0 && dj == 0)
        continue;

      int ni = i + di, nj = j + dj;
      if (ni < 0 || ni >= h || nj < 0 || nj >= w) continue;
      if (c[ni][nj]) Dfs(ni, nj);
    }
  }
}

int main() {
  while (true) {
    cin >> w >> h;

    if (w == 0 && h == 0)
      break;

    c.resize(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]) {
          Dfs(i, j);
          ++ans;
        }
      }
    }
    c.clear();

    cout << ans << endl;
  }
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<map>
#include<set>

using namespace std;

typedef long long ll;
typedef vector<int> VI;

#define FOR(i,n) for(int (i)=0;(i)<(n);(i)++)
#define FOR1(i,n) for(int (i)=1;(i)<(n);(i)++)
#define eFOR(i,n) for(int (i)=0;(i)<=(n);(i)++)
#define eFOR1(i,n) for(int (i)=1;(i)<=(n);(i)++)
#define SORT(i) sort((i).begin(),(i).end())
constexpr auto INF = 1000000000;
constexpr auto LLINF = 9223372036854775807;
constexpr auto mod = 1000000007;

int w, h, c[50][50];
int dx[8] = { -1,0,1,-1,1,-1,0,1 }, dy[8] = { -1,-1,-1,0,0,1,1,1 };
void search(int x, int y) {
	if (x >= 0 && x < w && y >= 0 && y < h) {
		if (c[y][x] == 1) {
			c[y][x] = 0;
			FOR(k, 8)search(x + dx[k], y + dy[k]);
		}
	}
}

int main() {
	while (1) {
		cin >> w >> h;
		if (w == 0 && h == 0)break;
		FOR(i, h)FOR(j, w)cin >> c[i][j];
		int cnt = 0;
		FOR(i, h)FOR(j, w)if (c[i][j] == 1) {
			search(j, i);
			cnt++;
		}
		cout <<"   "<< cnt << endl;
	}
	

	return 0;
}