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++: //
// Created by elegant-belltree on 15/06/03.
//

#include <iostream>
#include <set>

using namespace std;

int main(){
    int w,h;
    int c[50][50];

    while (cin>>w>>h && (w>0 || h>0)) {
        int label=2;
        set<int> labels;

        for (int n = 0; n < 2; ++n) {
            for (int i = 0; i < h; ++i) {
                for (int j = 0; j < w; ++j) {
                    if(n==0) {
                        cin >> c[j][i];
                    }
                    if (0<c[j][i]) {
                        for (int k = -1; k < n+1 && i+k<h; ++k) {
                            for (int l = -1; (n!=0 || k!=0 || l!=0) && l < 3 && j + l < w; ++l) {
                                if (-1 < i + k && -1 < j + l && 0 < c[j + l][i + k] && (c[j][i] == 1 || c[j + l][i + k] < c[j][i])) {
                                    if(n==1){
                                        labels.erase(c[j][i]);
                                    }
                                    c[j][i] = c[j + l][i + k];
                                }
                            }
                        }
                        if (c[j][i] == 1) {
                            c[j][i] = label++;
                            labels.insert(c[j][i]);
                        }
                    }
                }
            }
        }
        /*for (int m = 0; m < h; ++m) {
            for (int i = 0; i < w; ++i) {
                cout<<c[i][m]<<" ";
            }
            cout<<endl;
        }*/
        cout<<labels.size()<<endl;
    }
    return 0;
}

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

void update(int x,int y,vector< vector<int> >& map){
	if(map[y][x]==1){
		map[y][x]=2;
	}else{
		return;
	}
	if(x!=0)update(x-1,y,map);
	if(y!=0)update(x,y-1,map);
	if(x!=0&&y!=0)update(x-1,y-1,map);
	if(y!=map.size()-1)update(x,y+1,map);
	if(x!=map[0].size()-1)update(x+1,y,map);
	if(y!=map.size()-1&&x!=0)update(x-1,y+1,map);
	if(y!=map.size()-1&&x!=map[0].size()-1)update(x+1,y+1,map);
	if(x!=map[0].size()-1&&y!=0)update(x+1,y-1,map);
}
int main(){
	while(true){
		vector< vector<int> > map;
		int w,h,tmp,sum=0;
		cin>>w>>h;
		if(w==0)break;
		map.resize(h);
		for(int y=0;y<h;y++){
			for(int x=0;x<w;x++){
				map[y].resize(w);
				cin>>tmp;
				map[y][x]=tmp;
			}
		}
		for(int y=0;y<h;y++){
			for(int x=0;x<w;x++){
				if(map[y][x]==1){
					sum++;
					update(x,y,map);
				}
			}
		}
		cout<<sum<<endl;
	}
	return 0;
}

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

typedef pair<int,int> P;

const int dx[] = { 0, 1, 0,-1, 1, 1,-1,-1};
const int dy[] = { 1, 0,-1, 0, 1,-1, 1,-1};
int h, w;
vector<vector<int> > islands(50, vector<int>(50));

void bfs (int i, int j) {
    queue<P> q;
    q.push(P(i,j));
    while (q.size()) {
        const P p = q.front();
        q.pop();
        for (int i = 0; i < 8; i++) {
            int x = p.first + dx[i], y = p.second + dy[i];
            if (0 <= x && x < w && 0 <= y && y < h && islands[x][y]) {
                islands[x][y] = 0;
                q.push(P(x,y));
            }
        }
    }
}

int main ()
{
    while (cin >> h >> w, (h || w)) {
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                cin >> islands[i][j];
            }
        }

        int count = 0;
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                if (islands[i][j] == 1) {
                    count++;
                    islands[i][j] = 0;
                    bfs(i, j);
                }
            }
        }
        cout << count << endl;
    }


    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i=0; i < (int)(n); i++)
using ull = unsigned long long;
using ll = long long;

struct pos {
    int x, y;
};

int main() {

    while (true) {
        int W, H;
        cin >> W >> H;
        if (W*H == 0) break;

        bool C[W][H] = {};
        rep(y, H) rep(x, W) cin >> C[x][y];

        int cnt = 0;
        stack<pos> s;
        rep(y, H) rep(x, W) {
            if (C[x][y]) {
                cnt++;
                s.push({x, y});
            }
            while (!s.empty()) {
                pos p = s.top();
                s.pop();
                C[p.x][p.y] = false;
                for (int dx=-1; dx<=1; dx++) {
                    for (int dy=-1; dy<=1; dy++) {
                        int nx = p.x+dx, ny = p.y+dy;
                        if ((nx<0) || (nx>=W) || (ny<0) || (ny>=H)) continue;
                        if (C[nx][ny]) {
                            s.push({nx, ny});
                            // cout << p.x << p.y << " " << nx << ny << endl;
                        }
                    }
                }
            }
        }
        cout << cnt << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
 
#define rep(i,n) for(ll i=0;i<n;++i)
#define ALL(a) (a).begin(),(a).end()
 
using namespace std;
using ll  = long long;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
 
const ll MOD  = 1e9+7;
const ll LINF = 1LL << 60;
const int INF = 1e9 + 7;

int mv[3] = {1, 0, -1};

int h, w;
int atlas[50][50];

void dfs(int y, int x){

    rep(i, 3)rep(j, 3){
        int dx = x + mv[i];
        int dy = y + mv[j];

        if(dx < 0 || dx >= w || dy < 0 || dy >= h)continue;

        if(atlas[dy][dx] == 1){
            atlas[dy][dx] = 0;
            dfs(dy, dx);
        }
    }
}

int main(){
    vector<int> ans;
    while(true){
        cin >> w >> h;

        if(h + w == 0)break;

        rep(i, h)rep(j, w){
            cin >> atlas[i][j];
        }

        int res = 0;
        rep(i, h)rep(j, w){
            if(atlas[i][j] == 1){
                atlas[i][j] = 0;
                res++;

                dfs(i, j);
            }
        }

        ans.push_back(res);
    }

    rep(i, ans.size()){
        cout << ans[i] << endl;
    }
}