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<bits/stdc++.h>
using namespace std;

//  四方向へのベクトル
const int dx[8] = {-1,0,1,0,1,1,-1,-1};
const int dy[8] = {0,-1,0,1,1,-1,1,-1};

int H,W;
//探索用
bool seen[501][5001];
int MAX = 0;
//using Graph = vector<vector<int>>;
bool roop;
int dfs(const vector<vector<int>> &G,int nh,int nw){
    seen[nh][nw] = true;
    
    for(int i=0;i<8;i++){
        int next_h = nh + dy[i];//8方向へ移動
        int next_w = nw + dx[i];

        if(next_h<0 || next_h >=H ||next_w<0||next_w >= W) continue;
        if(seen[next_h][next_w]) continue;
        if(G[next_h][next_w] == 0) continue;

        dfs(G,next_h,next_w);
    }

}
int main(){
    while(true){
        int cnt=0;
        cin >> W >> H;
        if(H+W == 0) break;

        vector<vector<int>> G(H);
        for(int i=0;i<H;i++){
            for(int k=0;k<W;k++){
                int a;
                cin >> a;
                G[i].push_back(a);
            }
        }
        memset(seen,0,sizeof(seen));

        for(int i=0;i<H;i++){
            for(int k=0;k<W;k++){
                if(G[i][k] == 1&& !seen[i][k]){
                    dfs(G,i,k);
                    cnt++;
                }
            }
        }

        cout <<cnt <<endl;
    }
    return 0;
    
}

C++: #include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;

bool c[51][51];
bool checked[51][51];
int w, h;

void dfs(int y, int x) {
    if (checked[y][x] || !c[y][x]) return;
    checked[y][x] = true;
    repd(i, -1, 2) {
        repd(j, -1, 2) {
            if (i == 0 && j == 0) continue;
            int yy = y + i, xx = x + j;
            if (yy < 0 || yy >= h || xx < 0 || xx >= w) continue;
            dfs(yy, xx);
        }
    }
}

int main()
{
    while (1) {
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        rep(i, h) {
            rep(j, w) {
                int k;
                cin >> k;
                c[i][j] = k;
                checked[i][j] = false;
            }
        }

        int cnt = 0;
        rep(i, h) {
            rep(j, w) {
                if (!checked[i][j] && c[i][j]) {
                    dfs(i, j);
                    cnt++;
                }
            }
        }

        cout << cnt << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<math.h>
#include<string>
#include<limits.h>
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define nrep(i,a,b) for(int i=a;i<b;i++)
#define mrep(i,a) for(int i=a;i<=0;i++)
#define INF LLONG_MAX
using namespace std;

ll w, h, ans;
vector<pair<ll, ll> > idou(8);

void dfs(vector<vector<ll> > &map, ll tate, ll yoko) {
	map[tate][yoko] = ans;
	rep(i, 8) {
		ll t = tate + idou[i].first;
		ll y = yoko + idou[i].second;
		if (0 <= t && t < h && 0 <= y && y < w && !map[t][y]) dfs(map, t, y);
	}
}

int main() {
	//???
	idou[0].first = -1;
	idou[0].second = 0;
	//??????
	idou[1].first = -1;
	idou[1].second = 1;
	//???
	idou[2].first = 0;
	idou[2].second = 1;
	//??????
	idou[3].first = 1;
	idou[3].second = 1;
	//???
	idou[4].first = 1;
	idou[4].second = 0;
	//??????
	idou[5].first = 1;
	idou[5].second = -1;
	//???
	idou[6].first = 0;
	idou[6].second = -1;
	//??????
	idou[7].first = -1;
	idou[7].second = -1;

	while (1) {
		cin >> w >> h;
		if (!w && !h) break;
		ans = 0;
		vector<vector<ll> > map(h, vector<ll>(w, -1));
		rep(i, h) rep(j, w) {
			ll temp; cin >> temp;
			if (temp) map[i][j] = 0;
		}rep(i, h) rep(j, w) {
			if (!map[i][j]) {
				ans++;
				dfs(map, i, j);
			}
		}cout << ans << endl;
	}
	return 0;
}

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

using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)

int w, h;
int map[50][50];
int ans;

bool iswalkable(int ty,int tx){
	if(ty >= 0 && ty < h && tx >= 0 && tx < w) return true;
	return false;
}

void dfs(int y, int x){
	if(map[y][x] == 0) return;
	map[y][x] = 0;
	
	for(int vy = -1; vy <= 1; vy++){
		for(int vx = -1; vx <= 1; vx++){
			
			if(vy == 0 && vx == 0) continue;
			int ty = y + vy;
			int tx = x + vx;
			if(iswalkable(ty, tx)){
				dfs(ty, tx);
			}
			
		}
	}
	return;
}
			
	
int main(int argc, char const* argv[])
{
	
	while(true){
		cin >> w >> h;
		if(w == 0 && h == 0) break;

		ans = 0;
		rep(i, h) rep(j, w) cin >> map[i][j];

		rep(i, h){
			rep(j, w){
				if(map[i][j]){
					ans++;
					dfs(i, j);
				}
			}
		}


		cout << ans << endl;
	}
	
	return 0;
}