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>
#include <list>
#include <set>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>

using namespace std;
using ll=long long;
using vi=vector<int>;
using vll=vector<ll>;
using pii=pair<int,int>;
using vs=vector<string>;
using vvi=vector<vi>;
using vpii=vector<pii>;
using si=set<int>;

#define REP(i,a) for(int i=0;i<a;i++)
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define rREP(i,a) for(int i=(a)-1;i>=0;i--)
#define rFOR(i,a,b) for(int i=(a)-1;i>=(b);i--)
#define out(S) cout<<(S)<<endl;
#define beginend(v) (v).begin(),(v).end()
#define IfOut(condition,text) if(condition){out(text);return 0;}
#define IfOutElse(condition,if_text,else_text) if(condition){out(if_text);return 0;}else{out(else_text);return 0;}
#define mod(s) ((s)%(ll)(1e9+7))
ll modpow(ll i, ll j) { ll tmp = 1; while (j) { if (j % 2)tmp = mod(tmp*i); i = mod(i*i); j /= 2; }return tmp; }
#define divmod(a,b) (mod(a*modpow((ll)b,(ll)(1e9+5))))
#define pb(i) push_back(i)
#define fst first
#define snd second
#define Foreach(item,collection) for(auto item:collection)
#define INF (int)1e10
#define removeAll(vec,deleg) auto itr=remove_if(beginend(vec),deleg);vec.erase(itr,vec.end());
void removeAt(string& s, int index) { s.erase(index, 1); }
template<typename T> void removeAt(vector<T>& v, int index) { v.erase(v.begin() + index); }
ll manhattanDistance(ll x1, ll y1, ll x2, ll y2) { return (abs(x2 - x1) + abs(y2 - y1)); }
vector<ll> sieveOfEratosthenes(int max) {ll prime;double sqrtmax = sqrt(max);vector<ll> primeVec, searchVec;FOR(i, 2, max + 1) searchVec.push_back(i);do{prime = searchVec[0];primeVec.push_back(prime);auto itr = remove_if(beginend(searchVec), [=](ll x)->bool {return x%prime == 0; });searchVec.erase(itr, searchVec.end());} while (prime < sqrtmax);primeVec.reserve(primeVec.size() + searchVec.size());primeVec.insert(primeVec.end(), beginend(searchVec));return primeVec;}
ll sum(vi v) {ll sum=0;Foreach(i,v){sum+=i;}return sum;}
#define ShowAll(v) Foreach(i,v) out(i);
bool isInnerIndex(int kx,int ky,int mx,int my){ return (kx>=0&&kx<mx&&ky>=0&&ky<my); }

int x,y;
vvi box;
int dx[]{0,1,0,-1,1,1,-1,-1};
int dy[]{1,0,-1,0,-1,1,-1,1};
ll s=0;
void dfs(int px,int py){
    box[py][px]=2;
    REP(i,8){
        int nx=px+dx[i];
        int ny=py+dy[i];
        if(isInnerIndex(nx,ny,x,y)){
            if(box[ny][nx]==1)dfs(nx,ny);
        }
    }
    return;
}

int main(){
    vi ans;
    while (cin>>x>>y,(x||y))
    {
        box.clear();
        box.resize(y);
        REP(i,y) REP(n,x) {
            int c;
            cin>>c;
            box[i].pb(c);
        }
        ll s=0;
        REP(i,y)REP(n,x){
            if(box[i][n]==1) {dfs(n,i);s++;}
        }
        ans.pb(s);
    }
    
    ShowAll(ans);
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>

int H,W;
int c[55][55];
vector<vector<int > > s(55,vector<int>(55));
bool seen[55][55];
int dx[] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};
void dfs(int y,int x,int a){
  if(x<0||W<=x||y<0||H<=y||c[y][x]==0){return;}
  if(seen[y][x]){return;}
  seen[y][x]=true;s[y][x]=a;
  for(int i=0;i<8;i++){
    dfs(y+dy[i],x+dx[i],a);
  }
}

int main() {
 while(cin>>W>>H){
  if(W==0&&H==0){return 0;}
 // vector<vector<int > > s(55,vector<int>(55));
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      s[i][j]=0;
    }
  }
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      cin>>c[i][j];
    }
  }
  for(int i=0;i<55;i++){
    for(int j=0;j<55;j++){
      seen[i][j]=false;
    }
  }
  int a=1;
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      if(c[i][j]){dfs(i,j,a);a++;}
    }
  }
  set<int>k;k.insert(0);
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      if(s[i][j]){k.insert(s[i][j]);}
    }
  }
  cout<<k.size()-1<<endl;
 }
  return 0;
}

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

using namespace std;
vector< vector<int> > map( 51 ,vector<int>(51,0));
int h,w;

void serch_island(int x,int y){
  map[y][x] = 0;
  const int dirX[] = {1 ,0, -1,  0, 1 , 1 , -1, -1};
  const int dirY[] = {0 ,1,  0, -1, 1 ,-1 ,  1, -1};
    for(int dir = 0; dir < 8; dir++){
      int nx = x + dirX[dir];
      int ny = y + dirY[dir];
      /*
      for(int i = 0; i < h ; i++){
        for(int j = 0; j < w; j++){
          cout << map[i][j] ;
        }
        cout << endl;
      }
      cout << endl;//*/
      if(nx >= 0 && nx < w &&
         ny >= 0 && ny < h &&
         map[ny][nx] == 1){
        serch_island(nx,ny);
      }
    }
    return ;
}

void solve(){
  int island = 0;

  for(int i = 0; i < h ; i++){
    for(int j = 0; j < w; j++){
      if(map[i][j] == 1){
        island++;
        serch_island(j, i);
      }
    }
  }
  cout << island << endl;
  map.clear();
  return ;
}
int main(){
  while(true){
    cin >> w >> h;
    if(h + w == 0) break;
    for(int i = 0; i < h ; i++){
      for(int j = 0; j < w; j++){
        cin >> map[i][j];
      }
    }
    solve();
  }
  return 0;
}