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>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())

const ll d[8][2] = {{0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}};

void dfs(const vector<vector<ll>> &c, vector<vector<ll>> &g, ll x, ll y, ll root) {
    if (g[y][x] != -1) return;
    if (c[y][x] == 0) return;
    g[y][x] = root;
    rep(i, 8) {
        ll nx = x + d[i][0];
        ll ny = y + d[i][1];
        if ((nx < 0) || (nx >= sz(c[0])) || (ny < 0) || (ny >= sz(c))) continue;
        dfs(c, g, nx, ny, root);
    }
}

void solve(ll w, ll h) {
    vector<vector<ll>> c(h, vector<ll>(w)), g(h, vector<ll>(w, -1));
    rep(i, h) rep(j, w) cin >> c[i][j];
    set<ll> s;
    rep(i, h) {
        rep(j, w) {
            dfs(c, g, j, i, i * w + j);
            if (g[i][j] >= 0) s.insert(g[i][j]);
        }
    }
    cout << sz(s) << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    while(true) {
        ll w, h;
        cin >> w >> h;
        if ((w == 0) && (h == 0)) break;
        solve(w, h);
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <cstring>

using namespace std;
// ascending order
#define vsort(v) sort(v.begin(), v.end())
// descending order
#define vsort_r(v) sort(v.begin(), v.end(), greater<int>())
#define vunique(v) v.erase(unique(v.begin(), v.end()), v.end())
#define mp make_pair
#define ts(x) to_string(x)
#define rep(i, a, b) for(int i = (int)a; i < (int)b; i++)
#define repm(i, a, b) for(int i = (int)a; i > (int)b; i--)
#define bit(a) bitset<8>(a)
#define des_priority_queue priority_queue<int, vector<int>, greater<int> >
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e18;

#define MAX_V 1000000

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

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

  vector<int> ans;
  while(true) {
      int w, h;
      cin >> w >> h;
      if(w == 0 && h == 0) break;

      int c[h][w];
      bool reached[h][w];
      rep(i, 0, h) rep(j, 0, w) {
          cin >> c[i][j];
          reached[i][j] = false;
      }

      int rsl = 0;
      rep(y, 0, h) {
          rep(x, 0, w) {
              if(c[y][x] == 0) continue;
              if(reached[y][x]) continue;

              rsl++;
              stack<pair<int, int> > s;
              s.push(make_pair(x, y));
              reached[y][x] = true;
              while(!s.empty()) {
                  pair<int, int> p = s.top();
                  s.pop();
                  rep(i, 0, 8) {
                      int nx = p.first + dx[i], ny = p.second + dy[i];
                      if(!(0 <= nx && nx < w && 0 <= ny && ny < h)) continue;
                      if(c[ny][nx] == 0) continue;
                      if(reached[ny][nx]) continue;

                      reached[ny][nx] = true;
                      s.push(make_pair(nx, ny));
                  }
              }
          }
      }
      ans.push_back(rsl);
  }
  rep(i, 0, ans.size()) cout << ans[i] << endl;

}

C++: #include <bits/stdc++.h>
using namespace std;

int x,y;
const int mx[8]={-1,-1,-1,0,0,1,1,1};
const int my[8]={-1,0,1,-1,1,-1,0,1};

void dfs(vector<vector<int>> &c,int H,int W,int& h,int& w)
{
c[H][W] = 0;
for(int i=0;i<8;i++){y=H+my[i],x=W+mx[i];if(y<0||y>h-1||x<0||x>w-1)continue;if(!c[y][x])continue;dfs(c,y,x,h,w);}
}

int main()
{
int w,h,ans;
vector<vector<int>> c(50,vector<int>(50));
scanf("%d%d", &w, &h);
while(w!=0&&h!=0)
{
ans=0;
for(int i=0;i<h;i++)for(int j=0;j<w;j++)scanf("%d", &c[i][j]);
for(int i=0;i<h;i++)for(int j=0;j<w;j++){if(!c[i][j])continue;dfs(c,i,j,h,w);ans++;}
printf("%d\n",ans);
scanf("%d%d", &w, &h);
}
return 0;
}

C++: #include <iostream>
#include <vector>

using namespace std;

inline bool inRange(int a, int x, int b) {
    return a <= x && x < b;
}

void fill(vector<vector<int> > &v, int r, int c) {
    v[r][c] = 0;
    for(int dr = -1; dr <= 1; ++dr) {
        for(int dc = -1; dc <= 1; ++dc) {
            int nr = r + dr;
            int nc = c + dc;
            if(inRange(0, nr, v.size()) && inRange(0, nc, v[0].size()) && v[nr][nc]) {
                fill(v, nr, nc);
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(0);

    while(true) {
        int C, R;
        cin >> C >> R;
        if(!C && !R) break;

        vector<vector<int> > v(R, vector<int>(C));
        for(int i = 0; i < R; ++i) 
            for(int j = 0; j < C; ++j)
                cin >> v[i][j];

        int cnt = 0;
        for(int r = 0; r < R; ++r) {
            for(int c = 0; c < C; ++c) {
                if(v[r][c]) {
                    ++cnt;
                    fill(v, r, c);
                }
            }
        }

        cout << cnt << endl;
    }

    return 0;
}