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<iostream>
#include<string.h>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i,n) for(i=0;i<n;i++)
using namespace std;
typedef pair<int, int> pii;

int main()
{
  int i,j,k;
  int h,w;
  int dx[8]={0,1,0,-1,1,1,-1,-1};
  int dy[8]={1,0,-1,0,1,-1,1,-1};

  while(1){

    cin>>w>>h;
    if(w==0 && h==0)break;

    int sum=0;
    int c[55][55]={};

    for(i=1;i<=h;i++)for(j=1;j<=w;j++)cin>>c[i][j];

    while(1){

      queue<pii> q;
      rep(i,h+1)rep(j,w+1)
	if(c[i][j]==1)goto START;
      break;//????????????

    START :
      q.push(pii(i,j));//y,x
      sum++;
      
      while(q.size()){
	i=q.front().first;
	j=q.front().second;
	q.pop();
	c[i][j]=0;
	rep(k,8){
	  int y=i+dy[k];
	  int x=j+dx[k];
	  if(c[y][x]==1){
	    q.push(pii(y,x));
	    c[y][x]=0;
	  }
	}
      }

    }
    cout<<sum<<endl;

  }
}

C++: #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define invrepr(i,a,b) for(int i=b-1;i>=a;i--)
#define invrep(i,n) invrepr(i,0,n)
#define repitr(itr,a) for(auto itr=a.begin();itr!=a.end();++itr)
const ll MOD=1e9+7;

int main() {
    ios_base::sync_with_stdio(false);

    while (1) {
        int w,h;
        cin >> w >> h; 
        if (w==0 && h==0) break;
        vector<vector<char>> s(h+2,vector<char>(w+2,'0'));
        rep(i,h) {
            rep(j,w) cin >> s[i+1][j+1];
        }
        int ans=0;
        vector<int> di={-1,-1,-1,0,0,1,1,1},dj={-1,0,1,-1,1,-1,0,1};
        repr(i,1,h+1) {
            repr(j,1,w+1) {
                if (s[i][j]=='1') {
                    queue<pair<int,int>> q;
                    q.push(make_pair(i,j));
                    s[i][j]='0'; 
                    while (!q.empty()) {
                        pair<int,int> p=q.front();
                        q.pop();
                        rep(k,8) {
                            int ni=p.first+di[k],nj=p.second+dj[k];
                            if (s[ni][nj]=='1') {
                                q.push(make_pair(ni,nj));
                                s[ni][nj]='0';
                            }
                        }
                    }
                    ++ans;
                }
            }
        }
        cout << ans << endl;
    }
    
    return 0;
}

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

int w,h;
int c[50][50];
vector<vector<int> > visited;
int dx[]={1,1,0,-1,-1,-1,0,1};
int dy[]={0,-1,-1,-1,0,1,1,1};

bool dfs(int x,int y){
    if(visited[x][y]) return false;
    visited[x][y]=1;

    for(int d=0;d<8;d++){
        int i=x+dx[d];
        int j=y+dy[d];
        if(0<=i&&i<h&&0<=j&&j<w){
            if(c[i][j]){
                dfs(i,j);
            }  
        }
    }
    return true;
}

int main(){
    visited.assign(50,vector<int>(50,0));
    cin>>w>>h;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>c[i][j];
        }
    }
    int cnt=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(c[i][j]){
                cnt+=dfs(i,j);
            }
        }
    }

    cout<<cnt<<endl;
    return 0;
}

C++: #include <iostream>
using namespace std;
void erase(int island[52][52],int x,int y){
	island[x][y]=0;
	for(int m=-1;m<=1;m++){
		if(island[x+m][y]==1){
			erase(island,x+m,y);
		}
		if(island[x][y+m]==1){
			erase(island,x,y+m);
		}
		if(island[x+m][y+m]==1){
			erase(island,x+m,y+m);
		}
		if(island[x-m][y+m]==1){
			erase(island,x-m,y+m);
		}
	}
}
int main(){
	int w,h;
	while(cin>>w>>h){
		if(w==0 && h==0) break;
		int island[52][52]={0};
		int num,count=0;		
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin>>num;
				island[i+1][j+1]=num;
			}
		}
		for(int i=1;i<h+1;i++){
			for(int j=1;j<w+1;j++){
				if(island[i][j]==1){
					erase(island,i,j);
					count++;
				}
			}
		}
		cout<<count<<endl;
	}
	return 0;
}