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 <cmath>
#include <string>
#include <utility>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#define debug(x) cerr << #x << ':' << x << endl
using namespace std;
typedef long long ll;

int dx[8]={1,1,0,-1,-1,-1,0,1}, dy[8]={0,1,1,1,0,-1,-1,-1};
void dfs(int x, int y, int Map[51][51], int h, int w){
    if(!(0<=x && x <=h-1 && 0<=y && y<=w && Map[x][y]==1)) return;
    Map[x][y]=0;
    for(int i=0; i<8; i++)
        dfs(x+dx[i], y+dy[i], Map, h, w);
}

void disp(int Map[51][51], int h, int w){
    cout << "==========" << endl;
    for(int x=0; x<=h-1; x++){
        for(int y=0; y<=w-1; y++) cout << Map[x][y] << "";
        cout << endl;
    }
    cout << "==========" << endl;
}

int main(){
    int Map[51][51]={};
    int h, w;
    while(1){
        cin >> w >> h;
        if(h == 0 && w ==0) break;
        for(int x=0; x<=h-1; x++)
            for(int y=0; y<w; y++)
                cin >> Map[x][y];
        
        int cnt=0;
        for(int x=0; x<=h-1; x++)
            for(int y=0; y<w; y++){
                // cout << "before" << endl;
                // disp(Map, h, w);
                if(Map[x][y]==1) dfs(x, y, Map, h, w), cnt++;
                // cout << "after" << endl;
                // disp(Map, h, w);
            }
                
        
        cout << cnt << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <fstream>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <chrono>
using namespace std;

void connection(vector<vector<bool>> &checked, int i, int j, int h, int w) {
    if (!checked.at(i).at(j)) return;
    checked.at(i).at(j) = false;
    for (int x = max(0, i - 1); x <= min(h - 1, i + 1); x++) {
        for (int y = max(0, j - 1); y <= min(w - 1, j + 1); y++) {
            if (x != i || y != j) connection(checked, x, y, h, w);
        }
    }
    return;
}

int main(int argc, const char * argv[]) {
    
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    while(true) {
        int w, h;
        cin >> w >> h;
        if (h == 0 && w == 0) break;
        
        vector<vector<bool>> checked(h, vector<bool>(w, false));
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                bool a;
                cin >> a;
                checked.at(i).at(j) = a;
            }
        }
        
        int ans = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (checked.at(i).at(j)) {
                    connection(checked, i, j, h, w);
                    ans++;
                }
            }
        }
        
        cout << ans << endl;
    }
    
    return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <array>
#include <map>
#include <set>
using namespace std;

#define IS_POWER_TWO(x) (!(x & (x-1))) 
struct UnionFind {
	vector<int> data;
	UnionFind(int size) : data(size, -1) { }
	bool unionSet(int x, int y) {
		x = root(x); y = root(y);
		if (x != y) {
			if (data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		return x != y;
	}
	bool findSet(int x, int y) {
		return root(x) == root(y);
	}
	int root(int x) {
		return data[x] < 0 ? x : data[x] = root(data[x]);
	}
	int size(int x) {
		return -data[root(x)];
	}
};

typedef pair<int, int> pii;
const pii Dir_eight[8]{
	{0,-1},{-1,0},{1,0},{0,1},{-1,-1},{1,1},{-1,1},{1,-1}
};

int main()
{
	int w, h;

	while (scanf("%d %d", &w, &h)) {
		if (w == 0)
			return 0;
		array<array<int, 52>, 52> map = {};
		UnionFind uf(2501);

		for (int i = 0; h > i; i++) {
			for (int j = 0; w > j; j++) {
				int a;
				scanf("%d", &a);

				map[j + 1][i + 1] = a;
				if (!a)uf.unionSet(i*w + j, h*w);
			}
		}

		for (int i = 1; h >= i; i++) {
			for (int j = 1; w >= j; j++) {
				if (map[j][i]) {
					for (int k = 0; 8 > k; k++) {
						if (map[j + Dir_eight[k].first][i + Dir_eight[k].second]) {
							uf.unionSet(j + Dir_eight[k].first + (i + Dir_eight[k].second-1)*w-1, (i-1)*w + j-1);
						}
					}
				}
			}
		}

		set<int> ver;

		for (int i = 0; h*w+1 > i; i++) {
			ver.insert(uf.root(i));
		}
		
		printf("%d\n", ver.size() - 1);
	}
	return 0;
}