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++: /*
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp
*/

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const ll MOD = 1000000007;

const int di[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dj[8] = {-1, 0, 1, -1, 1, -1, 0, 1};

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

int solve(){
    int cnt = 0;
    bool visited[h][w];
    memset(visited, false, sizeof visited);
    queue<pair<int, int> > Q;

    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(!visited[i][j] && c[i][j] == 1){
                cnt++;
                // bfs
                visited[i][j] = true;
                Q.push(make_pair(i, j));
                while(!Q.empty()){
                    pair<int, int> p = Q.front(); Q.pop();
                    int si = p.first;
                    int sj = p.second;

                    for(int k=0;k<8;k++){
                        int ni = si + di[k];
                        int nj = sj + dj[k];
                        if(ni < 0 || ni >= h || nj < 0 || nj >= w) continue;
                        if(!visited[ni][nj] && c[ni][nj] == 1){
                            visited[ni][nj] = true;
                            Q.push(make_pair(ni, nj));
                        }
                    }
                }
            }
        }
    }
    return cnt;
}

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    while(1){
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++) cin >> c[i][j];
        }
        int ans = solve();
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<ll,pll> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-11;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
const int DX[8]={-1,0,1,0,-1,-1,1,1},DY[8]={0,-1,0,1,-1,1,-1,1};

const int NPrime=3000000;
bool prime[NPrime+1];
vi EnumeratePrime(){
	fill(prime+2,prime+NPrime+1,true);
	vi a;
	for(int i=2;i<=NPrime;i++) if(prime[i]){
		a.emplace_back(i);
		for(int j=2*i;j<=NPrime;j+=i) prime[j]=0;
	}
	return a;
}

int h,w;
vvi a;

void dfs(int i,int j){
	a[i][j]=0;
	for(int k=0;k<8;k++){
		int x=i+DX[k],y=j+DY[k];
		if(x>=0&&x<h&&y>=0&&y<w&&a[x][y]) dfs(x,y);
	}
}

int main(){
	while(1){
		cin>>w>>h;
		if(h==0) break;
		a=vvi(h,vi(w));
		for(int i=0;i<h;i++) for(int j=0;j<w;j++) cin>>a[i][j];
		int res=0;
		for(int i=0;i<h;i++) for(int j=0;j<w;j++) if(a[i][j]){
			dfs(i,j);
			res++;
		}
		cout<<res<<endl;
	}
}

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

int c[50][50];
void dfs(int i, int j, int w, int h) {
  c[i][j] = 0;
  rep(k, 3) {
    int di = i - 1 + k;
    if (di < 0 || h <= di) continue;
    rep(l, 3) {
      int dj = j - 1 + l;
      if (dj < 0 || w <= dj) continue;
      if (di == i && dj == j) continue;
      if (c[di][dj]) dfs(di, dj, w, h);
    }
  }
}

int main() {
  vector<int> ans;
  while (1) {
    int w, h;
    cin >> w >> h;
    if (w + h == 0) break;
    // vector<vector<int> > c(w, vector<int>(h));
    rep(i, h) rep(j, w) cin >> c[i][j];
    // cout << endl;
    // rep(i, h) {
    //   rep(j, w) printf("%d ", c[i][j]);
    //   cout << endl;
    // }
    int cnt = 0;
    rep(i, h) rep(j, w) {
      if (c[i][j] == 0) continue;
      cnt++;
      dfs(i, j, w, h);
      // rep(i, h) {
      //   rep(j, w) printf("%d ", c[i][j]);
      //   cout << endl;
      // }
    }
    ans.push_back(cnt);
  }
  for (int a : ans) cout << a << endl;
  return 0;
}