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 <vector>
using namespace std;

const int dx2[] = {1,1,1,0,0,0,-1,-1,-1};
const int dy2[] = {1,0,-1,1,0,-1,1,0,-1};

void go(vector<vector<int> > &F,int h,int w){
    if(F[h][w]==0) return;
    F[h][w] = 0;
    for(int i=0;i<9;i++){
        go(F,h+dy2[i],w+dx2[i]);
    }
}
int main(){
    while(true){
        int w,h;
        cin >> w >> h;
        if(w==0) break;
        vector<vector<int> > F(h+2,vector<int>(w+2,0));
        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                cin >> F[i][j];
            }
        }
        int c=0;
        for(int i=0;i<h+2;i++){
            for(int j=0;j<w+2;j++){
                if(F[i][j] == 1){
                    go(F,i,j);
                    c++;
                }
            }
        }
        cout << c << endl;
    }
    return 0;
}

C++: #include<stdio.h>
void printmap();
void draw(int x,int y);
int w,h,c[51][51];

int main(){
	int i,j;
	int count;


	scanf("%d %d",&w,&h);


	while((w!=0)&&(h!=0)){
		count=0;
	
		for(j=0;j<h;j++){
			for(i=0;i<w;i++){
				scanf("%d",&c[j][i]);
			}
		}
		//printf("\n");
		
		for(i=0;i<w;i++){
			c[h][i]=0;
		}
		for(i=0;i<h;i++){
			c[i][w]=0;
		}

		for(i=0;i<w;i++){
			for(j=0;j<h;j++){
				if(c[j][i]==1){
					count++;
					//printf("count up count=%d\n",count);
					draw(j,i);
					//printmap();
				}
			}
		}

		printf("%d\n",count);
		scanf("%d %d",&w,&h);
	}
	return 0;
}

void draw(int x,int y){
	//printf("delete %d %d\n",x,y);
	int i,j;
	c[x][y]=0;
	for(i=0;i<3;i++){
		for(j=0;j<3;j++){
			if(c[x-1+i][y-1+j]==1){
				draw(x-1+i,y-1+j);
			}
		}
	}


}

void printmap(){
	int i,j;
	for(j=0;j<h;j++){
		for(i=0;i<w;i++){
			printf("%d ",c[j][i]);
		}
		printf("\n");
	}
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <stack>
#include <algorithm>
using namespace std;

typedef pair<int,int> P;
int F[50][50];
int x[8]={1,1,1,0,0,-1,-1,-1};
int y[8]={1,0,-1,1,-1,1,0,-1};
int h,w;

void dfs(int j, int i){
  stack<P> S;
  S.push(P{j,i});
  F[i][j]=0;
  while(!S.empty())
  {
    P p=S.top(); S.pop();
    for(int i=0;i<8;i++)
    {
      int nx=p.first+x[i], ny=p.second+y[i];
      if(nx>=0 && nx<w && ny>=0 && ny<h && F[ny][nx]==1)
      {
        F[ny][nx]=0;
        S.push(P{nx,ny});
      }
    }
  }
}
int island(int w,int h){
  int ans=0;
  for(int i=0;i<h;i++)
  {
    for(int j=0;j<w;j++)
    {
      if(F[i][j]==1)
      {
        ans++;
        dfs(j,i);
      }
    }
  }
  return ans;
}

int main(){
    cin>>w>>h;
    while(true){
      if(w==0 && h==0) break;
      for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
          cin>>F[i][j];
        }
      }
      cout<<island(w,h)<<"\n";
      cin>>w>>h;
    }

}

C++: #include<iostream>
#include<bitset>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
ll INF = 1 << 30;


int rel[12][12];
vector<vector<int>> mp;
vector<vector<int>> vst;
int mx, my;

int adj(int x, int m) {

	if (x < 0) return 0;
	if (x >= m) return m - 1;

	return x;
}

void dfs(int x, int y) {
	int d[] = { -1,0,1 };
	int nx, ny;

	if (vst[y][x] == 1) return;

	vst[y][x] = 1;

	rep(j, 3) {
		rep(i, 3) {
			nx = adj(x + d[i], mx);
			ny = adj(y + d[j], my);
			if (mp[ny][nx] == 1 && vst[ny][nx] == 0) {
				dfs(nx, ny);
			}
		}
	}
}

int islCnt() {

	int f = 0;

	rep(i, my) {
		rep(j, mx) {

			if (mp[i][j] == 1 && vst[i][j] == 0) {
				f++;
				dfs(j, i);
			}

		}
	}

	return f;
}


int main() {


	int n, m, x, y, ans;
	ans = 0;

	while (true) {
		cin >> n >> m;

		if (n == 0 && m == 0) break;

		mp = vector<vector<int>>(m);
		vst = vector<vector<int>>(m);

		rep(i, m) {
			rep(j, n) {
				cin >> x;
				mp[i].push_back(x);
				vst[i].push_back(0);

			}
		}

		mx = mp[0].size();
		my = mp.size();

		ans = islCnt();

		cout << ans << endl;
	}

}