Go: package main

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

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		return []byte(f1())
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

// -----------------------------------------------------------------------------

func main() {
	defer stdout.Flush()

	for {
		ans, ok := solve()
		if !ok {
			break
		}
		println(ans)
	}

}

func solve() (int, bool) {
	w := readInt()
	h := readInt()
	if w == 0 && h == 0 {
		return 0, false
	}

	c := make([][]int, h+2)
	c[0] = make([]int, w+2)
	c[h+1] = make([]int, w+2)
	for i := 1; i-1 < h; i++ {
		c[i] = make([]int, w+2)
		for j := 1; j-1 < w; j++ {
			c[i][j] = readInt()
		}
	}

	var ans int
	for i := 1; i-1 < h; i++ {
		for j := 1; j-1 < w; j++ {
			if 0 < c[i][j] {
				ans++
				dfs(c, i, j)
			}
		}
	}
	return ans, true
}

func dfs(c [][]int, y, x int) {
	c[y][x] = 0
	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{-1, -1, -1, 0, 0, 1, 1, 1}
	for i := range dx {
		nx := x + dx[i]
		ny := y + dy[i]
		if 0 < c[ny][nx] {
			dfs(c, ny, nx)
		}
	}
}

// -----------------------------------------------------------------------------

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

C++: #include <bits/stdc++.h>

using namespace std;

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

int w, h;
int c[55][55];

void dfs(int i, int j) {
    if(c[i][j] == 0) return;
    
    c[i][j] = 0;
    dfs(i-1, j-1);
    dfs(i-1, j);
    dfs(i-1, j+1);
    dfs(i  , j-1);
    dfs(i  , j+1);
    dfs(i+1, j-1);
    dfs(i+1, j);
    dfs(i+1, j+1);
}

int main() {
	while(1) {
	    cin >> w >> h;
	    if(w == 0 && h == 0) break;
	    
	    rep(i, h) rep(j, w) cin >> c[i+1][j+1];
	    
	    int ans = 0;
	    rep(i, h) {
	        rep(j, w) {
	            if(c[i+1][j+1] == 0) continue;
	            dfs(i+1, j+1);
	            ans++;
	        }
	    }
	    cout << ans << endl;
	}
}