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;
using ll = long long;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define debug(x) cerr << #x << ": " << x << endl;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

ll c[100][100];
bool b[100][100];
ll dx[8] = {0, 0, 1, 1, 1, -1, -1, -1}, dy[8] = {1, -1, 0, 1, -1, 0, -1, 1};
ll w, h;

void dfs(ll x, ll y) {
    b[x][y] = true;
    rep(i, 8) {
        ll nx = x + dx[i], ny = y + dy[i];
        if (0 <= nx && nx < h && 0 <= ny && ny < w && c[nx][ny] == 1) {
            if (b[nx][ny]) continue;
            dfs(nx, ny);
        }
    }
}

int main() {
    vector<ll> vec;
    while(cin >> w >> h) {
        if(w == 0 && h == 0) {
            for(auto v : vec) cout << v << endl;
            return 0;
        }

        rep(i, h) rep(j, w) cin >> c[i][j];
        rep(i, h) rep(j, w) b[i][j] = false;

        ll ans = 0;
        rep(i, h) rep(j, w) {
            if (c[i][j] == 1 && b[i][j] == false) {
                ans++;
                dfs(i, j);
            }
        }
        vec.push_back(ans);
    }
}

C++: //include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <queue>

using namespace std;

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

bool tab[60][60];
int dx[] = {-1,0,1,-1,1,-1,0,1};
int dy[] = {-1,-1,-1,0,0,1,1,1};
void dfs(int x, int y){
  if(!tab[y][x]) return;
  tab[y][x] = false;
  for(int i=0;i<8;++i)
	dfs(x+dx[i], y+dy[i]);
}

int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  while(true){
	int W, H; cin >> W >> H;
	if(W == 0) break;
	int ans = 0;
	fill((bool*)tab, (bool*)tab+60*60, false);
	REP(y,H) REP(x,W){
	  int c; cin >> c;
	  tab[y+1][x+1] = c == 1;
	}
	for(int y=1;y<=H;++y)
	  for(int x=1;x<=W;++x)
		if(tab[y][x]){
		  ++ans;
		  dfs(x,y);
		}
	cout << ans << endl;
  }
  
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
vector<vector<int> > c;
vector<vector<bool> > done;
int w,h;
int dy[] = {-1,-1,-1,0,0,1,1,1};
int dx[] = {1,0,-1,1,-1,1,0,-1};

void dfs(int y, int x)
{
		done[y][x] = true;
		for(int i=0;i<8;i++)
		{
				int ny = y + dy[i];
				int nx = x + dx[i];
				if(0<=ny && ny<h && 0<=nx && nx<w && c[ny][nx]==1 && !done[ny][nx])
				{
						dfs(ny,nx);
				}
		}
}

signed
main()
{
	while(cin >> w >> h and !(w==0 & h==0))
	{
			c = vector<vector<int> >(h,vector<int>(w));
			done = vector<vector<bool> >(h,vector<bool>(w, false));
			for(int y=0;y<h;y++)
					for(int x=0;x<w;x++)
							cin >> c[y][x];

			int cnt = 0;
			for(int y=0;y<h;y++)
			{
					for(int x=0;x<w;x++)
					{
							if(c[y][x] && !done[y][x]){
									cnt++;
									dfs(y,x);
							}
					}
			}
			cout << cnt << endl;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int dx[] = {-1,-1,-1,0,0,1,1,1};
int dy[] = {0,1,-1,1,-1,0,1,-1};
bool visited[100][100];
int w,h;
int f[100][100];
void dfs(int x,int y){
  visited[y][x] = true;
  for(int i=0;i<8;++i){
    int mx=x+dx[i];
    int my=y+dy[i];
    if(mx>=w||my>=h||mx<0||my<0) continue;
    if(visited[my][mx] || !f[my][mx]) continue;
    dfs(mx,my);
  }
}

int main(){
  while(cin>>w>>h,w||h){
    memset(visited,0,sizeof(visited));
    for(int i=0;i<h;++i)
      for(int j=0;j<w;++j) cin >> f[i][j];
    int cnt = 0;
    for(int i=0;i<h;++i)
      for(int j=0;j<w;++j){
	if(f[i][j]&&!visited[i][j]){
	  ++cnt;
	  dfs(j,i);
	}
      }
    cout << cnt << endl;
  }
  return 0;
}