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 <cstring>
using namespace std;

int is[52][52];
int w, h, c;

void search(int x, int y){
	int i, j;
	if(!is[x][y]) return;
	is[x][y] = 0;
	for(i = -1; i <= 1; i++){
		for(j = -1; j <= 1; j++){
			search(x + i , y + j);
		}
	}
	return;
}

int main(void){
	int i, j;
	while(cin >> h >> w, w){
		memset(is, 0, sizeof(is));
		c = 0;
		for(i = 1; i <= w; i++)
			for(j = 1; j <= h; j++)
				cin >> is[i][j]; 

		while(1){
			for(i = 1; i <= w; i++){
				for(j = 1; j <= h; j++){
					if(is[i][j]) break;
				}
				if(is[i][j]) break;
			}
			if(i == w+1) break;
			search(i, j);
			c++;
		}
		cout << c << endl;
	}
	return 0;
}

C++: #include<queue>
#include<cstdio>

#define	mp	make_pair

using namespace std;

typedef	pair<int,int>	pii;

const int dx[]={1,1,0,-1,-1,-1,0,1},dy[]={0,-1,-1,-1,0,1,1,1};
int field[50][50];
bool visited[50][50];

int main(){
	for(int w,h;scanf("%d%d",&w,&h),w;){
		for(int i=0;i<h;i++)for(int j=0;j<w;j++){
			scanf("%d",field[i]+j);
			visited[i][j]=false;
		}

		int cnt=0;
		for(int i=0;i<h;i++)for(int j=0;j<w;j++){
			if(field[i][j]==1 && !visited[i][j]){
				visited[i][j]=true;
				queue<pii> qu;	qu.push(mp(i,j));
				while(!qu.empty()){
					pii a=qu.front();	qu.pop();
					for(int k=0;k<8;k++){
						int xx=a.second+dx[k],yy=a.first+dy[k];
						if(0<=xx && xx<w && 0<=yy && yy<h
						&& field[yy][xx]==1 && !visited[yy][xx]){
							qu.push(mp(yy,xx));
							visited[yy][xx]=true;
						}
					}
				}
				cnt++;
			}
		}

		printf("%d\n",cnt);
	}

	return 0;
}

C++: #include<iostream>
using namespace std;
int w,h,i,j,c[50][50];

void visit(int i,int j){

  if(i<0||j<0)return;
  if(i>=50||j>=50)return;

  c[i][j]=0;
  for(int k=-1;k<=1;k++)
    for(int l=-1;l<=1;l++)
      if(c[i+k][j+l]==1)
	visit(i+k,j+l);
     
}
int main(){
  int count;
  while(1){ 
    count=0;
    cin>>w>>h;
    if(w==0&&h==0)break; 
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
	cin>>c[i][j];
      }
    }
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
	if(c[i][j]==1){
	  count++;
	  visit(i,j);
	}
      }
    }
    cout<<count<<endl;
  }
  return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef pair<int, int> P;
int dx[8] = { -1, -1, 0, 1, 1, 1, 0, -1 };
int dy[8] = { 0, -1, -1, -1, 0, 1, 1, 1 };
void DFS(vector<vector<int>>&A, P p, int H, int W) {
	A.at(p.first).at(p.second) = 0;
	for (int i = -1; i <= 1; i++) {
		for (int j = -1; j <= 1; j++) {
			if (i == 0 && j == 0) continue;
			int nx = p.second + i;
			int ny = p.first + j;
			if (0 <= nx && nx < W && 0 <= ny && ny < H && A.at(ny).at(nx) == 1) DFS(A, make_pair(ny, nx), H, W);
		}
	}
	return;
}
int main() {
	int W, H;
	cin >> W >> H;
	while (W != 0 && H != 0) {
		vector<vector<int>>A(H, vector<int>(W));
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				cin >> A.at(i).at(j);
			}
		}
		vector<vector<bool>>reach(H, vector<bool>(W, false));
		int ans = 0;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				if (A.at(i).at(j) == 1) {
					DFS(A, make_pair(i, j), H, W);
					ans++;
				}
			}
		}
		cout << ans << endl;
		cin >> W >> H;
	}
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int w, h;
vector<vector<int>> g, d;

void dfs(int y, int x) {
	d[y][x] = 1;
	for (int i = -1; i <= 1; i++) {
		for (int j = -1; j <= 1; j++) {
			int dy = y + i;
			int dx = x + j;
			if (dy < 0 || dy >= h || dx < 0 || dx >= w) continue;
			if (g[dy][dx] == 0 || d[dy][dx] != 0) continue;
			dfs(dy, dx);
		}
	}
}

int main() {
	while (cin >> w >> h) {
		if (w == 0 && h == 0) break;
		g.assign(h, vector<int>(w));
		rep(i, h) rep(j, w) cin >> g[i][j];
		d.assign(h, vector<int>(w));
		int ans = 0;
		rep(i, h) rep(j, w) {
			if (g[i][j] == 0 || d[i][j] != 0) continue;
			dfs(i, j);
			ans++;
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <utility>
#include <numeric>
#include <stack>
#include <queue>
using namespace std;
#define ll long long
#define INF 1000000007
int h,w;
int m[50][50];
void dfs(int k,int n){
    m[k][n]=0;
    for(int i=-1;i<=1;i++){
        for(int j=-1;j<=1;j++){
            if(i==0&&j==0)continue;
            int ni=k+i;
            int nj=n+j;
            if(0<=ni&&ni<h&&0<=nj&&nj<w&&m[ni][nj]==1)dfs(ni,nj);
        }
    }
    return;
}
int count(){
    int re=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(m[i][j]==1){
                dfs(i,j);
                re++;
            }
        }
    }
    return re;
}
int main(){
    while(1){
        cin>>w>>h;
        if(h==0&&w==0)break;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin>>m[i][j];
            }
        }
        cout<<count()<<endl;
    }
    return 0;
}