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 <queue>
#include <stdio.h>

using namespace std;

#define NUM 50

int w, h;
int c[NUM][NUM];
bool visited[NUM][NUM];
queue<pair<int, int> > Q;

void init(){
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            visited[i][j] = false;
        }
    }
}

bool can_visit(int i, int j){
    if(i < 0 || h <= i || j < 0 || w <= j){
        return false;
    }
    if(visited[i][j] || c[i][j] == 0){
        return false;
    }
    return true;
}

pair<int, int> find_new_place(){
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(can_visit(i, j)){
                return make_pair(i, j);
            }
        }
    }

    return make_pair(-1, -1);
}

bool exec(void){
    scanf("%d %d", &w, &h);
    if(w == 0 && h == 0){
        return false;
    }

    init();

    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            scanf("%d", &c[i][j]);
        }
    }

    int count = 0;

    while(true){
        pair<int, int> new_place = find_new_place();
        if(new_place.first == -1){
            break;
        }

        count++;

        Q.push(new_place);
        visited[new_place.first][new_place.second] = true;
        while(!Q.empty()){
            pair<int, int> p = Q.front(); Q.pop();
            int x = p.first;
            int y = p.second;

            vector<pair<int, int> > v;
            v.push_back(make_pair(x, y + 1));
            v.push_back(make_pair(x, y - 1));
            v.push_back(make_pair(x + 1, y + 1));
            v.push_back(make_pair(x + 1, y));
            v.push_back(make_pair(x + 1, y - 1));
            v.push_back(make_pair(x - 1, y + 1));
            v.push_back(make_pair(x - 1, y));
            v.push_back(make_pair(x - 1, y - 1));

            for(auto&& item : v){
                int x_ = item.first;
                int y_ = item.second;
                if(can_visit(x_, y_)){
                    visited[x_][y_] = true;
                    Q.push(make_pair(x_, y_));
                }
            }
        }
    }

    printf("%d\n", count);

    return true;
}

int main(void){
    while(exec()){
        ;
    }
    
    return 0;
}

C++: #include <stdio.h>

	int sima[50][50];
	int cnt=0;
	int cflag=0;

int count_sima(int w,int h){
	if(w<0)return 0;
	if(h<0)return 0;
	if(sima[w][h]==3)return 0;
	if(sima[w][h]==0)return 0;

		if(cflag==0){
			cnt++;
			cflag=1;
		}
		

		sima[w][h]=0;

		count_sima(w,h+1);
		count_sima(w+1,h+1);
		count_sima(w+1,h);
		count_sima(w+1,h-1);
		count_sima(w,h-1);
		count_sima(w-1,h-1);
		count_sima(w-1,h);
		count_sima(w-1,h+1);


	return 0;
}


int main(void){
	int w2,h2; //島の大きさを格納する
	int p;

	scanf("%d %d",&w2,&h2);
	while(w2!=0 && h2!=0){

		//島配列をすべてを3にする
		for(int i=0; i<50; i++){
			for(int j=0; j<50; j++){
				sima[i][j]=3;
			}
		}

		//入力された地図のデータを設定
		for(int i=0; i<h2; i++){
			for(int j=0; j<w2; j++){
				scanf("%d",&p);
				sima[i][j]=p;
			}
		}

		//ここまで入力された地図のデータを設定

		for(int i=0; i<h2; i++){
			for(int j=0; j<w2; j++){
				count_sima(i,j);
				cflag=0;
			}
		}

		printf("%d\n",cnt);
		cnt=0;

		scanf("%d %d",&w2,&h2);
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;
const int MOD = 1000000007;

template <typename T>
void print(const T &v);

vector<vector<int>> grid(55, vector<int>(55, -1));
vector<vector<bool>> seen(55, vector<bool>(55, false));

void dps(int x, int y)
{
    int xs[8] = {1, -1, 0, 0, -1, -1, 1, 1};
    int ys[8] = {0, 0, 1, -1, -1, 1, -1, 1};
    for (int i = 0; i < 8; i++)
    {
        int nx = x + xs[i];
        int ny = y + ys[i];
        if (nx < 0 || ny < 0)
        {
            continue;
        }
        if (seen[nx][ny] || grid[nx][ny] != 1)
        {
            continue;
        }
        seen[nx][ny] = true;
        dps(nx, ny);
    }
}

void init()
{
    for (int i = 0; i < 55; i++)
    {
        for (int j = 0; j < 55; j++)
        {
            grid[i][j] = -1;
            seen[i][j] = false;
        }
    }
}
int main()
{
    while (1)
    {
        int w, h;
        cin >> w >> h;
        if (w == 0 || h == 0)
        {
            return 0;
        }
        init();
        for (int i = 0; i < h; i++)
        {
            for (int j = 0; j < w; j++)
            {
                cin >> grid[i][j];
            }
        }
        int cnt = 0;
        for (int i = 0; i < h; i++)
        {
            for (int j = 0; j < w; j++)
            {
                if (seen[i][j] || grid[i][j] != 1)
                {
                    continue;
                }
                ++cnt;
                dps(i, j);
            }
        }
        cout << cnt << endl;
    }
}

// Use For Debug
template <typename T>
void print(T const &v)
{
    for (int i = 0; i < v.size(); i++)
    {
        if (i)
            cout << ' ';
        cout << v[i];
    }
    cout << endl;
}

C++: #include <cstdio>

using namespace std;

char mp[60][60] = {0};
int dx[9] = {0, 0, 1, -1, 1, -1, 1, -1};
int dy[9] = {1, -1, 0, 0, 1, -1,-1,  1};

void def(int y, int x){
	mp[y][x] = 0;
	for (int i = 0; i < 8; i++){
		int ty = y + dy[i];
		int tx = x + dx[i];
		if (mp[ty][tx] == '1') def(ty, tx);
	}
}

int main()
{
	int w, h;
	int pi, pj;
	int cnt;
	while (scanf("%d %d", &w, &h), !(w == 0 && h == 0)){
		cnt = 0;
		
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				scanf("%s", &mp[i][j]);
			}
		}
		
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (mp[i][j] == '1'){
					def(i, j);
					cnt++;
				}
			}
		}
		
		printf("%d\n", cnt);
	}
}