Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

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;
}