Go: package main

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

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		return []byte(f1())
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

// -----------------------------------------------------------------------------

func main() {
	defer stdout.Flush()

	for {
		ans, ok := solve()
		if !ok {
			break
		}
		println(ans)
	}

}

func solve() (int, bool) {
	w := readInt()
	h := readInt()
	if w == 0 && h == 0 {
		return 0, false
	}

	c := make([][]int, h+2)
	c[0] = make([]int, w+2)
	c[h+1] = make([]int, w+2)
	for i := 1; i-1 < h; i++ {
		c[i] = make([]int, w+2)
		for j := 1; j-1 < w; j++ {
			c[i][j] = readInt()
		}
	}

	var ans int
	for i := 1; i-1 < h; i++ {
		for j := 1; j-1 < w; j++ {
			if 0 < c[i][j] {
				ans++
				dfs(c, i, j)
			}
		}
	}
	return ans, true
}

func dfs(c [][]int, y, x int) {
	c[y][x] = 0
	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{-1, -1, -1, 0, 0, 1, 1, 1}
	for i := range dx {
		nx := x + dx[i]
		ny := y + dy[i]
		if 0 < c[ny][nx] {
			dfs(c, ny, nx)
		}
	}
}

// -----------------------------------------------------------------------------

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<math.h>
using namespace std;
#define N (1000000000+7)
#define INF 1e16
typedef long long ll;
typedef pair<int,int> P;

int h,w;
int board[52][52];
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
bool check(int i, int j) {
	return (i >= 0) && (i < h) && (j >= 0) && (j < w) && (board[i][j] ==1);
}

void dfs(int x,int y){
    if(board[x][y]==0)return;
    board[x][y]=0;
    for(int i=x-1;i<=x+1;i++){
        for(int j=y-1;j<=y+1;j++){
            if(i==x && j==y)continue;
            if(check(i,j))dfs(i,j);
        }
    }
    return;
}

int main(void){
    while(true){
        int ans = 0;
        cin>>w>>h;
        if(w==0 && h==0)break;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin>>board[i][j];
            }
        }
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(board[i][j]==1){
                    dfs(i,j);
                    ans++;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T& a, const T& b)
{
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T& a, const T& b)
{
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(x) (x).begin(), (x).end() //sortなどの引数を省略したい
#define F first
#define S second

typedef long long ll;
const ll MOD = 1e9 + 7;
const int inf = 1000000;
const int minf = -1000000;
const ll lnf = 1e12;
const ll mlnf = -1e12;

int H, W;
int dx[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };
int dy[8] = { 1, 0, -1, 0, 1, -1, 1, -1 };
void dfs(vector<vector<int>>& field, int x, int y);
int main()
{
    while (1) {
        cin >> W >> H;
        if (H == 0 && W == 0)
            break;
        vector<vector<int>> field(H, vector<int>(W));
        rep(i, H) rep(j, W) cin >> field[i][j];
        int ans = 0;
        for (int h = 0; h < H; h++) {
            for (int w = 0; w < W; w++) {
                if (field[h][w] == 0)
                    continue;
                dfs(field, h, w);
                ans++;
            }
        }
        cout << ans << endl;
    }
}

void dfs(vector<vector<int>>& field, int x, int y)
{
    field[x][y] = 0;
    for (int i = 0; i < 8; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx < 0 || H <= nx || ny < 0 || W <= ny)
            continue;
        if (field[nx][ny] == 0)
            continue;
        dfs(field, nx, ny);
    }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int map[60][60];
int main(void) {
	vector<int> list;
	for (;;) {
		int w, h;
		cin >> w >> h;
		if (!(w&&h))return 0;
		int cnt = 1;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				int buf;
				cin >> buf;
				map[i][j] = (buf?cnt++:0);
			}
		}
		bool update = true;
		for (;update;){
			cnt = 0;
			update = false;
			for (int i = 0; i < h; i++) {
				for (int j = 0; j < w; j++) {
					if (!map[i][j])continue;
					for (int n = max(0, i - 1); n <= min(h-1, i + 1); n++) {
						for (int m = max(0, j - 1); m <= min(w-1, j + 1); m++) {
							if (!map[n][m])continue;
							if (map[i][j]>map[n][m]) {
								map[i][j] = map[n][m];
								update = true;
							}
						}
					}
				}
			}
		}
		vector<int> num;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				num.push_back(map[i][j]);
			}
		}
		sort(num.begin(), num.end());
		num.erase(unique(num.begin(),num.end()),num.end());
		cout << num.size()-((num[0]==0)?1:0)<<endl;
	}
	return 0;
}

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

struct Map {
	std::vector<bool> data; // true ????????¶
	int width;
	int height;

	void initialize(int width, int height) {
		this->width = width;
		this->height = height;
		data.resize(width * height);
	}

	bool is_ground(int x, int y) {
		return data[y * width + x];
	}

	void set_as_sea(int x, int y) {
		data[y * width + x] = false;
	}
};

void paint(Map& map, int x, int y) {
	if (x < 0 || x >= map.width || y < 0 || y >= map.height) return;
	if (!map.is_ground(x, y)) return;

	map.set_as_sea(x, y);

	paint(map, x - 1, y - 1);
	paint(map, x - 1, y);
	paint(map, x - 1, y + 1);
	paint(map, x, y - 1);
	paint(map, x, y + 1);
	paint(map, x + 1, y - 1);
	paint(map, x + 1, y);
	paint(map, x + 1, y + 1);
}

int main()
{
	Map map;

	while (true) {
		int w, h;
		std::cin >> w >> h;

		if (w == 0) return 0;

		map.initialize(w, h);

		for (int i = 0; i < w * h; i++) {
			int c;
			std::cin >> c;
			map.data[i] = c == 1;
		}

		int count = 0;

		for (int y = 0; y < h; y++) {
			for (int x = 0; x < w; x++) {
				if (map.is_ground(x, y)) {
					count++;
					paint(map, x, y);
				}
			}
		}

		std::cout << count << std::endl;
	}
}