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;

int idirs[] = {0, 0, 1, 1, 1, -1, -1, -1};
int jdirs[] = {1, -1, 0, 1, -1, 0, 1, -1};

int map[52][52];

void dfs(int i, int j) {
    if (!map[i][j]) return;
    map[i][j] = 0;
    for (int k = 0; k < 9; ++k) {
        dfs(i + idirs[k], j + jdirs[k]);
    }
}

int main() {
    while (1) {
        int w, h;

        cin >> w >> h;

        if (w == 0) break;

        for (int i = 0; i < 52; ++i)
            for (int j = 1; j < 52; ++j)
                map[i][j] = 0;

        for (int i = 1; i <= h; ++i) {
            for (int j = 1; j <= w; ++j) {
                cin >> map[i][j];
            }
        }

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

        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define mp make_pair

const vector<int> dx = {-1, -1, -1, 0, 0, 1, 1, 1};
const vector<int> dy = {-1, 0, 1, -1, 1, -1, 0, 1};

int main() {
    while (true) {
        int w, h;
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        vector<vector<int>> grid(h, vector<int>(w));
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                cin >> grid[i][j];
            }
        }
        int ans = 0;
        while (true) {
            stack<pair<int, int>> sk;
            bool flag = false;
            for (int i = 0; i < h; ++i) {
                for (int j = 0; j < w; ++j) {
                    if (grid[i][j]) {
                        sk.push(mp(i, j));
                        flag = true;
                        break;
                    }
                }
                if (flag) break;
            }
            if (!flag) {
                cout << ans << endl;
                break;
            }
            while (!sk.empty()) {
                pair<int, int> v = sk.top();
                grid[v.first][v.second] = 0;
                sk.pop();
                for (int i = 0; i < 8; ++i) {
                    pair<int, int> nv = mp(v.first + dx[i], v.second + dy[i]);
                    if (nv.first < 0 || nv.first >= h || nv.second < 0 || nv.second >= w) continue;
                    if (grid[nv.first][nv.second] == 0) continue;
                    grid[nv.first][nv.second] = 0;
                    sk.push(mp(nv.first, nv.second));
                }
            }
            ans++;
        }
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) FOR(i,0,n)
#define repr(i,n) for(int i=(n)-1;0<=i;--i)
#define each(e,v) for(auto&& e:(v))
#define all(v) begin(v),end(v)
#define dump(x) cerr<<#x<<": "<<(x)<<endl
#define INF 100000000
#define IINF 9999999999999;
using vint = vector<int>;
using ll = long long;
using vll = vector<ll>;
template <class T> void chmin(T& a, const T& b) { a = min(a, b); }
template <class T> void chmax(T& a, const T& b) { a = max(a, b); }

int prime[25] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
long long int dp[101][20] = {0};

bool earth[51][51];

void erase_island(int h, int w) {
    earth[h][w] = false;
    //cout << "# " << h << " " << w << endl;
    if(earth[h - 1][w - 1]) erase_island(h - 1, w - 1);
    if(earth[h - 1][w]) erase_island(h - 1, w);
    if(earth[h - 1][w + 1]) erase_island(h - 1, w + 1);
    if(earth[h][w + 1]) erase_island(h, w + 1);
    if(earth[h + 1][w + 1]) erase_island(h + 1, w + 1);
    if(earth[h + 1][w]) erase_island(h + 1, w);
    if(earth[h + 1][w - 1]) erase_island(h + 1, w - 1);
    if(earth[h][w - 1]) erase_island(h, w - 1);
}

int main() {
    while(true) {
        int w, h;
        cin >> w >> h;
        if(w == 0 && w == 0) break;
        rep(i, h) rep(j, w) {
                int temp;
                cin >> temp;
                if(temp == 0) earth[i][j] = false;
                else earth[i][j] = true;
            }

        int ans = 0;
        rep(i, h) rep(j, w) {
                if(earth[i][j]) erase_island(i , j), ++ans;
            }

        cout << ans << endl;
    }

    return 0;
}

C++: #include<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

int c[52][52];

bool dfs(int x,int y){
  if(c[x][y]==0) return false;
  c[x][y]=0;
  for(int i=-1;i<=1;i++){
    for(int j=-1;j<=1;j++){
      if((i|j)!=0&&c[x+i][y+j]!=0)
	dfs(x+i,y+j);
    }
  }
  return true;
}

int main(){ _;
  int w,h;
  while(cin>>w>>h,!(w==0&&h==0)){
    int count=0;
    REP(j,h+2)REP(i,w+2) c[i][j]=0;
    REP(j,h)REP(i,w) cin>>c[i+1][j+1];
    REP(j,h)REP(i,w)
      if(dfs(i+1,j+1))
	count++;
    cout<<count<<endl;
  }
}