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 <cstdio>
#include <algorithm>

using namespace std;

int field[64][64];
int cnt;
int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int w, h;

void dfs(int x, int y);

int main()
{
	int x, y;

	while (true){
		cnt = 0;

		scanf("%d %d", &w, &h);
		if (w == 0){
			break;
		}

		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				scanf("%d", &field[i][j]);
			}
		}

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

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

	return (0);
}

void dfs(int x, int y)
{
	field[y][x] = 0;

	for (int i = 0; i < 8; i++){
		int nx = x + dx[i];
		int ny = y + dy[i];

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

C++: #include <iostream>
#define R 1
#define U 0
using namespace std;
int w,h;
int c[60][60];

void rec(int x,int y){
  if( x<0 || x>=h ) return;
  if( y<0 || y>=w ) return;
  if( c[x][y]==U )  return;

  c[x][y]=U;
  for( int i=-1;i<=1;i++ ){
    for( int j=-1;j<=1;j++ ){
      if( i==j && i==0 ) continue;
      rec( x+i,y+j );
    }
  }
}

int main(){
  while( cin>>w>>h && (w|h) ){
    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]==R ){
	  ans++;
	  rec( i,j );
	}
      }
    }
    cout << ans << endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> P3;
typedef pair<P ,P> PP;
constexpr ll MOD = ll(1e9) + 7;
constexpr int IINF = INT_MAX;
constexpr ll LLINF = LLONG_MAX;
constexpr int MAX_N = int(2e5) + 5;
constexpr double EPS = 1e-9;
constexpr int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i > 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), (v).end()

int h, w, field[55][55];

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, nj = j + dj;
            if(ni < 0 || ni >= h) continue;
            if(nj < 0 || nj >= w) continue;
            if(field[ni][nj] == 1){
                dfs(ni, nj);
            }
        }
    }
}

int main() {
    while(cin >> w >> h, w){
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin >> field[i][j];
            }
        }
        int ans = 0;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(field[i][j] == 1){
                    dfs(i,j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <set>
#include <algorithm>
using namespace std;
int group[55][55];
int parent(int n){
	int i=n/55,j=n%55;
	if(group[i][j]==-1)	return -1;
	return group[i][j]=(group[i][j]==n?group[i][j]:parent(group[i][j]));
}
void merge(int s,int t){
	int si=s/55,sj=s%55;
	s=parent(group[si][sj]);
	int ti=t/55,tj=t%55;
	t=parent(group[ti][tj]);
	group[t/55][t%55]=s;
}
bool isInside(int i,int j,int w,int h){
	if(i<0||h<=i)	return false;
	if(j<0||w<=j)	return false;
	return true;
}
const int dx[]={1,0,-1};
const int dy[]={1,0,-1};
int main(){
	while(true){
		int w,h;	cin>>w>>h;
		if(w==0)	break;
		for(int i=0;i<h;i++)	for(int j=0;j<w;j++)	group[i][j]=-1;
		for(int i=0;i<h;i++)	for(int j=0;j<w;j++){
			int c;	cin>>c;
			if(c==1)	group[i][j]=55*i+j;
		}
		for(int i=0;i<h;i++)	for(int j=0;j<w;j++){
			if(group[i][j]>=0){
				for(int x=0;x<3;x++)	for(int y=0;y<3;y++){
					int ni=i+dy[y],nj=j+dx[x];
					if(!isInside(ni,nj,w,h))	continue;
					if(group[ni][nj]>=0)	merge(55*i+j,55*ni+nj);
				}
			}
		}
		set<int> ret;
		for(int i=0;i<h;i++)	for(int j=0;j<w;j++)	ret.insert(parent(group[i][j]));
		ret.erase(-1);
		cout<<ret.size()<<endl;
	}
	return 0;
}

C++: #include <utility>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
using namespace std;
int cnt;
int t[3]={-1,0,1};
    int map[55][55];
void dfs(int h,int w){
    if(map[h][w]==1){
        map[h][w]=0;
        for(int i=0;i<3;i++){for(int j=0;j<3;j++)dfs(h+t[i],w+t[j]);}
       }
}
int main()
{
    int w,h;

    while(cin>>w>>h,w+h){cnt=0;
        for(int i=0;i<55;i++){for(int j=0;j<55;j++)map[i][j]=0;}
        for(int i=0;i<h;i++){for(int j=0;j<w;j++)scanf("%d",&map[i+1][j+1]);}
        for(int i=0;i<53;i++){for(int j=0;j<53;j++){
            if(map[i][j]==1)cnt++;
            dfs(i,j);}}
        cout<<cnt<<endl;}
    return 0;
}