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>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i,k,n) for(int i = k; i < n; i++)
#define mmax(start, end) *max_element(start, end)
#define mmin(start, end) *min_element(start, end)
#define argmax max_element
#define argmin min_element
#define push push_back
#define pop pop_back
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
typedef string str;

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

template<class T = ll> T in() {
    T _in;
    cin >> _in;
    return _in;
}

int w, h;
bool c[55][55] = {};

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

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    while(cin >> w >> h,w){
        rep(i, h) {
            rep(j, w) {
                cin >> c[i][j];
            }
        }

        int ans = 0;
        // 探索
        rep(y, h) {
            rep(x, w) {
                if (c[y][x]) {
                    DFS(y, x);
                    ans++;
                }
            }
        }

        cout << ans << endl;
    };
    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;


int w , h;
int land[51][51];

void dfs(int px , int py){
	int tx , ty;
	land[py][px]=0;
	for(int i=-1 ; i<=1 ; ++i){
		for(int j=-1 ; j<=1 ; ++j){
			tx=px+i , ty=py+j;
			if(tx<0 || tx>=w || ty<0 || ty>=h) continue;
			if(land[ty][tx]) dfs(tx , ty);
		}
	}
}


int main(){
	int cnt=0;
	while(cin >> w >> h , w||h){
		for(int i=0 ; i< h ; ++i){
			for(int j=0 ; j< w ; ++j){
				cin >> land[i][j];
			}
		}
		for(int i=0 ; i<h ; ++i){
			for(int j=0 ; j<w ; ++j){
				if(land[i][j]){
					dfs( j , i);
					cnt++;
				}
			}
		}
		cout << cnt << endl;
		cnt=0;
	}
	return 0; 
}

C++: #include"bits/stdc++.h"

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;


vector<string>road;
int ix[] = { 0,1,1,1,0,-1,-1,-1 };
int iy[] = { -1,-1,0,1,1,1,0,-1 };
void dfs(int y, int x, int h, int w) {
	road[y][x] = '0';
	rep(i, 8) {
		int dx = x + ix[i], dy = y + iy[i];
		if (dx < 0 || w <= dx || dy < 0 || h <= dy || road[dy][dx] == '0')continue;
		dfs(dy, dx, h, w);
	}
}

int main() {
	while (1) {
		int h, w, cnt = 0;
		string k;
		cin >> w >> h;
		if (h == 0 || w == 0)break;
		road.resize(h);
		road.assign(h, "");
		rep(i, h)rep(j, w) {
			cin >> k;
			road[i] += k;
		}
		rep(i, h)rep(j, w) {
			if (road[i][j] == '0')continue;
			dfs(i, j, h, w);
			cnt++;
		}
		cout << cnt << endl;
	}
	return 0;
}

C++: #include <math.h>

#include <algorithm>
#include <bitset>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

void dfs(int x, int y, vector<vector<int>>& field) {
  if (0 <= x && x < field.size() && 0 <= y && y < field[0].size()) {
    if (field[x][y] == 0) {
      return;
    }
  } else {
    return;
  }

  field[x][y] = 0;
  for (int dx = -1; dx <= 1; dx++) {
    for (int dy = -1; dy <= 1; dy++) {
      dfs(x + dx, y + dy, field);
    }
  }
  return;
}

int main() {
  vector<int> ans_v;
  for (int k = 0; k < 100; k++) {
    int w, h;
    cin >> w >> h;
    if (w == 0) {
      break;
    }

    vector<vector<int>> field(h, vector<int>(w));
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> field[i][j];
      }
    }
    int ans = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (field[i][j] == 1) {
          dfs(i, j, field);
          ans++;
        }
      }
    }

    ans_v.push_back(ans);
  }

  for (int i = 0; i < ans_v.size(); i++) {
    cout << ans_v[i] << endl;
  }

  return 0;
}