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>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
#define WHITE 0
#define GRAY 1
typedef long long LL;
const int MAXN = 60;
int w,h,ans, mapp[MAXN][MAXN], color[MAXN][MAXN];
vector<int> v_ans;

void dfs_visit(int u,int k) {

	color[u][k] = GRAY;//参照したことを示す


	if(mapp[u][k + 1] == 1 && color[u][k + 1] == WHITE) {//右
		dfs_visit(u,k + 1);
	}

	if(mapp[u + 1][k + 1] == 1 && color[u + 1][k + 1] == WHITE) {//右下
		dfs_visit(u + 1,k + 1);
	}

	if(mapp[u + 1][k] == 1 && color[u + 1][k] == WHITE) {//下
		dfs_visit(u + 1,k);
	}

    if(k > 0){
        if(mapp[u + 1][k - 1] == 1 && color[u + 1][k - 1] == WHITE) {// 左下
            dfs_visit(u + 1,k - 1);
        }
    }

    if(k > 0){
        if(mapp[u][k - 1] == 1 && color[u][k - 1] == WHITE) {// 左
            dfs_visit(u,k - 1);
        }
    }

    if(u > 0){
        if(mapp[u - 1][k] == 1 && color[u - 1][k] == WHITE) {// 上
            dfs_visit(u - 1,k);
        }
    }

    if(u > 0){
        if(mapp[u - 1][k + 1] == 1 && color[u - 1][k + 1] == WHITE) {// 右上
            dfs_visit(u - 1,k + 1);
        }
    }

    if(u > 0 && k > 0){
        if(mapp[u - 1][k - 1] == 1 && color[u - 1][k - 1] == WHITE) {// 左上
            dfs_visit(u - 1,k - 1);
        }
    }


}

void dfs() {
    int ans = 0;
	for(int i = 0; i < h; i++) {
        for(int j = 0; j < w; j++){
            if(mapp[i][j] == 1 && color[i][j] == WHITE ) {//未探訪の島なら探索

                dfs_visit(i,j);
                ans += 1;
            }
        }

	}

	v_ans.push_back(ans);

}

int main() {

    while(1){
        memset(mapp, 0, sizeof(mapp));//木情報をリセット
        memset(color, 0, sizeof(color));//探訪情報をリセット
        cin >> w >> h;
        if(w == 0) break;
        for(int i = 0; i < h; i++){

            for(int j = 0; j < w; j++){
                cin >> mapp[i][j];
            }

        }
        dfs();
    }
    rep(i,v_ans.size()) cout <<v_ans[i] <<endl;
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using VI = vector<ll>;
using VV = vector<VI>;
using VS = vector<string>;

// tourist set
template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << '\n'; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
// tourist set end

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i,b) FOR(i, 0, b)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<'\n'
#define p2(s, t) cout << (s) << " " << (t) << '\n'
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << '\n'
#define p_yes() p("YES")
#define p_no() p("NO")
#define SZ(x) ((int)(x).size())
#define SORT(A) sort(ALL(A))
#define RSORT(A) sort(ALL(A), greater<ll>())

void no(){p_no(); exit(0);}
void yes(){p_yes(); exit(0);}

const ll mod = 1e9 + 7;
const ll inf = 1e18;
const double PI = acos(-1);


using PII = pair<ll, ll>;
#define MP make_pair
int dx[8] = {-1, 0, 1,  0, -1, 1, -1,  1};
int dy[8] = { 0, 1, 0, -1, -1, 1,  1, -1};


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

    // input
    ll w,h;

    while(cin>>w>>h){
      if(w==0) return 0;

      VV G(h);
      rep(i, h){
        rep(j, w){
          ll a; cin >> a;
          G[i].push_back(a);
        }
      }

      ll cnt = 0;
      rep(i, h){
        rep(j, w){
          if(G[i][j]==1){
            G[i][j]=0;
            cnt++;
            // bfs
            queue<PII> que;
            que.push(MP(i,j));
            while(!que.empty()){
              auto pa = que.front(); que.pop();
              rep(k, 8){
                ll ty = pa.first + dy[k];
                ll tx = pa.second+ dx[k];
                if(0<=ty && ty<h && 0<=tx && tx<w && G[ty][tx]==1){
                  G[ty][tx] = 0;
                  que.push(MP(ty, tx));
                }
              }
            }
          }
        }
      }
      p(cnt);
    }
    
    return 0;
}