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>
using namespace std;
struct Point
{
	int x;
	int y;
	Point(int x = 0, int y = 0) :x(x), y(y) {}
};
const int MAX = 50;
int map[MAX][MAX];
bool book[MAX][MAX];
void depthFSearch(int h, int w, Point start)
{
	int next[9][2] = { { -1,-1 },{ -1,0 },{ -1,1 },{ 0,-1 },{ 0,0 },{ 0,1 },{ 1,-1 },{ 1,0 },{ 1,1 } };
	for (int k = 0; k <= 8; k++)
	{
		int nx = start.x + next[k][0];
		int ny = start.y + next[k][1];
		if (nx < 0 || nx >= h || ny < 0 || ny >= w || !map[nx][ny])
			continue;
		if (!book[nx][ny] && map[nx][ny])
		{
			book[nx][ny] = true;
			Point p = { nx,ny };
			depthFSearch(h, w, p);
		}
	}
	return;
}
int main()
{
	while (true)
	{
		int w, h;
		cin >> w >> h;
		if (!w && !h)
			break;
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
			{
				cin >> map[i][j];
				book[i][j] = false;
			}
		int sum = 0;
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
			{
				if (!book[i][j] && map[i][j])
				{
					book[i][j] = true;
					Point start(i, j);
					depthFSearch(h, w, start);
					sum++;
				}
			}
		cout << sum << endl;
	}
}

C++: #ifndef _WIN32
#include<iostream>
#endif

using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>

#define FOR(i,bg,ed) for(int i=(bg);i<(ed);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

typedef vector<int> V;
typedef vector<V> VV;

VV wrap(VV vs,int c) {
	int R = vs.size();
	int C = vs[0].size();
	VV ret(R + 2, V(C + 2, c));
	REP(i, R)REP(j, C)ret[i + 1][j + 1] = vs[i][j];
	return ret;
}
const int dr[] = { 0,0,-1,1 ,1,1,-1,-1};
const int dc[] = { -1,1,0,0, -1,1,-1,1};
VV g;
void dfs(int r,int c) {
	if (g[r][c] == 0)return;
	//cout << "(" << r << "," << c << ")";
	g[r][c] = 0;
	REP(i, 8) {
		int nr = r + dr[i];
		int nc = c + dc[i];
		dfs(nr, nc);
	}
}
int main() {
	int R, C;
	while (cin >> C >> R, C + R) {
		VV in(R,V(C));
		REP(i, R)REP(j,C)cin >> in[i][j];
		g = wrap(in,0);
		int res = 0;
		REP(i, R)REP(j, C) {
			if (g[i + 1][j + 1]) {
				res++;
				dfs(i + 1, j + 1);// cout << endl;
			}
		}

		cout << res << endl;
	}


}

C++: #define rep(i, n) for(int i = 0; i < n; i++)
#define ck(a, b) (0<=a && a<b)
using namespace std;
int dx[]={-1,0,1,-1,1,-1,0,1}, dy[]={-1,-1,-1,0,0,1,1,1};
int w, h; int il[50][50];

int main(){
	while(cin>>w>>h, w||h){
		int c = 0;
		rep(y, h)rep(x, w)cin>>il[y][x];
		rep(y, h)rep(x, w){
			if(il[y][x]){
				int cked[50][50] = {0};
				cked[y][x] = 1;
				bool finish = 0;
				while(!finish){
					finish = 1;
					rep(i, h)rep(j, w){
						if(cked[i][j]){
							rep(d, 8){
								if(ck(j+dx[d],w) && ck(i+dy[d],h) &&
									il[i+dy[d]][j+dx[d]] &&
									!cked[i+dy[d]][j+dx[d]]
								){
									cked[i+dy[d]][j+dx[d]] = 1; finish = 0;
								}
							}
						}
					}
				}
				c++;
				rep(i, h)rep(j, w)if(cked[i][j])il[i][j]=0;
			}
		}
		cout<<c<<endl;
	}
	return 0;
}

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

int map[50][50];
int w,h,label;

void check(int x,int y,int lnum){
  map[y][x]=lnum;
  if (x>0){
    if (map[y][x-1]==1) check(x-1,y,lnum);
    if (y>0){
      if (map[y-1][x-1]==1) check(x-1,y-1,lnum);
    }
    if (y<h-1){
      if (map[y+1][x-1]==1) check(x-1,y+1,lnum);
    }
  }
  if(x<w-1){
    if (map[y][x+1]==1) check(x+1,y,lnum);
    if (y>0){
      if (map[y-1][x+1]==1) check(x+1,y-1,lnum);
    }
    if (y<h-1){
      if (map[y+1][x+1]==1) check(x+1,y+1,lnum);
    }
  }
  if (y>0){
    if (map[y-1][x]==1) check(x,y-1,lnum);
  }
  if (y<h-1){
    if (map[y+1][x]==1) check(x,y+1,lnum);
  }
}

int main(){
  cin >> w >> h;
  while (w!=0||h!=0){
    label=2;
    for (int i=0;i<h;i++)
      for (int j=0;j<w;j++)
	cin >> map[i][j];

    for (int i=0;i<h;i++){
      for (int j=0;j<w;j++){
	if (map[i][j]==1){
	  //cout << "(" << map[i][j] << ")";
	  check(j,i,label++);
	}
      }
    }
    //cout << "(" << label << ")";

    cout << (label-2) << endl;

    cin >> w >> h;
 }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>

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

using namespace std;
int field[50][50];
int w, h;

void dfs(int i, int j){
    field[i][j] = 0;
    for(int di=-1; di<=1; di++){
        for(int dj=-1; dj<=1; dj++){
            int ni = i+di;
            int nj = j+dj;
            if(field[ni][nj]==1 && 0 <= ni && ni < h &&
                    0 <= nj && nj < w){
                dfs(ni,nj);
            }
        }
    }
    return;
}

int main(){
    while(cin >> w >> h){
        if(w==0 && h==0) break;
        REP(i,h){
            REP(j,w){
                cin >> field[i][j];
            }
        }

        int cnt = 0;
        REP(i,h){
            REP(j,w){
                if(field[i][j] == 1){
                    dfs(i,j);
                    cnt++;
                }
            }
        }
        cout << cnt << endl;
    }

    return 0;
}