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 rep1(i, n) for (int i = 1; i < (n); ++i)
using namespace std;
typedef long long ll;

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


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

    while(1){
        int w, h;
        cin >> w >> h;

        if(w==0) break;

        vector<vector<int>> c(h, vector<int>(w));
        rep(i, h) rep(j, w) cin >> c[i][j];

        int cnt = 0;
        vector<vector<int>> seen(h, vector<int>(w, 0));

        rep(i, h) rep(j, w){
            if(c[i][j] == 0) continue;
            if(seen[i][j]) continue;
            ++cnt;
            
            seen[i][j] = cnt;
            vector<pair<int, int>> stack;
            stack.push_back(make_pair(i, j));
            while(!stack.empty()){
                int x = stack.back().second;
                int y = stack.back().first;
                stack.pop_back();

                rep(dir, 8){
                    int ny = y + dy[dir];
                    int nx = x + dx[dir];

                    if(ny<0 || ny>=h || nx<0 || nx>=w) continue;
                    if(c[ny][nx] == 0) continue;
                    if(seen[ny][nx]) continue;

                    seen[ny][nx] = cnt;
                    stack.push_back(make_pair(ny, nx));
                }
            }
        }

        cout << cnt << endl;
    }

    return 0;
}

C++: #include <algorithm>
#include <vector>
#include <cfloat>
#include <string>
#include <cmath>
#include <set>
#include <cstdlib>
#include <map>
#include <ctime>
#include <iomanip>
#include <functional>
#include <deque>
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <stack>
#include <climits>
#include <sys/time.h>
#include <cctype>

using namespace std;

typedef long long ll;

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

    bool field[w][h];
    memset(field, false, sizeof(field));
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
	int t;
	cin >> t;
	if (t == 0) {
	  field[j][i] = false;
	}else {
	  field[j][i] = true;
	}
      }
    }

    int cnt = 0;
    int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
    int dy[] = {1, -1, 0, 0, 1, -1, -1, 1};
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
	if (field[j][i]) {
	  stack<int> st;
	  st.push(j);
	  st.push(i);
	  field[j][i] = false;
	  while (!st.empty()) {
	    int y = st.top(); st.pop();
	    int x = st.top(); st.pop();

	    for (int p = 0; p < 8; p++) {
	      int nx = x+dx[p];
	      int ny = y+dy[p];

	      if (nx < 0 || ny < 0 ||
		  nx >= w || ny >= h ||
		  !field[nx][ny]) {
		continue;
	      }
	      st.push(nx);
	      st.push(ny);
	      field[nx][ny] = false;
	    }
	  }
	  cnt++;
	}
      }
    }

    std::cout << cnt << std::endl;
  }
}

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

#define ll long long
#define vvi vector< vector<int> >
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back 
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

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

int h,w;
bool inrange(int x,int y){
	if(x<0||y<0||x>h-1||y>w-1) return false;
	else return true;
}

bool search(int x,int y){
	bool rst = false;
	if(data[x][y] == 0) return rst;
	else {
		data[x][y] = 0;
		rst = true;
	}
	REP(i,8){
		int nx = x+dx[i];
		int ny = y+dy[i];
		if(!inrange(nx,ny)) continue;
		search(nx,ny);
	}
	return rst;
}

int main(){
	while(1){
		cin >> w >> h;
		if(w==0&&h==0) break;
		int ans = 0;
		REP(i,h) REP(j,w) cin >> data[i][j];
		REP(i,h){
			REP(j,w){
				if(search(i,j)==true) ans++;
			}
		}
		cout << ans << endl;
	}
}

C++: #include <cstdio>
#define f(n)for(int i=0;i<n;i++)
#define s(w)scanf("%d",&w)
int a,n,m,d[99][99];
int u[16]={-1,-1,-1,0,0,1,1,1,-1,0,1,-1,1,-1,0,1};
void k(int x,int y){
	if(d[x][y]){f(8)k(x+u[i],y+u[i+8]);d[x][y]=0;}
}
int main(){
	a=0;
	s(n);s(m);
	f(9801)d[i/99][i%99]=0;
	f(n*m)s(d[i/n+1][i%n+1]);
	f(n*m)if(d[i/n+1][i%n+1])a++,k(i/n+1,i%n+1);
	printf("%d\n",a);
	main();
}