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>
typedef long long ll;
using namespace std;
ll MOD = 1000000007;

#define vec                vector<int>
#define vecll              vector<ll>
#define vecd               vector<double>
#define vecst              vector<string>
#define vecb               vector<bool>
#define vec2(var, n, m)    vector<vector<int>> var(n, vector<int>(m, 0))
#define vecll2(var, n, m)  vector<vector<ll>> var(n, vector<ll>(m, 0))

#define rep(i,n)         for(ll i=(ll)0; i<(ll)n; i++)
#define REP(i,m,n)       for(ll i=(ll)m; i<(ll)n; i++)
#define arr(var, n)      vec var(n); rep(i,n){cin >> var[i];}
#define arrll(var, n)    vecll var(n); rep(i,n){cin >> var[i];}
#define arrst(var, n)    vecst var(n); rep(i,n){cin >> var[i];}
#define all(var)         (var).begin(), (var).end()
#define sortall(var)     sort(all(var))
#define uniqueall(v)     v.erase(unique(v.begin(), v.end()), v.end());
#define f_sum(var)       accumulate(all(var), 0)
#define f_sumll(var)     accumulate(all(var), 0LL)
#define chmin(v1, v2)    v1 = min(v1, v2)
#define chmax(v1, v2)    v1 = max(v1, v2)
#define pb(var)          push_back(var)
#define prt(var)         cout << (var) << "\n"
#define prtd(n, var)     cout << fixed << setprecision(n) << var << "\n"
#define prtfill(n, var)  cout << setw(n) << setfill('0') << var;
#define prt2(v1, v2)     cout << v1 << " " << v2 << "\n"
#define prt3(v1, v2, v3) cout << v1 << " " << v2 << " " << v3 << "\n"
#define prtall(v)        rep(i,v.size()){cout<<v[i]<<(i!=v.size()-1?" ":"\n");}
void prtok(bool ok){prt(ok ? "yes" : "no");}
//----------------------------------------------------------------
bool ch[51][51];
int h, w;

void dfs(vector<vector<bool>> mp, int i, int j){
  //prt3("--->", i, j);
  ch[i][j] = true;
  bool up=true, down=true, left=true, right=true;

  if(i==0) up=false;
  if(i==h-1) down=false;
  if(j==0) left=false;
  if(j==w-1) right=false;

  if(up && left){if(mp[i-1][j-1] && !ch[i-1][j-1]) dfs(mp, i-1, j-1);}
  if(up) {if(mp[i-1][j] && !ch[i-1][j]) dfs(mp, i-1, j);}
  if(up && right) {if(mp[i-1][j+1] && !ch[i-1][j+1]) dfs(mp, i-1, j+1);}
  if(left) {if(mp[i][j-1] && !ch[i][j-1]) dfs(mp, i, j-1);}
  if(right) {if(mp[i][j+1] && !ch[i][j+1]) dfs(mp, i, j+1);}
  if(down && left) {if(mp[i+1][j-1] && !ch[i+1][j-1]) dfs(mp, i+1, j-1);}
  if(down) {if(mp[i+1][j] && !ch[i+1][j]) dfs(mp, i+1, j);}
  if(down && right) {if(mp[i+1][j+1] && !ch[i+1][j+1]) dfs(mp, i+1, j+1);}
}

int main(void) {
  while(1){
    rep(i,51){
      rep(j,51){
        ch[i][j]=false;
      }
    }
    cin >> w >> h;

    if(w==0 && h==0){break;}
    vector<vector<bool>> mp(h, vector<bool>(w, false));
    rep(i,h){
      rep(j,w){
        int tmp;
        cin >> tmp;
        mp[i][j] = (tmp>0 ? true : false);
      }
    }

    int ans=0;
    rep(i,h){
      rep(j,w){
        if(!ch[i][j] && mp[i][j]){
          ans++;
          dfs(mp, i, j);
        }
      }
    }
    prt(ans);
  }
}

C++: #include <iostream>

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

void dfs(int x,int y){
    c[x][y] = 0;
    for(int dx=-1;dx<=1;dx++){
        for(int dy=-1;dy<=1;dy++){
            int nx = x+dx,ny= y+dy;
            if(0<=nx&&nx<h&&0<=ny&&ny<w&&c[nx][ny] == 1){
                dfs(nx,ny);
            }
        }
    }
    return;
}

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

C++: #include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<functional>
#define rep(i, n) for(int i = 0; i < n; i++)
#define repp(i, a, b) for(int i = a; i <= b; i++)
using namespace std;
using ll = long long;
using vel = vector<ll>;
using vvel = vector<vel>;

vvel maze;
ll w, h;

ll dfs(ll y, ll x) {
    if(maze[y][x]!=1) return 0;
    maze[y][x] = -1;
    repp(dy, -1, 1) {
        repp(dx, -1, 1) {
            if(y+dy < 0 || y+dy >= h || x+dx < 0 || x+dx >= w ) continue;
            if(dx == 0 && dy == 0) continue;
            dfs(y+dy, x+dx);
        }
    }
    return 1;
};

int main() {
    while(cin >> w >> h, w) {
        maze.assign(h, vel(w, 0));
        rep(y, h) rep(x, w) cin >> maze[y][x];
        ll ans = 0;
        rep(y, h) {
            rep(x, w) {
                if(maze[y][x] == 1) 
                    ans += dfs(y, x);
            }
        }
        cout << ans << endl;
    }
}