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"

#define REP(i,num) for(ll i=0;i<(num);++i)
#define LOOP(i) while(i--)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=ll>
T in(){T x; cin >> x; return (x);}
template<typename T=ll,typename C=vector<T>>
C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);}

void vout(){cout << endl;}
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);}
void out(){cout << endl;}
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);}

class ConnectNodeInfo{
	vector<vector<pair<ll,ll>>> graph;
public:
	ConnectNodeInfo(int node_num){
		graph.resize(node_num);
	}
	void AddNonDirectionalConnection(ll u,ll v,ll w){
		graph[u].emplace_back(v,w);
		graph[v].emplace_back(u,w);
	}
	void AddDirectionalConnection(ll u,ll v,ll w){
		graph[u].emplace_back(v,w);
	}
	vector<pair<ll,ll>>& operator[](ll index){
		return graph[index];
	}
	size_t size(){return graph.size();}
};

vector<int> D,F;

int dfs(int p,int num,int H,int W,vector<vector<int>>& M,vector<vector<int>>& D){
	int u=p%W,v=p/W;
	D[v][u] = num;
	if(u>0 && M[v][u-1] && D[v][u-1]==-1){
		dfs(u-1+v*W,num,H,W,M,D);
	}
	if(u<W-1 && M[v][u+1] && D[v][u+1]==-1){
		dfs(u+1+v*W,num,H,W,M,D);
	}
	if(v>0 && M[v-1][u] && D[v-1][u]==-1){
		dfs(u+(v-1)*W,num,H,W,M,D);
	}
	if(v<H-1 && M[v+1][u] && D[v+1][u]==-1){
		dfs(u+(v+1)*W,num,H,W,M,D);
	}

	if(u>0 && v>0 && M[v-1][u-1] && D[v-1][u-1]==-1){
		dfs(u-1+(v-1)*W,num,H,W,M,D);
	}
	if(u<W-1 && v<H-1 && M[v+1][u+1] && D[v+1][u+1]==-1){
		dfs(u+1+(v+1)*W,num,H,W,M,D);
	}
	if(v>0 && u<W-1 && M[v-1][u+1] && D[v-1][u+1]==-1){
		dfs(u+1+(v-1)*W,num,H,W,M,D);
	}
	if(v<H-1 && u>0 && M[v+1][u-1] && D[v+1][u-1]==-1){
		dfs(u-1+(v+1)*W,num,H,W,M,D);
	}
	return 0;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	while(true){
		int W=in(),H=in();
		if(!W && !H) break;

		vector<vector<int>> M(H,vector<int>(W,0)),D(H,vector<int>(W,-1));
		REP(i,H){
			REP(j,W){
				M[i][j]=in();
			}
		}

		int num=0;
		REP(i,H){
			REP(j,W){
				if(M[i][j] && D[i][j]==-1){
					dfs(j+i*W,++num,H,W,M,D);
				}
			}
		}
		out(num);
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
enum class WalkingResult { undef, walked };
enum class LandS { sea, land };
using LMap = vector<vector<LandS>>;

void solve(int w, int h) {
    LMap lMap(h, vector<LandS>(w));
    vector<pair<int, int>> lands;
    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            int t;
            cin >> t;
            lMap[i][j] = t == 1 ? LandS::land : LandS::sea;
            if (t == 1) lands.push_back(make_pair(i, j));
        }
    }

    vector<vector<WalkingResult>> wMap(h, vector<WalkingResult>(w, WalkingResult::undef));
    int count = 0;
    stack<pair<int, int>> s;
    for (int i = 0; i < lands.size(); ++i) {
        pair<int, int> x = lands[i];
        bool already = wMap[x.first][x.second] == WalkingResult::walked;
        if (already) continue;
        s.push(x);
        while(!s.empty()) {
            pair<int, int> pos = s.top();
            s.pop();
            bool out = pos.first < 0 || h <= pos.first || pos.second < 0 || w <= pos.second;
            if (out) continue;
            bool sea = lMap[pos.first][pos.second] == LandS::sea;
            bool walked = wMap[pos.first][pos.second] == WalkingResult::walked;
            if (sea || walked) continue;
            wMap[pos.first][pos.second] = WalkingResult::walked;
            s.push(make_pair(pos.first + 1, pos.second));
            s.push(make_pair(pos.first - 1, pos.second));
            s.push(make_pair(pos.first, pos.second + 1));
            s.push(make_pair(pos.first, pos.second - 1));
            s.push(make_pair(pos.first - 1, pos.second - 1));
            s.push(make_pair(pos.first + 1, pos.second - 1));
            s.push(make_pair(pos.first - 1, pos.second + 1));
            s.push(make_pair(pos.first + 1, pos.second + 1));
        }
        count++;
    }
    cout << count << endl;
}

int main() {

    while (true) {
        int w, h;
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        solve(w, h);
    }
    return 0;
}

C++: #include <iostream>
using namespace std;

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

void dfs(int x,int y) {
    used[x][y]=1;
    for(int dx=-1; dx<=1; ++dx) {
        for(int dy=-1; dy<=1; ++dy) {
            int xx=x+dx;
            int yy=y+dy;
            if(x+dx < 0 || x+dx >=h) continue;
            if(y+dy < 0 || y+dy >=w) continue;
            if(used[xx][yy] || c[xx][yy]==0) continue;
            dfs(xx,yy);
        }
    }
}

int main() {
    while(true) {
        int ans=0;
        cin>>w>>h;
        if(!w) return 0;
        for(int i=0; i<h; ++i) {
            for(int j=0; j<w; ++j) {
                cin>>c[i][j];
                used[i][j]=0;
            }
        }
        for(int i=0; i<h; ++i) {
            for(int j=0; j<w; ++j) {
                if(used[i][j] || c[i][j]==0) continue;
                //cerr<<i<<" "<<j<<endl;
                dfs(i,j);
                ans++;
            }
        }
        cout<<ans<<endl;
    }
}