Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include "bits/stdc++.h"
#define REP(i,a,b) for(i=a;i<b;++i)
#define rep(i,n) REP(i,0,n)
#define ll long long
#define ull unsigned ll
typedef long double ld;
#define ALL(a) (a).begin(),(a).end()
#define ifnot(a) if(not a)
#define dump(x)  cerr << #x << " = " << (x) << endl
using namespace std;

void reader(int &a) { scanf("%d", &a); }
void reader(double &a) { scanf("%lf", &a); }
void reader(char a[]) { scanf("%s", a); }
void reader(char &a) { scanf(" %c", &a); }
void reader(ll &a) { scanf("%lld", &a); }
void reader(ull &a) { scanf("%llu", &a); }
// void reader(string& a){cin >> a;};
template<class T, class U> void reader(T& t, U& u) { reader(t); reader(u); }
template<class T, class U, class V> void reader(T& t, U& u, V& v) { reader(t); reader(u); reader(v); }

void writer(const int a, char c) { printf("%d", a); putchar(c); }
void writer(const ll a, char c) { printf("%lld", a); putchar(c); }
void writer(const ull a, char c) { printf("%llu", a); putchar(c); }
void writer(const double a, char c) { printf("%.20lf", a); putchar(c); }
void writer(const char a[]) { printf("%s", a); };
void writer(const char a[], char c) { printf("%s", a); putchar(c); };
void writer(const char a, char c) { putchar(a); putchar(c); };
template<class T> void writerLn(T t) { writer(t, '\n'); }
template<class T, class U> void writerLn(T t, U u) { writer(t, ' '); writer(u, '\n'); }
template<class T, class U, class V> void writerLn(T t, U u, V v) { writer(t, ' '); writer(u, ' '); writer(v, '\n'); }
template<class T> void writerArr(T x[], int n) { int i; if (!n) { putchar('\n'); return; }rep(i, n - 1) writer(x[i], ' '); writer(x[n - 1], '\n'); }
template<class T> void writerVec(vector<T> x) { int n = x.size(); int i; if (!n) { putchar('\n'); return; }rep(i, n - 1) writer(x[i], ' '); writer(x[n - 1], '\n'); }

vector<string> split(const string &str, char sep) { vector<string> v; stringstream ss(str); string buffer; while (getline(ss, buffer, sep)) { v.push_back(buffer); }return v; }
// #define int ll
bool test = 1;
int dx[] = { 0,1,0,-1 };
int dy[] = { 1,0,-1,0 };
#define MAX MAX000
#define INF (1 << 28)
ull mod = (int)1e9 + 7;
//.....................

int W, H;
char c[100][100];
void grid_dfs(int y, int x) {
	int i, j;
	c[y][x] = '0';
	for (i = -1; i <= 1; i++) {
		for (j = -1; j <= 1; j++) {
			int nx = x + i;
			int ny = y + j;
			if (nx < 0 || W <= nx) continue;
			if (ny < 0 || H <= ny) continue;
			if (c[ny][nx] == '0') continue;
			grid_dfs(ny, nx);
		}
	}
}
signed main(void)
{
	int i, j, k;
	while (1) {
		cin >> W >> H;
		if (W == 0) break;
		rep(i, H) rep(j, W) {
			reader(c[i][j]);
		}
		/*rep(i, H) {
			writerArr(c[i],W);
		}*/
		int cnt = 0;
		rep(i, H) rep(j, W) {
			if (c[i][j] == '1') {
				cnt++;
				grid_dfs(i, j);
			}
		}
		cout << cnt << endl;
	}
	return 0;
}

C++: #include <iostream>
using namespace std;

int map[52][52];

void Serch(int i,int j){
  int dis[3] = {-1,0,1};
  map[i][j] = 0;
  for(int p=0;p<3;p++){
      for(int q=0;q<3;q++){
        if(p == 1 && q == 1) continue;
        if(map[i+dis[p]][j+dis[q]] == 1) Serch(i+dis[p],j+dis[q]);
      }
  }

 return;
}

int main(){
  int w,h;

  while(cin >> w >> h){
    int count = 0;
    if(!(w|h)) break;

    for(int i=0;i<w+2;i++)map[0][i] = map[h+1][i] = 0;
    for(int i=0;i<h+2;i++)map[i][0] = map[i][w+1] = 0;

    for(int i = 1;i<h+1;i++){
      for(int j = 1;j<w+1;j++){
        cin >> map[i][j];
      }
    }

    for(int i = 1;i<h+1;i++){
      for(int j = 1;j<w+1;j++){
        if(map[i][j] == 1){
          Serch(i,j);count++;
        }
      }
    }

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

  return 0;
}

C++: #include <iostream>
using namespace std;
int cnt,w,h;
int map[51][51];
void check(int i,int j){
	if(map[i][j]!=1)return;
	map[i][j]=2;
	if(i>0)check(i-1,j);
	if(i<h-1)check(i+1,j);
	if(j>0)check(i,j-1);
	if(j<w-1)check(i,j+1);
	if(i>0&&j>0)check(i-1,j-1);
	if(i>0&&j<w-1)check(i-1,j+1);
	if(i<h-1&&j>0)check(i+1,j-1);
	if(i<h-1&&j<w-1)check(i+1,j+1);
}


int main(){
	while(1){
		cin >>w >>h;
		if(!(w||h))break;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin >>map[i][j];
			}
		}
		cnt=0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(map[i][j]==1){
					cnt++;
					check(i,j);
				}
			}
		}
	cout << cnt<<endl; 

	}
}

C++: #include<bits/stdc++.h>
using namespace std;
char a[50][50];
void calc(int i,int j){
  if(a[i][j]=='1'){
    a[i][j]=2;
    calc(i+1,j);
    calc(i,j+1);
    calc(i+1,j+1);
    calc(i+1,j-1);
    calc(i-1,j);
    calc(i,j-1);
    calc(i-1,j-1);
    calc(i-1,j+1);
  }
  else return;
}

int main(){
  while(1){
  int w,h;
  cin>>w>>h;
  if(w==0&&h==0) break;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      cin>>a[i][j];
    }
  }
  int count=0;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(a[i][j]=='1'){
	count++;
	calc(i,j);
      }
    }
  }
  cout<<count<<endl;
  }
  return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<map>

using namespace std;

#define reps(i,f,n) for(int i=f;i<int(n);i++)
#define rep(i,n) reps(i,0,n)

int masu[55][55];
int w,h;

void saiki(int y,int x){
	if(y<0 || x<0 || y>=h || x>=w)return;
	if(masu[y][x]==0)return;
	masu[y][x]=0;
	
	int dx[]={0,1,0,-1,-1,-1,1,1};
	int dy[]={1,0,-1,0,1,-1,1,-1};
	
	rep(i,8){
		saiki(y+dy[i],x+dx[i]);
	}
}

int main(){
	
	A:
	cin>>w>>h;
	if(w==0 && h==0)return 0;
	
	rep(i,h){
		rep(j,w){
			cin>>masu[i][j];
		}
	}
	
	int count = 0;
	rep(i,h){
		rep(j,w){
			if(masu[i][j]==1){
				count++;
				saiki(i,j);
			}
		}
	}
	printf("%d\n",count);
	
	goto A;
}