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 <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define P pair<int, int>
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
//#define int long long
const int MAX_HW = 55;

int H, W, v[MAX_HW][MAX_HW];
int my[] = {0, 0, 1, -1, 1, 1, -1, -1};
int mx[] = {1, -1, 0, 0, 1, -1, 1, -1};

void dfs(int y, int x){
  REP(i, 8){
    int ny = y + my[i], nx = x + mx[i];
    if(ny < 0 || nx < 0 || ny >= H || nx >= W || v[ny][nx] == 0) continue;
    v[ny][nx] = 0;
    dfs(ny, nx);
  }
}

int main() {
  while(cin >>W >>H && W){
    int ans = 0;
    REP(i, H) REP(j, W) cin >>v[i][j];
    REP(i, H){
      REP(j, W){
        if(v[i][j] == 1){
          ++ans;
          v[i][j] = 0;
          dfs(i, j);
        }
      }
    }
    cout <<ans <<endl;
  }
  return 0;
}

C++: #include <stdio.h>
int h, w;
void solve(int a[50][50],int x,int y){
	int i, j;
	a[y][x] = 0;
	for(i = y - 1;i <= y + 1;i++){
		for(j = x - 1;j <= x + 1;j++)
			if(0 <= i && i < h && 0 <= j && j < w && a[i][j]) solve(a,j,i);
	}
	return;
}

int main(void){
	int i, j, flag[50][50], count;
	scanf("%d%d",&w,&h);
	while(w != 0 || h != 0){
		for(i = 0;i < h;i++)
			for(j = 0;j < w;j++) scanf("%d",&flag[i][j]);
		count = 0;
		for(i = 0;i < h;i++){
			for(j = 0;j < w;j++){
				if(flag[i][j]){
					count++;
					solve(flag,j,i);
				}
			}
		}
		printf("%d\n",count);
		scanf("%d%d",&w,&h);
	}
	return 0;
}

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

int H, W, C[50][50];

void dfs(int a, int b) {
    C[a][b] = 0;
    for (int i = -1; i <= 1; ++i) {
        for (int j = -1; j <= 1; ++j) {
            int na = a + i, nb = b + j;
            if (na >= 0 && na < H && nb >= 0 && nb < W && C[na][nb] == 1) dfs(na, nb);
        }
    }
    return;
}

int main() {
    while (true) {
        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 = 0;
        for (int i = 0; i < H; ++i) {
            for (int j = 0; j < W; ++j) {
                if (C[i][j]) {
                    dfs(i, j);
                    ++ans;
                }
            }
        }

        cout << ans << endl;
    }
    
    return 0;
}

C++: #include<cstdio>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
 
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1001001001
#define LLINF 1001001001001001001
#define mp make_pair
#define LLIandI pair<long long int , int>
#define ll long long

int main(){
    while(1){
    int W,H;
    scanf("%d %d\n",&W,&H);
    if(W==0)break;
    int c[100][100];//W,H<=50
    bool f[100][100];
    rep(i,H){
        rep(j,W){
            scanf("%d",&c[i][j]);
            f[i][j]=false;
        }
    }
    queue< pair<int,int> > q;
    int count=0;
    rep(i,H)rep(j,W){
        if(f[i][j])continue;
        if(c[i][j]==0)continue;
        count++;
        //printf("x=%d y=%d",i,j);
        q.push( mp(i,j) );
        while(!q.empty()){
            pair<int,int> p=q.front();
            q.pop();
            int py=p.first;int px=p.second;
            if(f[py][px])continue;
            if(c[py][px]==0)continue;
            f[py][px]=true;
            if(py>0)q.push( mp(py-1,px) );
            if(py<H-1)q.push( mp(py+1,px) );
            if(px>0)q.push( mp(py,px-1) );
            if(px<W-1)q.push( mp(py,px+1) );
            if(py>0&&px>0)q.push( mp(py-1,px-1));
            if(py>0&&px<W-1)q.push( mp(py-1,px+1));
            if(py<H-1&&px>0)q.push( mp(py+1,px-1));
            if(py<H-1&&px<W-1)q.push( mp(py+1,px+1));

        }
    }
    
    printf("%d\n",count);
    }
}