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>
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) (x).begin(),(x).end()
using namespace std;
const int INF=1145141919,MOD=1e9+7;
const long long LINF=8101145141919364364;
const int dx[]={1,1,0,-1,-1,-1,0,1},dy[]={0,-1,-1,-1,0,1,1,1};

int main(){
    int w,h;
    while(cin>>w>>h&&w){
        int c[h][w]; rep(i,h)rep(j,w) cin>>c[i][j];
        int cnt=1;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(c[i][j]==1){
                    cnt++;
                    queue<pair<int,int>> q;
                    c[i][j]=cnt;
                    q.push({i,j});
                    while(q.size()){
                        pair<int,int> p=q.front(); q.pop();
                        int cy=p.first,cx=p.second;
                        rep(k,8){
                            int ny=cy+dy[k],nx=cx+dx[k];
                            if(0<=ny&&ny<h&&0<=nx&&nx<w&&c[ny][nx]==1){
                                c[ny][nx]=cnt;
                                q.push({ny,nx});
                            }
                        }
                    }
                }
            }
        }
        cout<<cnt-1<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#define rep(i, n) for(int i = 0; i < (int)(n); ++i)
#define REP(i, n) for(int i = 1; i < (int)(n); ++i)
using namespace std;

int w, h;
vector<vector<int>> m(50, vector<int>(50));
vector<vector<bool>> searched(50, vector<bool>(50, false));
vector<int> dx = {1, 1, 1, 0, -1, -1, -1, 0};
vector<int> dy = {-1, 0, 1, 1, 1, 0, -1, -1};

void DFS(int i, int j) {
    if (i < 0 || h <= i || j < 0 || w <= j) return;
    if (m[i][j] == 0) return;
    if (searched[i][j]) return;
    
    searched[i][j] = true;
    rep(k, 8) DFS(i + dx[k], j + dy[k]);
    return;
}

int main() {
    while (true) {
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        rep(i, h) rep(j, w) cin >> m[i][j];
        rep(i, h) rep(j, w) searched[i][j] = false;
        
        int cnt = 0;
        rep(i, h) rep(j, w) {
            if (m[i][j] == 0) continue;
            if (searched[i][j]) continue;
            ++cnt;
            DFS(i, j);
        }
        
        cout << cnt << endl;
    }
    return 0;
}

C++: #include<iostream>
using namespace std;
void dfs(int,int);
int a[50][50]={};

int main(){
  while(1){
    int count=0,w,h;
    cin>>w>>h;
    if(w+h==0)break;
    
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	cin>>a[i][j];

    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	if(a[i][j]==1){
	  dfs(j,i);
	  count++;
	}
    cout<<count<<endl;  
  }
  return 0;
}

  void dfs(int x,int y){
    a[y][x]=0;
    int nx,ny;
    int dx[8]={1,1,1,0,-1,-1,-1,0};
    int dy[8]={1,0,-1,-1,-1,0,1,1};
    for(int k=0;k<8;k++){
	nx=x+dx[k];
	ny=y+dy[k];

	if(nx>=0&&nx<50&&ny>=0&&ny<50&&a[ny][nx]==1)
	  dfs(nx,ny);
    }
  }

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <tuple>
using namespace std;
using ll=long long;
using R=double;

int w,h;

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

void dfs(int x, int y){
	field[y][x] = 3;

	for(int i = 0; i < 8; i++){
		int nx = x + dx[i];
		int ny = y + dy[i];
		if(nx >= 0 and nx <= w-1 and ny >= 0 and ny <= h and field[ny][nx]==1){
			dfs(nx, ny);
		}
	}
	return;
}

int main(){
	while(1){
		cin >> w >> h;
		if(w==0 and h==0){
			return 0;
		}
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin >> field[i][j];
			}
		}

		int count = 0;

		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				if(field[i][j]==1){
					dfs(i,j);
					count++;
				}
			}
		}
		cout << count << endl;
	}
}

C++: #include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
using namespace std;
#define REP(i,a,n) for(int i = a ; i < n ; i++)
#define rep(i,n) REP(i,0,n)

typedef long long ll;

int w,h;
int t[52][52];
int dx[] = {-1,0,1,-1,1,-1,0,1};
int dy[] = {-1,-1,-1,0,0,1,1,1};


void solve(int x,int y){
  t[y][x] = 0;
  rep(i,8){
    int nx = x + dx[i];
    int ny = y + dy[i];
    if(w>nx && nx>=0 && h>ny && ny>=0 && t[ny][nx]){
      solve(nx,ny);
    }
  }
}

int main(){
  while(cin>>w>>h,w||h){
    rep(i,h){
      rep(j,w){
        cin>>t[i][j];
      }
    }

    int cnt = 0;
    rep(i,h){
      rep(j,w){
        if(t[i][j]){
          solve(j,i);
          cnt++;
        }
      }
    }
    cout<<cnt<<endl;
  }
}