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;

#define ll long long
#define vi  vector<int>
#define vvi vector<vi>
#define pi  pair<int,int>
#define mp  make_pair
#define pb  push_back
#define MOD 1e9 + 7
#define PAI  3.1415
#define all(x) (x).begin(),(x).end()
#define chmax(x,y) x = max(x,y)
#define chmin(x,y) x = min(x,y)
#define pr(x) cout << x << endl
#define Endl endl
#define rep(i,n) for(int i = 0 ; i < n; i++)

const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
const int ddx[8] = {-1,0,1,-1,1,-1,0,1};
const int ddy[8] = {-1,-1,-1,0,0,1,1,1};
const int inf = 99999999;
const ll linf = 1LL << 62;

ll gcd(ll a,ll b){
  if(a < b)swap(a , b);
  if(a % b == 0) return b;
  else gcd(b, a%b);
}

ll lcm(ll a,ll b){
  if(a < b)swap(a , b);
  return (a/gcd(a , b))*b;
}

struct UnionFind {
  vi par; //par[i] : iの親番号（例）par[3] = 2 : 3の親は2

  UnionFind(int N) : par(N) { //最初は全てが根として初期化
    rep(i,N) par[i] = i;
  }

  int root(int x){ //データxが属する木の根を再帰で得る : root(x) = {xの木の根}
    if(par[x] == x)return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y){ //xとyの木を併合
    int rx = root(x); //xの根をrx
    int ry = root(y); //yの根をry
    if(rx == ry)return; //xとyの根が同じ
    par[rx] = ry; //xとyの根が同じでない時、xの根rxをyの根にryにつける。
  }

  bool same(int x, int y){//2つのデータx，yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int h,w;
vector<vector<int> > v(60, vector<int>(60, 0));
vector<vector<bool> > visited(60, vector<bool>(60,false));

int dfs(int i, int j){
  if(visited[i][j])return 0;

  visited[i][j] = true;
  rep(x,8){
    rep(y,8){
      int posx = j + ddx[x];
      int posy = i + ddy[y];
      if(0 <= posx && posx < w && 0 <= posy && posy < h){
        if(!visited[posy][posx] and v[posy][posx]){
          dfs(posy,posx);
        }
      }
    }
  }
  return 1;
}

int main(){

   while(cin >> w >> h, w and h){
    int ans = 0;
    rep(i,60){
      rep(j,60){
         visited[i][j] = false;
      }
    }

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

    rep(i,h){
      rep(j,w){
        if( !visited[i][j] && v[i][j])ans += dfs(i,j);
      }
    }
   cout << ans << endl;
   }

  return 0;

}

C++: #include <iostream>
using namespace std;

void f(int n){
if(n%100==0){
cerr<<n<<endl;
}
}

int main(){
f(1);
}

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

int main() {
    int w, h;

    stack<pair<int, int>> s;
    
    vector<int> anss(0);
    while(true) {
        cin >> w >> h;
        if (w == 0 && h == 0) {
            break;
        }
        vector<vector<int>> map(h, vector<int>(w));
        vector<vector<bool>> reached(h, vector<bool>(w));
        rep(i, h) rep(j, w) cin >> map[i][j];

        int ans = 0;
        rep(i, h) rep(j, w) {
            s.push(pair<int, int>(i, j));
            bool hasi = false;
            while(!s.empty()) {
                pair<int, int> p = s.top(); s.pop();
                int ph = p.first;
                int pw = p.second;
                if (ph < 0 || ph >= h || pw < 0 || pw >= w) {
                    continue;
                }
                if (map[ph][pw] == 0) {
                    continue;
                }
                if (reached[ph][pw]) {
                    continue;
                }
                hasi = true;
                reached[p.first][p.second] = true;
                s.push(pair<int, int>(ph - 1, pw - 1));
                s.push(pair<int, int>(ph - 1, pw));
                s.push(pair<int, int>(ph - 1, pw + 1));
                s.push(pair<int, int>(ph, pw - 1));
                s.push(pair<int, int>(ph, pw + 1));
                s.push(pair<int, int>(ph + 1, pw - 1));
                s.push(pair<int, int>(ph + 1, pw));
                s.push(pair<int, int>(ph + 1, pw + 1));
            }
            if (hasi) ans++;
        }
        anss.push_back(ans);
    }
    
    for(int i = 0; i < anss.size(); i++ ) {
        cout << anss[i] << endl;
    } 
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

struct node{
  int x,y;
};

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

int main(){
  int w,h,i,j,k,ans=0;
  while(1){
    queue<node> que;
    cin >> w >> h;
    if(w+h == 0)break;
    int c[h][w];
    ans = 0;
    node now,next,to;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++)cin >> c[i][j];
    }
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
        if(c[i][j]){
          ++ans;
          now.x = j; now.y = i;
          que.push(now);
          while(!que.empty()){
            next = que.front();
            c[next.y][next.x] = 0;
            que.pop();
            for(k=0;k<8;k++){
              to.x = next.x + dx[k];
              to.y = next.y + dy[k];
              if(to.x<0 || to.x >=w || to.y<0 || to.y >=h)continue;
              if(c[to.y][to.x]){
                que.push(to);
              }
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}