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++: #include<cstdio>
#include<iostream>
using namespace std;

int array[1000][1000];
int X,Y;

void dfs(int y, int x){

  int nx,ny;
  array[y][x]=0;
  for(int dx=-1;dx<=1;dx++)
    for(int dy=-1;dy<=1;dy++){
      nx=x+dx;
      ny=y+dy;
      if(nx<X && ny<Y && nx>=0 && ny>=0 && array[ny][nx]!=0 ) dfs(ny,nx);
    }

}


int main(){

  while(1){
    cin>>X>>Y;
    int cnt=0;
    if(X==0 && Y==0) break;
    for(int i=0;i<Y;i++)
      for(int j=0;j<X;j++)
	cin>>array[i][j];
    
    for(int i=0;i<Y;i++)
      for(int j=0;j<X;j++)
	if(array[i][j]==1){ 
	  dfs(i,j);
	  cnt++;
	}
    cout<<cnt<<endl;
  }
  return 0;
}

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

int w, h;
int map[52][52];

int check(int i, int j)
{
	if (map[i][j] == 0)return 0;
	else
	{
		map[i][j] = 0;
		check(i-1, j - 1);
		check(i-1, j);
		check(i-1, j + 1);
		check(i, j - 1);
		check(i, j + 1);
		check(i + 1, j - 1);
		check(i + 1, j);
		check(i + 1, j + 1);
		return 0;
	}
}

int main()
{
	while (true)
	{
		map[52][52] = { 0 };
		int count = 0;
		cin >> w >> h;
		if (w == 0)return 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)
				{
					check(i, j);
					count++;
				}
			}
		}
		cout << count << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
int dx[8] = {1,0,-1,0,1,-1,-1,1};//右、上、左、下、右上、左上、左下、右下のつもり
int dy[8] = {0,1,0,-1,1,1,-1,-1};//実際は色々反転している。どうせ4か8方向調べるから関係ないとしている。
bool seen[60][60]={true};
int h,w;
void dfs(vector<vector<int> > &G,int i,int j){
    if(G[i][j]) seen[i][j] = true;
    for(int k=0;k<8;k++){
	int di = i + dx[k];
	int dj = j + dy[k];
	if(di < 0 || h <= di || dj < 0 || w <= dj)continue;
	
	if(seen[di][dj]==false) dfs(G,di,dj);
    }
}
int main(void){
    while(cin >> w >> h){
	if(w == 0 && h== 0) break;
	for(int i=0;i<h;i++)for(int j=0;j<w;j++) seen[i][j] = true;
	vector<vector<int> > G(h,vector<int>(w));
	for(int i=0;i<h;i++){
	    for(int j=0;j<w;j++){
		cin >> G[i][j];
		if(G[i][j]) seen[i][j] = false;
	    }
	}
	int t =0;
	for(int i=0;i<h;i++){
	    for(int j=0;j<w;j++){
		if(seen[i][j]) continue;
		dfs(G,i,j);
		t++;
	    }
	}
	cout << t << endl;
    }

    return 0;
}

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

int x,y;
int field[50][50];

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


int main(){
	while(1){
		int result = 0;
		cin >> x >> y;
		if(x == 0 && y == 0)
			break;
		for(int j=0; j<y; j++){
			for(int i=0; i<x; i++){
				cin >> field[i][j];
			}
		}
		for(int j=0; j<y; j++){
			for(int i=0; i<x; i++){
				if(field[i][j]==1){
					result++;
					dfs(i,j);
				}
			}
		}
		cout << result << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
struct edge {
    int to;     // 辺の行き先
    int weight; // 辺の重み
    edge(int t, int w) : to(t), weight(w) { }
};
using Graph = vector<vector<int>>;
using dou =long double;
string yes="yes";
string Yes="Yes";
string YES="YES";
string no="no";
string No="No";
string NO="NO";

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
#define rep(i, n)         for(int i = 0; i < (int)(n); i++)
#define brep(n)           for(int bit=0;bit<(1<<n);bit++)
#define erep(i,container) for (auto i : container)
#define irep(i, n)        for(int i = n-1; i >= (int)0; i--)
#define rrep(i,m,n) for(ll i = m; i < (ll)(n); i++)
#define reprep(i,j,h,w) rep(i,h)rep(j,w)
#define all(x) (x).begin(),(x).end()
#define VEC(type,name,n) std::vector<type> name(n);rep(i,n)std::cin >> name[i];
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define itn int
#define mp make_pair
#define sum(a) accumulate(all(a),0ll)
#define keta fixed<<setprecision
#define vvector(name,typ,m,n,a)vector<vector<typ> > name(m,vector<typ> (n,a))
#define vvvector(name,t,l,m,n,a) vector<vector<vector<t> > > name(l, vector<vector<t> >(m, vector<t>(n,a)));
#define vvvvector(name,t,k,l,m,n,a) vector<vector<vector<vector<t> > > > name(k,vector<vector<vector<t> > >(l, vector<vector<t> >(m, vector<t>(n,a)) ));
#define case std::cout << "Case #" <<qqq+1<<": "
#define res resize
#define as assign
#define ffor for(;;)
#define ppri(a,b) std::cout << a<<" "<<b << std::endl
#define pppri(a,b,c) std::cout << a<<" "<<b <<" "<< c<<std::endl
#define aall(x,n) (x).begin(),(x).begin()+(n)
 
typedef long long ll;
typedef pair<int,int> P;
//typedef long long T;
const int INF = 2000000000;
const ll INF64 = 922337203685477580ll;
const ll MOD = 1000000007ll;
int w,h;
void dfs(std::vector<std::vector<int> > &a,int y,int x){
    a[y][x]=0;
    rrep(i,max(0,y-1),min(y+2,h)){
        rrep(j,max(0,x-1),min(x+2,w)){
            if(y==i&&x==j)continue;
            if(a[i][j]==0)continue;
            dfs(a,i,j);
        }
    }
}
int main(){
    for(;;){
        
        std::cin >> w>>h;
        if(w==0)exit(0);
        vvector(a,int,h,w,0);
        reprep(i,j,h,w)std::cin >> a[i][j];
        int ans=0;
        reprep(i,j,h,w){
            if(a[i][j]){
                ans++;
                dfs(a,i,j);
            }
        }
        std::cout << ans << std::endl;
    }
    
    
}