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 <algorithm>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define SAY_YES cout << "YES" << endl;
#define SAY_Yes cout << "Yes" << endl;
#define SAY_NO cout << "NO" << endl;
#define SAY_No cout << "No" << endl;
#define IFYES(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "YES" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "NO" << endl;  \
    }
#define IFYes(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "Yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "No" << endl;  \
    }
#define IFyes(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "no" << endl;  \
    }
#define DEBUG_OUTPUT_ARRAY(XXX, ONE)                            \
    for (int i = 0; i < (ONE); i++)                             \
    {                                                           \
        cout << "DEBUG: i = " << i << " -> " << XXX[i] << endl; \
    }
#define DEBUG_OUTPUT_ARRAY2(XXX, ONE, TWO)                             \
    for (int i = 0; i < (ONE); i++)                                    \
    {                                                                  \
        cout << "<<< i = " << i << " >>>" << endl;                     \
        for (int j = 0; j < (TWO); j++)                                \
        {                                                              \
            cout << "DEBUG: j = " << j << " -> " << XXX[i][j] << endl; \
        }                                                              \
    }
#define DEBUG_OUTPUT_ARRAY2_BOX(XXX, ONE, TWO) \
    for (int i = 0; i < (ONE); i++)            \
    {                                          \
        cout << i << "  ";                     \
        for (int j = 0; j < (TWO); j++)        \
        {                                      \
            cout << XXX[i][j] << " ";          \
        }                                      \
        cout << endl;                          \
    }

typedef pair<long long int, long long int> pll;


const long long int mod = 1000000007;
const long long int INF = 99999999999999999;

long long int w,h,ch[100][100];

void dfs(long long int hhh,long long int www){
	ch[hhh][www]=0;
	if(ch[hhh][www+1]==1){dfs(hhh,www+1);}
	if(ch[hhh][www-1]==1){dfs(hhh,www-1);}
	if(ch[hhh+1][www]==1){dfs(hhh+1,www);}
	if(ch[hhh-1][www]==1){dfs(hhh-1,www);}
	
	if(ch[hhh+1][www+1]==1){dfs(hhh+1,www+1);}
	if(ch[hhh-1][www-1]==1){dfs(hhh-1,www-1);}
	if(ch[hhh+1][www-1]==1){dfs(hhh+1,www-1);}
	if(ch[hhh-1][www+1]==1){dfs(hhh-1,www+1);}
}

int main()
{
    cout << fixed << setprecision(18);
	cin>>w>>h;
	while(w!=0&&h!=0){
		long long int res=0;
		for(int i=1;i<=h;i++){
			for(int j=1;j<=w;j++){
				cin>>ch[i][j];
			}
		}
		for(int i=0;i<=h;i++){
			ch[i][0]=0;
			ch[i][w+1]=0;
		}
		for(int i=0;i<=w;i++){
			ch[0][i]=0;
			ch[h+1][i]=0;
		}
		for(int i=1;i<=h;i++){
			for(int j=1;j<=w;j++){
				if(ch[i][j]==1){
					res++;
					dfs(i,j);
				}
			}
		}
		cout<<res<<endl;
		cin>>w>>h;

	}
}

C++: #include<iostream>
using namespace std;
int ans;
int map[100][100];
void dfs(int x,int y,int w,int h);
int dx[8] = {1,1,0,-1,-1,-1,0,1},dy[8] = {0,1,1,1,0,-1,-1,-1};

int main(){
  int w,h;
  while(cin>>w>>h){
    ans=0;
    if(w == 0 && h == 0)break;
    for(int i = 0;i<h;i++)
      for(int j = 0;j<w;j++)
	cin>>map[i][j];
    for(int i = 0;i<h;i++)
      for(int j = 0;j<w;j++){
	if(map[i][j] == 1){
	  map[i][j] = 0;
	  ans++;
	  dfs(j,i,w,h);
	}
      }
    // cout<<"map"<<endl;
    // for(int a=0;a<h;a++){
    //   for(int b=0;b<w;b++) cout<<map[a][b];
    //   cout<<endl;
    // }
    //cout<<endl;
    cout<<ans<<endl;
  }
  return 0;
}

void dfs(int x,int y,int w,int h){
  //cout<<"LK+LKJ"<<endl;
  int nx,ny;
  for(int i = 0;i<8;i++){
    nx = x+dx[i]; ny = y+dy[i];
    if(nx>=0 && nx<w && ny>=0 && ny<h && map[ny][nx] == 1){
      map[ny][nx] = 0;
      dfs(nx,ny,w,h);
    }
  }
}

C++: //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
const double PI=3.14159265358979323846;
const in MOD = 1000000007;
const in INF=1<<30;
using P=pair<int,int>;
vector<int> dx={0,1,-1,0,1,-1,1,-1};
vector<int> dy={1,0,0,-1,1,1,-1,-1};
vector<vector<int>> a(0);
int h,w;

void dfs(int x,int y){
    a[x][y]=0;
    rep(i,8){
        int nx=x+dx[i],ny=y+dy[i];
        if(nx>=h||nx<0||ny>=w||ny<0) continue;
        if(a[nx][ny]==0) continue;
        dfs(nx,ny);
    }
}

int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cout << fixed << setprecision(10);
    cin>>w>>h;
    vector<int> ans(0);
    while(h+w>0){
        int cnt=0;
        a=vector<vector<int>>(h,vector<int>(w));
        rep(i,h)rep(j,w) cin>>a[i][j];
        rep(i,h)rep(j,w){
            if(a[i][j]==1){dfs(i,j);cnt ++;}
        }
        cin>>w>>h;
        ans.emplace_back(cnt);
    }
    for(auto e:ans) cout<<e<<endl;
}