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 "bits/stdc++.h"

using namespace std;

typedef long long ll;

const ll INF=1e13+7;
const int dx[]={0,1,0,-1,1,-1,-1,1};
const int dy[]={1,0,-1,0,1,1,-1,-1};

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 <class T, class Y>T GCD(T a, Y b){if(a<b){int c=a;a=b;b=c;}if (a % b == 0){return b;}return GCD(b, a % b);}
#define rep(i,n) for(int i=0;i<(n);i++)

int Place[60][60];
bool seen[60][60];
int W,H;

void DFS(int h,int w){
    seen[h][w]=true;

    for(int i=0;i<8;i++){
        int nw=w+dx[i];
        int nh=h+dy[i];

        if(nw<0||nw>=W||nh<0||nh>=H)continue;
        if(Place[nh][nw]==0)continue;

        if(seen[nh][nw]==true)continue;

        DFS(nh,nw);
    }
}

int Input(){
    int Co=0;
    cin>>W>>H;
    if(W==0&&H==0) return -1;
    rep(i,H){
        rep(j,W)cin>>Place[i][j];
    }
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            if(Place[i][j]==1&&seen[i][j]==false){
                Co++;
                DFS(i,j);
            }
        }
    }
    return Co;
}

int main(){
    vector<int> answers(0);
    int ans=0,num=0;
    while(1){
        memset(seen,false,sizeof(seen));
        ans=Input();
        if(ans==-1)break;
        answers.push_back(ans);
        num++;
    }
    rep(i,num)cout<<answers[i]<<endl;
    return 0;
}

//a=97,z=122

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

int W,H, grid[50][50];

int dfs(int sy, int sx){
	if(grid[sy][sx]==0)return 0;

	stack<pair<int,int>> S;
	S.push( {sy,sx} );

	while(S.size()){
		pair<int,int> u=S.top();	S.pop();
		grid[u.first][u.second]=0;

		for(int dy=-1; dy<=1; dy++){
			for(int dx=-1; dx<=1; dx++){
				int nY=u.first+dy, nX=u.second+dx;
				if(nY<0||nX<0||nY>=H||nX>=W)continue;
				else if(grid[nY][nX]==0)continue;

				S.push( {nY,nX} );
			}
		}
	}
	return 1;
}

int main(){
	while((cin>>W>>H)&&W&&H){
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				cin>>grid[i][j];
			}
		}

		int ans=0;
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				ans+=dfs(i,j);
			}
		}
		cout<<ans<<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 FOR(i,m,n) for(int i=m;i<n;++i)
#define RFOR(i,m,n) for(int i=m-1;i>=n;--i)
#define ALL(v) v.begin(),v.end()
#define PB(a) push_back(a)
#define INF 1000000001
#define MOD 1000000007

const int dx[8] = { 1,1,0,-1,-1,-1,0,1 };
const int dy[8] = { 0,1,1,1,0,-1,-1,-1 };

using namespace std;

void dfs(vector<vector<int> > &v,int x,int y) {
	v[x][y] = 0;
	REP(i, 8) {
		int nx = x + dx[i], ny = y+dy[i];
		if (nx >= 0 && ny >= 0 && nx < v.size() && ny < v[0].size() && v[nx][ny] == 1) {

			dfs(v, nx, ny);
		}
	}
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	while (1) {
		int w, h, cnt = 0;
		cin >> w >> h;
		if (!w) break;
		vector<int> dm(w);
		vector<vector<int> > v(h, dm);
		REP(i, h)REP(j, w) cin >> v[i][j];
		REP(i, h)REP(j, w)
			if (v[i][j] == 1) {
				dfs(v, i, j);
				cnt++;
			}
		cout << cnt << endl;
	}
	return 0;
}

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

using namespace std;
typedef long long ll;

#define LOOP(i, a, n) for (int i = (int)a; i < (int)n; ++i)
#define REP(i, n) LOOP(i, 0, n)
#define MOD 1000000007
#define INF 1000000000
#define PI 3.14159265359
#define MAX_W 50
#define MAX_H 50

int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1}, dy[8] = {-1, 1, 0, 0, -1, 1, -1, 1}, board[MAX_H][MAX_W], w, h;

void dfs(int x, int y) {
  board[y][x] = 0;

  for (int i = 0; i < 8; i++) {
    int nextx = x + dx[i], nexty = y + dy[i];
    if (nextx >= 0 && nextx < w && nexty >= 0 && nexty < h && board[nexty][nextx] == 1) {
      dfs(nextx, nexty);
    }
  }

  return;
}

int main(void) {
  vector<int> ans;

  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0) {
      break;
    }
    fill(board[0], board[49], 0);
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> board[i][j];
        //cout << "i:" << i << " j:" << j << endl;
      }
    }

    int res = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (board[i][j] == 1) {
          dfs(j, i);
          res++;
        }
      }
    }

    ans.push_back(res);
  }

  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << endl;
  }
  return 0;
}