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;
int w, h, isl, v;
int a[2501];
deque<int> Q;
int main()
{
	while(cin >> w >> h && w>0)
	{
		for(int i=1; i<=w*h; i++)
		{
			cin >> a[i];
		}
		isl=0;
		for(int i=1; i<=w*h; i++)
		{
			if(a[i]==1)
			{
				isl++;
				Q.clear();
				Q.push_back(i);
				while(!Q.empty())
				{
					v=Q.front();
					Q.pop_front();
					if(v>w)
					{
						if(a[v-w]==1)
						{
							a[v-w]=0;
							Q.push_back(v-w);
						}
						if(v%w!=1)
						{
							if(a[v-w-1]==1)
							{
								a[v-w-1]=0;
								Q.push_back(v-w-1);
							}
						}
					}
					if(v%w!=1)
					{
						if(a[v-1]==1)
						{
							a[v-1]=0;
							Q.push_back(v-1);
						}
					}
					if(v%w!=0)
					{
						if(v>w)
						{
							if(a[v-w+1]==1)
							{
								a[v-w+1]=0;
								Q.push_back(v-w+1);
							}
						}
						if(a[v+1]==1)
						{
							a[v+1]=0;
							Q.push_back(v+1);
						}
						if(v+w<w*h)
						{
							if(a[v+w+1]==1)
							{
								a[v+w+1]=0;
								Q.push_back(v+w+1);
							}
						}
					}
					if(v+w<=w*h)
					{
						if(v%w!=1)
						{
							if(a[v+w-1]==1)
							{
								a[v+w-1]=0;
								Q.push_back(v+w-1);
							}
						}
						if(a[v+w]==1)
						{
							a[v+w]=0;
							Q.push_back(v+w);
						}
					}
				}
			}
		}
		cout << isl << endl;
	}
	return 0;
}

C++: #include <stdio.h>
int map[100][100],n,m;
bool went[100][100];
void walk(int x,int y){
    if(x<0||y<0||x>=n||y>=m)return ;
    if(went[x][y])return ;
    if(map[x][y]==0)return ;
    went[x][y]=true;
    walk(x-1,y);
    walk(x+1,y);
    walk(x,y+1);
    walk(x,y-1);
    walk(x+1,y+1);
    walk(x+1,y-1);
    walk(x-1,y+1);
    walk(x-1,y-1);
    return ;
}
int main(){
    int ans;
    while(1){
        scanf("%d%d",&m,&n);
        if(n==0&&m==0)return 0;
        ans=0;
        for(int i=0;i<n;i++)for(int j=0;j<m;j++){
            scanf("%d",&map[i][j]);
            went[i][j]=false;
        }
        for(int i=0;i<n;i++)for(int j=0;j<m;j++)if(!went[i][j]&&map[i][j]==1){
            ans++;
            walk(i,j);
        }
        printf("%d\n",ans);
    }
}

C++: #include <algorithm>
#include <iostream>
#include <vector>
#include <cstring>

using namespace std;


bool done[52][52];
int field[52][52];
int ofs[8][2] = {
  {0, 1},
  {1, 0},
  {-1, 0},
  {0, -1},
  {1, 1},
  {1, -1},
  {-1,-1},
  {-1, 1}
};

void paint(int y, int x)
{
  for (int i = 0; i < 8; ++i) {
    int nx = x + ofs[i][0];
    int ny = y + ofs[i][1];
    if (field[ny][nx] && !done[ny][nx]) {
      done[ny][nx] = true;
      paint(ny, nx);
    }
  }
}

int main() {
  int w, h;
  for(;;) {
    cin >> w >> h;
    if ((w|h) == 0) {
      break;
    }
    memset(field, 0, sizeof(field));
    memset(done, 0, sizeof(done));
    for (int i = 1; i <= h; ++i) {
      for (int j = 1; j <= w; ++j) {
        cin >> field[i][j];
      }
    }
    int cnt = 0;
    for (int i = 1; i <= h; ++i) {
      for (int j = 1; j <= w; ++j) {
        if (field[i][j] && !done[i][j]) {
          paint(i, j);
          ++cnt;
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}