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;
vector<int> num;
unsigned long long int mod = 1000000007;

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

void runtime() {
    count1 = 0;
    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;
            }
        }
    }
    num.push_back(count1);
}


int main() {
    cin >> w >> h;
    while (!(w == 0 && h == 0)) {
        count1 = 0;
        runtime();
        cin >> w >> h;
    }
    for (auto i : num) {
        cout << i << endl;
    }
    return 0;
}

C++: #include<cstdio>
using namespace std;
int w,h,cnt,a[50][50];
void func(int,int);

int main(void)
{
	int i,j;
	while(1)	{
		scanf("%d %d",&w,&h);
		if(w==0 && h==0)	break;
		for(i=0;i<h;i++)	{
			for(j=0;j<w;j++)	{
				scanf("%d",&a[i][j]);
			}
		}
		cnt=0;
		for(i=0;i<h;i++)	{
			for(j=0;j<w;j++)	{
				if(a[i][j]==1)	{
					cnt++;
					a[i][j]=0;
					func(i,j);
				}
			}
		}
		printf("%d\n",cnt);
	}
	return 0;
}
void func(int y,int x)
{
	int i,j,xx,yy;
	for(i=-1;i<=1;i++)	{
		for(j=-1;j<=1;j++)	{
			if(y+i>=0 && y+i<h && x+j>=0 && x+j<w && a[y+i][x+j]==1)	{
				a[y+i][x+j]=0;
				func(y+i,x+j);
			}
		}
	}
}

C++: 

#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <cmath>
#include <string>
#include <sstream>
#include <iomanip>
#include <complex>
using namespace std;
 
#define ll long long
#define vvi vector< vector<int> >
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back 
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
 
int w,h;
int island[200][200];
int c=0;
bool searched[200][200];

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

void search(int x,int y){ 
  searched[x][y] = true;
  //island[i][j]==1?????????
  REP(i,8){
	int nx=x+dx[i]; int ny=y+dy[i];
	if(nx<0||ny<0||nx>h-1||ny>w-1) continue;
	//cout << nx << " " << ny << endl;
	if(island[nx][ny]&&searched[nx][ny]==false){
	  search(nx,ny);
	}
  }
}

int main(){

  while(1){
	c = 0;
	cin >> w >> h;

	if(w==0&&h==0) break;
	REP(i,h){
	  REP(j,w){
		searched[i][j] = false;
		cin >> island[i][j];
	  }
	}

	REP(i,h){
	  REP(j,w){
		if(island[i][j]&&searched[i][j]==false){
		  search(i,j);
		  c++;
		}

	  }}

	cout << c <<endl;

  }


}

C++: #include <iostream>

using namespace std;

#define MAX_H 55
#define MAX_W 55

int W, H;

int C[MAX_H][MAX_W];

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

void dfs(int x, int y)
{
	if (C[y][x] == 1)
	{
		C[y][x] = 0;

		for (int i = 0; i < 8; i++)
		{
			if (0 <= x + dx[i] && x + dx[i] <= W - 1 && 0 <= y + dy[i] && y + dy[i] <= H - 1)
			{
				if (C[y + dy[i]][x + dx[i]] == 1)
				{
					dfs(x + dx[i], y + dy[i]);
				}
			}
		}
	}
}

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

		if (W == 0 && H == 0) { break; }

		int count = 0;

		for (int i = 0; i < H; i++)
		{
			for (int j = 0; j < W; j++)
			{
				if (C[i][j] == 1)
				{
					dfs(j, i);

					count++;
				}
			}
		}

		cout << count << endl;
	}

	return 0;
}

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

void dfs(std::vector<std::vector<int> >& map, std::vector<std::vector<int> >& visited, int& island, int x, int y, int w, int h)
{
	visited.at(x).at(y) = island;
	int dx[8] = {1, 1, 0, -1, -1, 0, 1, -1};
	int dy[8] = {1, 0, 1, -1, 0, -1, -1, 1};
	for (int i = 0; i < 8; i++)
	{
		int nex = dx[i] + x;
		int ney = dy[i] + y;
		if (nex < 0 || nex >= w || ney < 0 || ney >= h)
			continue;
		if (map.at(nex).at(ney) == 1 && visited.at(nex).at(ney) != island)
		{	
			dfs(map, visited, island, nex, ney, w, h);
		}
	}
}


int main()
{
	std::vector<int> I;
	while (true)
	{
		int w, h;
		std::cin >> w >> h;
		if (w == 0 && h == 0)
			break;
		std::vector<std::vector<int> > map(h, std::vector<int> (w));
		std::vector<std::vector<int> > visited(h, std::vector<int> (w, 0));
		for (int i = 0; i < h; i++)
		{
			for (int j = 0; j < w; j++)
			{
				std::cin >> map.at(i).at(j);
			}
		}
		int island = 1;
		for (int i = 0; i < h; i++)
		{
			for (int j = 0; j < w; j++)
			{
				if (map.at(i).at(j) == 1 && visited.at(i).at(j) == 0)
				{
					dfs(map, visited, island, i, j, h, w);
					++island;	
				}
			}
		}
		I.push_back(island - 1);
	}
	for (std::vector<int>::iterator i = I.begin(); i != I.end(); i++)
		std::cout << *i << '\n';

	return 0;
}