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 <bits/stdc++.h>

using namespace std;
using ll = long long;

void dfs(const vector<vector<bool>> &field, vector<vector<bool>> &visit, int x, int y, int w, int h) {
    visit[y][x] = true;
    for (int dx = -1; dx <= 1; dx++) {
        for (int dy = -1; dy <= 1; dy++) {
            if (dx == 0 && dy == 0)
                continue;
            int nx = x + dx;
            int ny = y + dy;

            if (0 <= nx && nx < w && 0 <= ny && ny < h && field[ny][nx] && !visit[ny][nx]) {
                dfs(field, visit, nx, ny, w, h);
            }
        }
    }
}

void solve() {
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0)
        exit(0);

    vector<vector<bool>> field(h, vector<bool>(w));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            int c;
            cin >> c;
            if (c)
                field[i][j] = true;
            else
                field[i][j] = false;
        }
    }

    int ans = 0;
    vector<vector<bool>> visit(h, vector<bool>(w, false));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (field[i][j] && !visit[i][j]) {
                dfs(field, visit, j, i, w, h);
                ans++;
            }
        }
    }

    cout << ans << endl;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    while (true)
        solve();

    return 0;
}

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

int dx[8] = {0,1,0,-1,1,1,-1,-1};
int dy[8] = {1,0,-1,0,1,-1,1,-1};

int h,w;
void dfs(vector<vector<int>> &s,int x,int y){
  if(x<0 || x>=h || y<0 || y>=w) return;
  if(!s[x][y]) return;
  s[x][y] = 0;
  rep(i,8) dfs(s,x+dx[i],y+dy[i]);
}
  
  
int main(){
  while(1){   
    cin>>w>>h; 
    if(!h) break;
    vector<vector<int>> s(h,vector<int>(w));
    rep(i,h)rep(j,w) cin>>s[i][j];
     
    int ans = 0;
    rep(k,250){
      rep(i,h){
        rep(j,w){
          if(s[i][j]){
            dfs(s,i,j);
            ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
}

C++: #include "bits/stdc++.h"
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define FORR(i, a, b) for(int i=(b)-1; i>=(a); --i)
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long LL;
typedef long double LD;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<bool> VB;
typedef vector<vector<int> > VVI;
typedef vector<vector<bool> > VVB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
const LL MOD=1000000007ll;
const LL INF=2000000000ll;

long power(long a,long b){return b?power(a*a%MOD,b/2)*(b%2?a:1)%MOD:1;}
long comb(int N,int K)
{
	long x=1,y=1;
	for(int i=1;i<=K;i++)
	{
		x=x*(N-i+1)%MOD;
		y=y*i%MOD;
	}
	return x*power(y,MOD-2)%MOD;
}

long hcomb(int N, int K){
    return comb(N+K-1, N-1);
}

void dfs(const vector<vector<int> > &G, int v, vector<int> &seen) {
    seen[v] = true; // v を訪問済にする

    // v から行ける各頂点 next_v について
    for (auto next_v : G[v]) { 
        if (seen[next_v]) continue; // next_v が探索済だったらスルー
        dfs(G, next_v, seen); // 再帰的に探索
    }
}

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

    int w, h;

    cin >> w >> h;

    while (w || h){
        VVI c(h, VI(w));
        VVI G(h*w);
        REP(i,h) REP(j,w) cin >> c[i][j];
        VI seen(h*w,false);
        REP(i,h) REP(j,w){
            if (c[i][j]){
                if (i != 0) if (c[i-1][j]) G[i*w+j].push_back((i-1)*w+j);
                if (i != h-1) if (c[i+1][j]) G[i*w+j].push_back((i+1)*w+j);
                if (j != 0) if (c[i][j-1]) G[i*w+j].push_back(i*w+j-1);
                if (j != w-1) if (c[i][j+1]) G[i*w+j].push_back(i*w+j+1);
                if (i != 0 && j != 0) if (c[i-1][j-1]) G[i*w+j].push_back((i-1)*w+j-1);
                if (i != 0 && j != w-1) if (c[i-1][j+1]) G[i*w+j].push_back((i-1)*w+j+1);
                if (i != h-1 && j != 0) if (c[i+1][j-1]) G[i*w+j].push_back((i+1)*w+j-1);
                if (i != h-1 && j != w-1) if (c[i+1][j+1]) G[i*w+j].push_back((i+1)*w+j+1);
            }
            else {
                seen[i*w+j] = true;
            }
        }

        int ans = 0;
        REP(i,h*w){
            if (seen[i] == false){
                dfs(G,i,seen);
                ans++;
            }
        }

        cout << ans << endl;

        cin >> w >> h;
    }
}