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<iomanip>
#include<cstdlib>
#include<math.h>
#include<algorithm>
#include<string>
#include<vector>
#include<list>
#include<queue>
#include<stack>
#include<deque>
#include<set>
#include<map>
#define REP(i, N) for(ll i = 0; i < N; ++i)
#define FOR(i, a, b) for(ll i = a; i < b; ++i)
#define ALL(a) (a).begin(),(a).end()
#define pb push_back
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int qx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int qy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

vector<vector<int> > mp;
int w, h;


void hoge(int wo, int ho) {
	mp[wo][ho] = 0;
	REP(i, 8) {
		int nw = wo + qx[i];
		int nh = ho + qy[i];
		if(nw >= 0 && nw < w && nh >= 0 && nh < h && mp[nw][nh] == 1) hoge(nw, nh);
	}
}
int main(void) {
	while(true) {
	ll count = 0;
	cin>>w>>h;
		if(w == 0 && h == 0) return 0;
		swap(w, h);
	mp.resize(w);
	REP(i, w) mp[i].resize(h);
	REP(i, w) {
		REP(j, h) {
			cin>> mp[i][j];
		}
	}
	REP(i, w) {
		REP(j, h) {
			if(mp[i][j] == 1) {
				count++;
				hoge(i, j);
			}
		}
	}
	cout<<count<<endl;
}
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long L;
L X[]={0,1,1,1,0,-1,-1,-1};
L Y[]={1,1,0,-1,-1,-1,0,1};
L h,w,mark[51][51],c=0;
L grid[51][51];
void call(L x,L y){
    mark[x][y]=1;
    for(L i=0;i<8;i++){
        L xx=X[i]+x,yy=Y[i]+y;
        if((xx>=0&&xx<h &&yy >=0&&yy<w) && grid[xx][yy] &&mark[xx][yy]==0){
            call(xx,yy);
        }
    }
}
int main()
{
    while(scanf("%lld%lld",&w,&h)&& w||h ){
        for(L i=0;i<h;i++){
            for(L j=0;j<w;j++)scanf("%lld",&grid[i][j]);
        }
        for(L i=0;i<h;i++){
            for(L j=0;j<w;j++){
                if(!mark[i][j] && grid[i][j]){
                    c++;
                    call(i,j);
                }
            }
        }
        printf("%lld\n",c);c=0;
        memset(mark,0,sizeof(mark));
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
using Graph = vector<vector<int>>;

int H, W;
vector<int> f1{-1, -1, -1, 0, 0, 0, 1, 1, 1};
vector<int> f2{-1, 0, 1, -1, 0, 1, -1, 0, 1};
void dfs(Graph &G, int h, int w){
  G[h][w] = -1;
  rep(i, 9){
    int nh = h + f1[i];
    int nw = w + f2[i];
    if (nh < 0 || nw < 0 || nh >= H || nw >= W) continue;
    if (G[nh][nw] == -1 || G[nh][nw] == 0) continue;
    dfs(G, nh, nw);
  }
}

int main(){
  while(1){
    cin >> W >> H;
    if (H == 0 && W == 0) break;
    Graph G(H, vector<int>(W));
    rep(i, H)rep(j, W) cin >> G[i][j];
    int count = 0;
    rep(i, H)rep(j, W){
      if (G[i][j] == -1 || G[i][j] == 0) continue;
      dfs(G, i, j);
      count++;
    }
    cout << count << endl;
  }
  
  return 0;
}

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

using std::cin;
using std::cout;
using std::endl;

int main(void) {
	int w,h;
	cin >> w;
	cin >> h;

	while (w!=0||h!=0) {
		int count = 0;
		std::stack<int> st_x;
		std::stack<int> st_y;
		int xn,yn;
		int c;
		bool all = false;
		std::vector< std::vector<bool> > island(w+2, std::vector<bool>(h+2));
		for (int i = 0; i < h+2; i++) {
			for (int j = 0; j < w+2; j++) {
				if (i==0||i==h+1||j==0||j==w+1) {
					island[i][j] = false;
				} else {
					cin >> c;
					if (c==1) {
						island[i][j] = true;
						st_x.push(j);
						st_y.push(i);
					} else {
						island[i][j] = false;
					}
				}
			}
		}

		while (!st_x.empty()) {
			xn = st_x.top();
			yn = st_y.top();
			st_x.pop();
			st_y.pop();
			if (island[xn][yn]) {
				std::stack<int> st_xn,st_yn;
				island[xn][yn] = false;
				count++;
				st_xn.push(xn);
				st_yn.push(yn);
				while (!st_xn.empty()) {
					xn = st_xn.top();
					yn = st_yn.top();
					st_xn.pop();
					st_yn.pop();
					if (island[xn+1][yn]) {
						st_xn.push(xn+1);
						st_yn.push(yn);
						island[xn+1][yn] = false;
					}
					if (island[xn-1][yn]) {
						st_xn.push(xn-1);
						st_yn.push(yn);
						island[xn-1][yn] = false;
					}
					if (island[xn+1][yn+1]) {
						st_xn.push(xn+1);
						st_yn.push(yn+1);
						island[xn+1][yn+1] = false;
					}
					if (island[xn+1][yn-1]) {
						st_xn.push(xn+1);
						st_yn.push(yn-1);
						island[xn+1][yn-1] = false;
					}
					if (island[xn-1][yn+1]) {
						st_xn.push(xn-1);
						st_yn.push(yn+1);
						island[xn-1][yn+1] = false;
					}
					if (island[xn-1][yn-1]) {
						st_xn.push(xn-1);
						st_yn.push(yn-1);
						island[xn-1][yn-1] = false;
					}
					if (island[xn][yn+1]) {
						st_xn.push(xn);
						st_yn.push(yn+1);
						island[xn][yn+1] = false;
					}
					if (island[xn][yn-1]) {
						st_xn.push(xn);
						st_yn.push(yn-1);
						island[xn][yn-1] = false;
					}
				}
			}
		}
		cout << count << endl;

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