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 "bits/stdc++.h"
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define FORR(i, a, b) for(int i=(b)-1; i>=(a); --i)
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long LL;
typedef long double LD;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<bool> VB;
typedef vector<vector<int> > VVI;
typedef vector<vector<bool> > VVB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
const LL MOD=1000000007ll;
const LL INF=2000000000ll;

long power(long a,long b){return b?power(a*a%MOD,b/2)*(b%2?a:1)%MOD:1;}
long comb(int N,int K)
{
	long x=1,y=1;
	for(int i=1;i<=K;i++)
	{
		x=x*(N-i+1)%MOD;
		y=y*i%MOD;
	}
	return x*power(y,MOD-2)%MOD;
}

long hcomb(int N, int K){
    return comb(N+K-1, N-1);
}

void dfs(const vector<vector<int> > &G, int v, vector<int> &seen) {
    seen[v] = true; // v を訪問済にする

    // v から行ける各頂点 next_v について
    for (auto next_v : G[v]) { 
        if (seen[next_v]) continue; // next_v が探索済だったらスルー
        dfs(G, next_v, seen); // 再帰的に探索
    }
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(10);

    int w, h;

    cin >> w >> h;

    while (w || h){
        VVI c(h, VI(w));
        VVI G(h*w);
        REP(i,h) REP(j,w) cin >> c[i][j];
        VI seen(h*w,false);
        REP(i,h) REP(j,w){
            if (c[i][j]){
                if (i != 0) if (c[i-1][j]) G[i*w+j].push_back((i-1)*w+j);
                if (i != h-1) if (c[i+1][j]) G[i*w+j].push_back((i+1)*w+j);
                if (j != 0) if (c[i][j-1]) G[i*w+j].push_back(i*w+j-1);
                if (j != w-1) if (c[i][j+1]) G[i*w+j].push_back(i*w+j+1);
                if (i != 0 && j != 0) if (c[i-1][j-1]) G[i*w+j].push_back((i-1)*w+j-1);
                if (i != 0 && j != w-1) if (c[i-1][j+1]) G[i*w+j].push_back((i-1)*w+j+1);
                if (i != h-1 && j != 0) if (c[i+1][j-1]) G[i*w+j].push_back((i+1)*w+j-1);
                if (i != h-1 && j != w-1) if (c[i+1][j+1]) G[i*w+j].push_back((i+1)*w+j+1);
            }
            else {
                seen[i*w+j] = true;
            }
        }

        int ans = 0;
        REP(i,h*w){
            if (seen[i] == false){
                dfs(G,i,seen);
                ans++;
            }
        }

        cout << ans << endl;

        cin >> w >> h;
    }
}

C++: #include<iostream>
using namespace std;
int map[114][514];
int h, w;
void dfs(int i, int j) {
	map[i][j] = 0;
	if (i < h) {
		if (j < w) {
			if (map[i - 1][j - 1] == 1)dfs(i - 1, j - 1);
		}
		if(map[i - 1][j] = 1)dfs(i - 1, j);
		if (j > 0) {
			if (map[i - 1][j + 1] == 1)dfs(i - 1, j + 1);
		}
	}
	if (j < w) {
		if (map[i][j - 1] == 1)dfs(i, j - 1);
	}
	if (j > 0) {
		if (map[i][j + 1] == 1)dfs(i, j + 1);
	}
	if (i > 0) {
		if (j < w) {
			if (map[i + 1][j - 1] == 1)dfs(i + 1, j - 1);
		}
		if (map[i + 1][j] = 1)dfs(i + 1, j);
		if (j > 0) {
			if (map[i + 1][j + 1] == 1)dfs(i + 1, j + 1);
		}
	}
	return;
}
int main() {
	while (cin >> h >> w) {
		if (!h && !w)return 0;
		int INF = 0;
		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) {
			INF++;
			dfs(i, j);
		}
		cout << INF << endl;
	}
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i=0;i < (int)(n);i++)

int dx[8]={0,0,-1,1,-1,-1,1,1};
int dy[8]={1,-1,0,0,-1,1,-1,1};
int h,w;

int maze[51][51];
bool seen[51][51];
void dfs(int x,int y){
     seen[x][y]=true;
     rep(i,8){
          int nx,ny;
          nx=x+dx[i];ny=y+dy[i];
          if (nx >= 0 && nx < h && ny >= 0 && ny < w && maze[nx][ny] == 1){ 
               if (seen[nx][ny]) continue;
               dfs(nx,ny);
          }   
     }
     
}

int main(){
     bool can = true;
     while(can){
          cin >> w >> h;
          if (h == 0 && w==0) {
               can = false;
               break;
          }
          int ans=0;
          rep(i,h) rep(j,w) seen[i][j]=false;

          rep(i,h) rep(j,w) cin >> maze[i][j];
          rep(i,h) rep(j,w){
               if (!seen[i][j] && maze[i][j] == 1) {
                    ans++;
                    //printf("(i,j) = (%d,%d)\n",i,j);
                    dfs(i,j);
               }
          }
          cout << ans << endl;
     }
    
     
     return 0;
}