Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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

int w,h;
int maze[100][100];

void solve(int x,int y){

  if(x < 0 || y < 0 || x == w || y == h) return;
  if(!maze[y][x]) return;

  maze[y][x] = 0;

  for(int i=-1;i<2;i++){
    for(int j=-1;j<2;j++){
      solve(x+i,y+j);
    }
  }

}
int main(){

  while(true){
    cin >> w >> h;
    if(!w && !h) break;

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

    int ans = 0;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(maze[i][j] == 1) solve(j,i), ans++;
      }
    }

    cout << ans << endl;

  }
}

C++: // include
#include <iostream>

// typedef
typedef unsigned long long ull;
typedef unsigned int uint;

// macro
#define REP(index, num) for (auto index = 0; index < num; index++)
#define REP1(index, num) for (auto index = 1; index <= num; index++)

#define MAX_NUM 60

// using namespace 
using namespace std;

const int ExistIsland(const int inX, const int inY);

// global
int W, H;
int c[MAX_NUM][MAX_NUM] = { 0 };

int main()
{
  while (1)
  {
    cin >> W >> H;

    // 入力が 0 0 なら終了
    if (W * H == 0)
      break;

    REP(hId, H)
      REP(wId, W)
      cin >> c[wId][hId];

    // 深さ優先探索
    int count = 0;
    REP(hId, H)
      REP(wId, W)
      count += ExistIsland(wId, hId);

    cout << count << endl;
  } 
}

const int ExistIsland(const int inX, const int inY)
{
  if (inX < 0 || W <= inX) return 0;
  if (inY < 0 || H <= inY) return 0;

  if (c[inX][inY] == 0)
    return 0;
  else
    c[inX][inY] = 0;

  //bool existFlag = true;

  // 周りに移動
  ExistIsland(inX + 1, inY);
  ExistIsland(inX + 1, inY + 1);
  ExistIsland(inX, inY + 1);
  ExistIsland(inX - 1, inY + 1);
  ExistIsland(inX - 1, inY);
  ExistIsland(inX - 1, inY - 1);
  ExistIsland(inX, inY - 1);
  ExistIsland(inX + 1, inY - 1);

  return 1;
}

C++: #include<iostream>
using namespace std;
struct Point
{
	int x;
	int y;
	Point(int x = 0, int y = 0) :x(x), y(y) {}
};
const int MAX = 50;
int map[MAX][MAX];
bool book[MAX][MAX];
void depthFSearch(int h, int w, Point start)
{
	int next[9][2] = { { -1,-1 },{ -1,0 },{ -1,1 },{ 0,-1 },{ 0,0 },{ 0,1 },{ 1,-1 },{ 1,0 },{ 1,1 } };
	for (int k = 0; k <= 8; k++)
	{
		int nx = start.x + next[k][0];
		int ny = start.y + next[k][1];
		if (nx < 0 || nx >= h || ny < 0 || ny >= w || !map[nx][ny])
			continue;
		if (!book[nx][ny] && map[nx][ny])
		{
			book[nx][ny] = true;
			Point p = { nx,ny };
			depthFSearch(h, w, p);
		}
	}
	return;
}
int main()
{
	while (true)
	{
		int w, h;
		cin >> w >> h;
		if (!w && !h)
			break;
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
			{
				cin >> map[i][j];
				book[i][j] = false;
			}
		int sum = 0;
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
			{
				if (!book[i][j] && map[i][j])
				{
					book[i][j] = true;
					Point start(i, j);
					depthFSearch(h, w, start);
					sum++;
				}
			}
		cout << sum << endl;
	}
}

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

int main(){

	
	int w,h;
	while(1){
		cin>>w>>h;if(w==0&&h==0)break;
		int n[51][51]={};
	bool N[51][51]={};
		for(int i=0;i<h;i++)
			for(int j=0;j<w;j++)
			{int x;cin>>x;if(x==1)N[i][j]=true;}

		int c=0;
		for(int i=0;i<h;i++)
			for(int j=0;j<w;j++)
			{if(N[i][j]==true&&n[i][j]==0){
				c++;
				n[i][j]=c;
				if(n[i-1][j+1]==0)n[i-1][j+1]=c;
				if(n[i-1][j]==0)n[i-1][j]=c;
				if(n[i-1][j-1]==0)n[i-1][j-1]=c;
				if(n[i][j+1]==0)n[i][j+1]=c;
				if(n[i-1][j]==0)n[i-1][j]=c;
				if(n[i+1][j+1]==0)n[i+1][j+1]=c;
				if(n[i+1][j]==0)n[i+1][j]=c;
				if(n[i+1][j-1]==0)n[i+1][j-1]=c;
			}
			else if(N[i][j]==true&&n[i][j]>0){
				int y=n[i][j];
				n[i-1][j+1]=y;
				n[i-1][j]=y;
				n[i-1][j-1]=y;
				n[i][j+1]=y;
				n[i-1][j]=y;
				n[i+1][j+1]=y;
				n[i+1][j]=y;
				n[i+1][j-1]=y;
			}
			}

			for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				int t=n[i][j];
				if(N[i][j]==true&&N[i][j+1]==true&&t>n[i][j+1])t=n[i][j+1];
				if(N[i][j]==true&&N[i][j-1]==true&&t>n[i][j-1])t=n[i][j-1];
				if(N[i][j]==true&&N[i+1][j+1]==true&&t>n[i+1][j+1])t=n[i+1][j+1];
				if(N[i][j]==true&&N[i+1][j]==true&&t>n[i+1][j])t=n[i+1][j];
				if(N[i][j]==true&&N[i+1][j-1]==true&&t>n[i+1][j-1])t=n[i+1][j-1];
				if(N[i][j]==true&&N[i-1][j+1]==true&&t>n[i-1][j+1])t=n[i-1][j+1];
				if(N[i][j]==true&&N[i-1][j]==true&&t>n[i-1][j])t=n[i-1][j];
				if(N[i][j]==true&&N[i-1][j-1]==true&&t>n[i-1][j-1])t=n[i-1][j-1];
				if(n[i][j]>t){
					for(int ii=0;ii<h;ii++)
			for(int jj=0;jj<w;jj++){
				if(n[i][j]==n[ii][jj])n[ii][jj]=t;
			}
				}
			}
			}

			int max=0;
			for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(N[i][j]==true&&n[i][j]>max)max=n[i][j];
			}}
			cout<<max<<endl;
			}
return 0;
}