Go: package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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