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 <map>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <queue>
#define INF 1e9+7
#define rep(i,n) for(int i=0;i<n;i++)
#define NO cout<<"NO"<<endl;
#define YES cout << "YES"<<endl;
#define No cout << "No"<<endl;
#define Yes cout << "Yes"<<endl;
#define all(a) a.begin(),a.end()
#define P pair<int,int>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

//(int)'a'は97 (int)'A'は65 (int)'1'は49
//おまじない

int main(){
    
    while(true){
        int h,w;
        int m[50][50];
        bool used[50][50]={0};
        int dx[]={-1,0,1};
        int dy[]={-1,0,1};
        int ans=0;
        
        cin >> w>>h;
        if(h==0&&w==0)break;
        
        rep(i,h){
            rep(j,w){
                cin >> m[i][j];
            }
        }
        
        rep(i,h){
            rep(j,w){
                if(m[i][j]==1&&!used[i][j]){
                    ans++;
                    queue<P> que;
                    que.push(P(i,j));
                    while(!que.empty()){
                        P p=que.front();que.pop();
                        int x = p.second,y=p.first;
                        rep(a,3){
                            rep(b,3){
                                int nx=x+dx[a],ny=y+dy[b];
                                if(nx>=0&&nx<w&&ny>=0&&ny<h&&m[ny][nx]==1&&!used[ny][nx]){
                                    que.push(P(ny,nx));
                                    used[ny][nx]=true;
                                }
                            }
                        }
                    }
                }
            }
        }
        cout << ans<<endl;
    }
    
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, e) for (int i = (int)(s); i <= (int)(e); i++)
#define printYesNo(is_ok) puts(is_ok ? "Yes" : "No");
#define SORT(v) sort(v.begin(), v.end());
#define RSORT(v) sort(v.rbegin(), v.rend());
#define REVERSE(v) reverse(v.begin(), v.end());


int ans = 0;
vector<vector<int> > island_map;
void dfs(vector<vector<int> > field, pair<int, int> init_pos)
{
  int dirx[] = {1, 1, 1, 0, 0, -1, -1, -1};
  int diry[] = {1, 0, -1, 1, -1, 1, 0, -1};

  int h = field.size();
  int w = field[0].size();
  stack<pair<int, int> > pos_stack;
  pos_stack.push(init_pos);

  while (pos_stack.size())
  {
    pair<int, int> pos = pos_stack.top();
    pos_stack.pop();
    int pos_x = pos.first;
    int pos_y = pos.second;
    rep(i, 8)
    {
      int next_x = pos_x + dirx[i];
      int next_y = pos_y + diry[i];

      if (0 <= next_x && next_x < h && 0 <= next_y && next_y < w && island_map[next_x][next_y] == 0 && field[next_x][next_y] == 1)
      {
        island_map[next_x][next_y] = ans;
        pos_stack.push(make_pair(next_x, next_y));
      }
    }
  }
}

int main()
{
  while(true)
  {
    int w, h;
    cin >> w >> h;
    if(w == 0 && h == 0)
      return 0;

    ans = 0;    
    vector<vector<int> > field(h, vector<int>(w, 0));
    island_map = vector<vector<int> >(h, vector<int>(w, 0));
    rep(i, h) rep(j, w)
    {
      cin >> field[i][j];
    }

    rep(i, h) rep(j, w)
    {
      if(island_map[i][j] == 0 && field[i][j] == 1)
      {
        ans++;
        dfs(field, make_pair(i, j));
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;

#define rep(i,N) for(int i=0;i<(int)(N);++i)
#define rrep(i,N) for(int i=(int)(N)-1;i>=0;--i)
#define debug(x) cout<<#x<<"="<<x<<endl;
constexpr ll MOD=1000000007;
constexpr ll INF=1LL<<60;
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;}
template<typename T> void fail(T v){cout << v << endl;exit(0);}
//template end
int W,H;
bool inside(int h,int w){return 0<=h&&h<H&&0<=w&&w<W;}
int gh[]={1,-1,0,0,1,1,-1,-1},gw[]={0,0,1,-1,-1,1,-1,1},use[51][51],G[51][51];
void dfs(int h,int w){
    rep(k,8){
        int i=gh[k]+h,j=gw[k]+w;
        if(!inside(i,j)||use[i][j]||G[i][j]==0)continue;
        use[i][j]=1;
        dfs(i,j);
    }
}
void solve(){
    while((cin>>W>>H,W|H)){
        rep(i,51)rep(j,51)use[i][j]=0;
        rep(i,H)rep(j,W)cin>>G[i][j];
        int res=0;
        rep(i,H)rep(j,W)if(!use[i][j]&&G[i][j]){dfs(i,j);res++;}
        cout<<res<<endl;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    solve();
    return 0;
}