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<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

int sea[60][60];
int w,h;

void f(int x,int y){
//cout<<x<<" "<<y<<endl;
	int i,j;
/*for(i=0;i<h;i++){
	for(j=0;j<w;j++){
		cout<<sea[i][j]<<" ";
	}
	cout<<endl;
}*/
	sea[y][x]=0;
	for(i=-1;i<2;i++){
		for(j=-1;j<2;j++){
			if(y+i<0||h<=y+i||x+j<0||w<=x+j)continue;
			if(sea[y+i][x+j]==1)f(x+j,y+i);
		}
	}
}

int main() {
	while(w=in(),h=in()){
		int i,j;
		for(i=0;i<h;i++){
			for(j=0;j<w;j++){
				cin>>sea[i][j];
			}
		}
		/*for(i=0;i<h;i++){
			for(j=0;j<w;j++){
				cout<<sea[i][j]<<" ";
			}
			cout<<endl;
		}*/
		int res=0;
		for(i=0;i<h;i++){
			for(j=0;j<w;j++){
				if(sea[i][j]==1){
					f(j,i);
					res++;
				}
			}
		}
		cout<<res<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;
int needx[]={-1,0,1,0,1,1,-1,-1};
int needy[]={0,1,0,-1,1,-1,1,-1};
//テ・ツウツカテ」ツつ津・ツ?ィテ」ツ?ヲfalseテ」ツ?ォテ・ツ、ツ嘉」ツ?暗」ツ?ヲtrueテ」ツつ津ィツソツ氾」ツ?凖ゥツ鳴「テヲツ閉ー//
bool land(int x,int y,vector<vector<bool> >& cango){
    if(cango[x][y]){
        cango[x][y]=false;
        for(int i=0;i<8;i++){
            int nextx=x+needx[i];
            int nexty=y+needy[i];
            if(cango[nextx][nexty])
                bool flag=land(nextx,nexty,cango);
            }
    return true;
     }else{
return false;
    }
}
int main(void){
    int input;
	int w,h;
	while(1){
cin>>w>>h;
if(h==0)break;
    vector<vector<bool> > cango(h+2,vector<bool>(w+2,false));
    for(int i=1;i<=h;i++){
        for(int j=1;j<=w;j++){
            cin >>input;
            if(input==1){
cango[i][j]=true;
            }
        }
    }
  
    int t=0;
for(int i=1;i<=h;i++){
    for(int j=1;j<=w;j++){
        if(land(i,j,cango)){
            t++;
        }
    }
}
cout <<t<<endl;
	}
return 0;
}

C++: #include <iostream>
#include <queue>
#include <utility>

using namespace std;
using Point = pair<int,int>;

int map[52][52];

void BFS(const Point &p0){
  queue<Point> q;
  q.push(p0);
  map[p0.first][p0.second] = 0;
  while(!q.empty()){
    auto p = q.front();q.pop();
    int y = p.first , x = p.second;

    for(int i=-1;i<=1;++i){
      for(int j=-1;j<=1;++j){
        if(i==0&&j==0)continue;
        if(map[i+y][j+x]){
          q.push(Point(i+y,j+x));
          map[i+y][j+x] = 0;
        }
      }
    }

  }

}

int main(){
  int w,h;
  while(cin>>w>>h&&w>0&&h>0){

    for(int i=1;i<h+1;++i){
      for(int j=1;j<w+1;++j){
        cin >> map[i][j];
      }
    }


    int num = 0;
    for(int i=1;i<h+1;++i){
      for(int j=1;j<w+1;++j){
        if(map[i][j]){
          BFS(Point(i,j));
          num++;
        }
      }
    }
    cout << num << endl;
  }

  return 0;
}

C++: #include<bits/stdc++.h>

#define rep(i,n) for(int i = 0; i < (n); i++)
#define rrep(i,n) for(int i = (n)-1; i >= 0; i--)
#define rep1(i,n) for(int i = 1; i <= (n); i++)
#define rrep1(i,n) for(int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

#define MAX_LEN 50
int board[MAX_LEN][MAX_LEN];
bool used[MAX_LEN][MAX_LEN];
int w,h;

bool isin(int x, int y){
  return (x>=0 && x<w && y>=0 && y<h);
}

void dfs(int x, int y){
  if( !isin(x, y) )return;
  if(used[y][x])return;
  if(board[y][x]==0)return;
  used[y][x]=true;

  dfs(x-1, y);
  dfs(x+1, y);
  dfs(x, y-1);
  dfs(x, y+1);

  dfs(x-1, y-1);
  dfs(x-1, y+1);
  dfs(x+1, y-1);
  dfs(x+1, y+1);
}

int main(){
  while(true){
    cin>>w>>h;
    if(w==0 && h==0)break;
    rep(i, MAX_LEN)rep(j, MAX_LEN){
      board[i][j]=0;
      used[i][j]=false;
    }

    rep(i, h)rep(j, w)cin>>board[i][j];

    int result=0;
    rep(i, h)rep(j, w){
      if(board[i][j]==1 && !used[i][j])result++;
      dfs(j, i);
    }

    // rep(i, h){
    //   rep(j, w)cout<<board[i][j];
    //   cout<<endl;
    // }

    cout<<result<<endl;
  }
  return 0;
}