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 <iostream>
#include <vector>
using namespace std;

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

int H,W;
vector<vector<int>> field;
int cnt;

bool seen[510][510];
void dfs(int h,int w){
  seen[h][w] = true;

  for(int dir=0;dir<8;dir++){
    int nh=h+dx[dir];
    int nw=w+dy[dir];

    if(nh<0 || nh>=H || nw<0 || nw>=W) continue;
    if(field[nh][nw] == 0) continue;

    if(seen[nh][nw]) continue;

    dfs(nh,nw);
  }
}

int main(){
  cin >> W >> H;
  while(H!=0 || W!=0){
    field.resize(H);
    for(int i=0;i<H;i++){
      field[i].resize(W);
    }
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
        cin >> field[i][j];
      }
    }

    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
        seen[i][j] = false;
      }
    }
    cnt = 0;
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
        if(seen[i][j]) continue;
        if(field[i][j] == 1){
          dfs(i,j);
          cnt++;
        }
      }
    }

    cout << cnt << endl;
    cin >> W >> H;
  }
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
using namespace std;

void solve(vector<vector<bool> > land)
{
	vector<vector<bool> > check(land.size(), vector<bool>(land[0].size(), false));

	int num = 0;
	for(unsigned i=1; i<land.size()-1; ++i){
		for(unsigned j=1; j<land[0].size()-1; ++j){
			if(!land[i][j] || check[i][j])
				continue;
			check[i][j] = true;
			++ num;

			queue<pair<int, int> > place;
			place.push(make_pair(i,j));
			while(!place.empty()){
				int y = place.front().first;
				int x = place.front().second;
				for(int dy=-1; dy<=1; ++dy){
					for(int dx=-1; dx<=1; ++dx){
						if(land[y+dy][x+dx] && !check[y+dy][x+dx]){
							check[y+dy][x+dx] = true;
							place.push(make_pair(y+dy, x+dx));
						}
					}
				}
				place.pop();
			}
		}
	}

	cout << num << endl;
}

int main()
{
	for(;;){
		int w, h;
		cin >> w >> h;
		if(w == 0 && h == 0)
			break;

		vector<vector<bool> > land(h+2, vector<bool>(w+2, false));
		for(int i=1; i<=h; ++i){
			for(int j=1; j<=w; ++j){
				int tmp;
				cin >> tmp;
				land[i][j] = (tmp == 1);
			}
		}

		solve(land);
	}

	return 0;
}

C++: #include <iostream>
#include <cstdio>
using namespace std;
char field[51][51];
int W, H;

void dfs(int x, int y) {
  //cout << "field[x][y] = " << field[x][y] << endl;
  field[x][y] = '0';
  
  for(int dx = -1 ; dx <= 1 ; dx++) {
    for(int dy = -1 ; dy <= 1 ; dy++) {
      int nx = x + dx, ny = y + dy;
      if(0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] == '1') dfs(nx, ny);
    }
  }
}

int main(void) {
  while(cin >> W >> H, W != 0 && H != 0) {
    int res = 0;
    for(int i = 0 ; i < H ; i++)
      for(int j = 0 ; j < W ; j++) cin >> field[i][j];
    
    for(int i = 0 ; i < H ; i++) {
      for(int j = 0 ; j < W ; j++) {
	if(field[i][j] == '1') {
	  dfs(i, j);
	  res++;
	  /*cout << endl;
	  for(int b = 0 ; b < H ; b++) {
	    for(int a = 0 ; a < W ; a++) {
	      cout << field[b][a] << ' ';
	    }
	    cout << endl;
	  }
	  cout << endl;*/
	}
      }
    }
    cout << res << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

void dfs (vector<vector<int>> &vec, vector<vector<int>> &shd, int h, int w) {
  if (h < 0 || h >= vec.size() || w < 0 || w >= vec[0].size()) return;
  else if (!vec[h][w]) return;
  else if (shd[h][w]) return;
  else shd[h][w] = 1;

  dfs(vec, shd, h + 1, w);
  dfs(vec, shd, h , w + 1);
  dfs(vec, shd, h - 1, w);
  dfs(vec, shd, h, w - 1);
  dfs(vec, shd, h + 1, w + 1);
  dfs(vec, shd, h + 1, w - 1);
  dfs(vec, shd, h - 1, w + 1);
  dfs(vec, shd, h - 1, w - 1);

  return;
}
     
int main() {
  int w, h, cou=0;
  vector<int> vec_ans(1000,2147483647);

  cin >> w >> h;

  do {
    int ans=0;
    vector<vector<int>> vec(h, vector<int>(w, 0));
    vector<vector<int>> shd(h, vector<int>(w, 0));
    rep(i, h) rep(j, w) cin >> vec[i][j];

    rep(i, h) rep(j, w) {
      if (!shd[i][j] && vec[i][j]) {
        ++ans;
        dfs(vec, shd, i, j);
      }
    }

    vec_ans[cou] = ans;
    ++cou;
    cin >> w >> h;
  } while (w || h);

  for (int i=0; vec_ans[i] != 2147483647; ++i) {
    cout << vec_ans[i] << endl;
  }
}