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,a,b) for(int i=a;i<b;i++)
#define repd(i,a,b) for(int i=a;i>=b;i--)
#define llrep(i,a,b) for(ll i=a;i<b;i++)
#define llrepd(i,a,b) for(ll i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
using namespace std;
void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; const ll MOD = 1000000007;
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; }

//---------------------------------------------------------------------------------------------------
int dx[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };
int dy[8] = { 1, 0, -1, 0 ,1, -1, 1, -1 };
ll H, W;
vector<vector<int>> area;

void dfs(int i, int j) {
    area[i][j] = 0;
    rep(n, 0, 8) {
        int nx = i + dx[n], ny = j + dy[n];
        if (nx >= H || ny >= W || nx < 0 || ny < 0) continue;
        if (area[nx][ny] == 1) dfs(nx, ny);
    }
}


void resolve(ll W, ll H) {
    area = vector<vector<int>>(H, vector<int>(W));
    rep(i, 0, H) rep(j, 0, W) cin >> area[i][j];
    ll count = 0;
    rep(i, 0, H) {
        rep(j, 0, W) {
            if (area[i][j] == 1) {
                count++;
                dfs(i, j);
            }
        }
    }
    cout << count << endl;
}

void _main() {
    while(true) {
        cin >> W >> H;
        if (W == 0 && H == 0) break;
        resolve(W,H);
    }
}

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

int dfs(int h, int w, vector<vector<int>> &field, vector<vector<int>> &flag) {
    flag[h][w] = 1;
    int dh[] = {-1, -1, 0, 1, 1, 1, 0, -1};
    int dw[] = {0, 1, 1, 1, 0, -1, -1, -1};
    
    for (int i = 0; i < 8; i++) {
        int nh = h + dh[i];
        int nw = w + dw[i];
        if (field[nh][nw] == 1 && flag[nh][nw] == 0) {
            dfs(nh, nw, field, flag);
        }
    }
    return 0;
}


int solve(int H, int W) {
    vector<vector<int>> field(H+2, vector<int>(W+2));
    for (int h = 1; h <= H; h++) {
        for (int w = 1; w <= W; w++) {
            cin >> field[h][w];
        }
    }
    
    int ret = 0;
    vector<vector<int>> flag(H+2, vector<int>(W+2));
    for (int h = 1; h <= H; h++) {
        for (int w = 1; w <= W; w++) {
            if (field[h][w] == 1 && flag[h][w] == 0) {
                dfs(h, w, field, flag);
                ret++;
            }
        }
    }
    return ret;
}

int main(void){
    int h, w;
    vector<int> ans;
    while (1) {
        cin >> w >> h;
        if (!h && !w) break;
        ans.push_back(solve(h, w));
    }
    for (auto &i : ans) {
        cout << i << endl;
    }
    
}

C++: #include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
typedef long long ll;
typedef vector<vector<ll>> vvl;
typedef vector<ll> vl;
typedef vector<vector<char>> vvc;
typedef vector<char> vc;
typedef pair<ll,ll> pll;
ll max(ll x,ll y){
if(x>y)return x;
else return y;
}
ll min(ll x,ll y){
  return x+y-max(x,y);
}

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


void dfs(vector<vector<int>> &graph,int x,int y,vector<vector<bool>> &seen,int h,int w){
  seen[x][y]=true;
  rep(i,8){
    rep(j,8){
      int ax=x+dx[i];
      int ay=y+dy[i];
      if(ax<0||ay<0||ax>=h||ay>=w)continue;
      if(seen[ax][ay])continue;
      if(graph[ax][ay]==0)continue;
      dfs(graph,ax,ay,seen,h,w);
    }
  }
}

int main() {
rep(aaa,100000000){
  int h,w;cin >> w >> h;
  if(h==0&&w==0)return 0;
  vector<vector<bool>> seen(h,vector<bool>(w,false));
  vector<vector<int>> graph(h,vector<int>(w));
  rep(i,h)rep(j,w)cin >> graph[i][j];
  int ans=0;
  rep(v,h){
    rep(u,w){
      if(seen[v][u])continue;
      if(graph[v][u]==0)continue;
      dfs(graph,v,u,seen,h,w);
      ans++;
    }
  }
  cout << ans << endl;
}
}

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

typedef pair<int, int> P;

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

int main() {
	int w, h;
	while (cin >> w >> h, w) {
		vector<vector<int>>ma(w, vector<int>(h));
		for (int i = 0; i < h;i++) {
			for (int j = 0; j < w;j++) {
				cin >> ma[j][i];
			}
		}
		int ans = 0;
		for (int i = 0; i < w;i++) {
			for (int j = 0; j < h;j++) {
				if (ma[i][j] == 1) {
					ans++;
					queue<P> q;
					q.push(P(i, j));
					ma[i][j] = 0;
					while (!q.empty()) {
						P p = q.front(); q.pop();
						for (int k = 0; k < 8; k++) {
							int nx = p.first + dx[k], ny = p.second + dy[k];
							if (nx < w && nx>=0 && ny < h && ny>=0 && ma[nx][ny]==1) {
								ma[nx][ny] = 0; q.push(P(nx, ny));
							}
						}
					}
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>

using namespace std;
int w, h;
int c[100][100];
int dx[8]={-1, 0, 1, 0, -1, -1, 1, 1};
int dy[8]={0, -1, 0, 1, -1, 1, -1, 1};
int cnt=0;

void search(int x, int y);

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

void search(int x, int y)
{
	int nx, ny;
	for(int i = 0; i < 8; i++){
		nx = x + dx[i];
		ny = y + dy[i];
		if(nx < 0 || ny < 0 || nx >= h || ny >= w) continue;
		if(c[nx][ny] == 1){
			c[nx][ny] = 0;
			search(nx, ny);
		}
	}
}