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>

using namespace std;

bool isVisited[50][50] = { false };
int isLand[50][50] = { 0 };

void dfs(int x, int y, int w, int h)
{
  if (x < 0 || x >= w || y < 0 || y >= h) return;
  if (isVisited[x][y] == true) return;
  if (isLand[x][y] == 0) return;

  isVisited[x][y] = true;

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

  for (int i = 0; i < 8; i++) {
    dfs(x + dx[i], y + dy[i], w, h);
  }
}

int main()
{
  int w, h;
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0) break;
    
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> isLand[j][i];
        isVisited[j][i] = false;
      }
    }

    int count = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (isVisited[j][i] == false && isLand[j][i] == 1) {
          dfs(j, i, w, h);
          count++;
        }
      }
    }
    cout << count << endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define FOR(i, m, n) for(ll i = m; i < n; i++)
#define all(vec) vec.begin(), vec.end()
#define tmax(x, y, z) max((x), max((y), (z)))
#define tmin(x, y, z) min((x), min((y), (z)))
ll MM = 1000000000; ll mod = MM + 7; ll MMM=9223372036854775807;//2^63 -1
ll GCD(ll x, ll y){ if(y == 0) return x; else return GCD(y, x % y);}
ll LCM(ll x, ll y){ return x / GCD(x, y) * y;}
template<class T> inline bool chmin(T& a, T b){ if(a > b){ a = b; return true;} return false;}
template<class T> inline bool chmax(T& a, T b){ if(a < b){ a = b; return true;} return false;}
const ll INF = 1LL << 60;
using Graph = vector<vector<ll>>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
//cout << fixed << setprecision(10);

int field[100][100];
ll w, h;


void dfs(ll y, ll x){
    field[y][x] = 0;
    rep(i, 8){
        ll ny = y + ddy[i], nx = x + ddx[i];
        if(0 <= ny && ny < h && 0 <= nx && nx < w && field[ny][nx]){
            dfs(ny, nx);
        }
    }
}


int main(){
    while(1){
        cin >> w >> h;
        if(!w && !h) break;
        rep(hi, h){
            rep(wi, w){
                cin >> field[hi][wi];
            }
        }

        ll ans = 0;
        rep(hi, h){
            rep(wi, w){
                if(field[hi][wi]){
                    dfs(hi, wi);
                    ans++;
                }
            }
        }

        cout << ans << endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <unordered_map>

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
typedef long long ll;

using namespace std;

template<class T>
vector<T> make_vec(size_t a){
    return vector<T>(a);
}
template<class T, class... Ts>
auto make_vec(size_t a, Ts... ts){
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

typedef vector<vector<int>> vv;

int hashp(int i, int j){
    return i*100 + j;
}

void bfs(const vv& table, vv& is_visited, int si, int sj) {
    queue<int> q;
    q.push(hashp(si,sj));
    while(!q.empty()) {
        int hash_value = q.front();
        q.pop();
        int i = hash_value/100;
        int j = hash_value%100;

        if(i >= table.size() || i < 0 || j >= table[0].size() || j < 0)
            continue;
        if(!table[i][j])
            continue;
        if(is_visited[i][j])
            continue;
        is_visited[i][j] = 1;
        q.push(hashp(i+1,j));
        q.push(hashp(i-1,j));
        q.push(hashp(i,j+1));
        q.push(hashp(i,j-1));
        q.push(hashp(i+1,j+1));
        q.push(hashp(i+1,j-1));
        q.push(hashp(i-1,j+1));
        q.push(hashp(i-1,j-1));
        // cout << "pushed" << endl;
    }
    return;
}

int main(){
    int w,h;
    while(cin >> w >> h) {
        if (w == 0 && h ==0)
            break;
        vector<vector<int>> table;
        vector<vector<int>> is_visited;
        rep(i, h) {
            vector<int> tmp(w, 0);
            table.push_back(tmp);
            is_visited.push_back(tmp);
            rep(j, w) {
                cin >> table[i][j];
            }
        }
        int count = 0;
        rep(i, h) {
            rep(j, w) {
                if(table[i][j] == 1 && is_visited[i][j] == 0) {
                    count++;
                    bfs(table, is_visited, i, j);
                }
            }
        }
        cout << count << endl;
    }
}