Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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;
}