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 <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;
}