Go: package main

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

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		return []byte(f1())
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

// -----------------------------------------------------------------------------

func main() {
	defer stdout.Flush()

	for {
		ans, ok := solve()
		if !ok {
			break
		}
		println(ans)
	}

}

func solve() (int, bool) {
	w := readInt()
	h := readInt()
	if w == 0 && h == 0 {
		return 0, false
	}

	c := make([][]int, h+2)
	c[0] = make([]int, w+2)
	c[h+1] = make([]int, w+2)
	for i := 1; i-1 < h; i++ {
		c[i] = make([]int, w+2)
		for j := 1; j-1 < w; j++ {
			c[i][j] = readInt()
		}
	}

	var ans int
	for i := 1; i-1 < h; i++ {
		for j := 1; j-1 < w; j++ {
			if 0 < c[i][j] {
				ans++
				dfs(c, i, j)
			}
		}
	}
	return ans, true
}

func dfs(c [][]int, y, x int) {
	c[y][x] = 0
	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{-1, -1, -1, 0, 0, 1, 1, 1}
	for i := range dx {
		nx := x + dx[i]
		ny := y + dy[i]
		if 0 < c[ny][nx] {
			dfs(c, ny, nx)
		}
	}
}

// -----------------------------------------------------------------------------

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define rep3(i, s, n) for (int i = s; i > (int)(n); i--)
#define all(obj) obj.begin(), obj.end()
#define db(x) cerr << #x << ":" << x << " "
#define dbl(x) cerr << #x << ":" << x << "\n"
#define dbv(vec) cerr << #vec << ":"; for (auto e : vec) cerr << e << " "; cout << "\n"
#define dbvv(vv) cerr << #vv << ":\n"; for (auto vec : vv) { for (auto e : vec) cerr << e << " "; cerr << "\n"; }
#define YN(f) cout << (f ? "YES" : "NO") << "\n"
#define Yn(f) cout << (f ? "Yes" : "No") << "\n"
#define yn(f) cout << (f ? "yes" : "no") << "\n"
using ll = long long;
using vi = vector<int>; using vl = vector<ll>; using vs = vector<string>;
using vvi = vector<vi>; using vvl = vector<vl>;
using pii = pair<int, int>; using pll = pair<ll, ll>;
using vpii = vector<pii>; using vpll = vector<pll>;
using ti3 = tuple<int, int, int>; using tl3 = tuple<ll, ll, ll>;

vector<vector<bool>> c;
vector<vector<bool>> r;

void dfs(int i, int j) {
	r[i][j] = true;	
	vi di = { 1, 1, 0, -1, -1, -1, 0, 1 };
	vi dj = { 0, 1, 1, 1, 0, -1, -1, -1 };
	rep(k, 8) {
		int in = i + di[k], jn = j + dj[k];
		if (c[in][jn] and !r[in][jn]) dfs(in, jn);
	}
}

int main() {
	while (true) {
		int w, h; cin >> w >> h;
		if (w == 0 and h == 0) break;
		c.assign(h + 2, {}); rep(i, h + 2) c[i].assign(w + 2, false);
		rep2(i, 1, h + 1) {
			rep2(j, 1, w + 1) {
				int cij; cin >> cij;
				c[i][j] = cij == 1;
			}
		}

		// dbvv(c);
		r.assign(h + 2, {}); rep(i, h + 2) r[i].assign(w + 2, false);
		int ans = 0;		
		rep2(i, 1, h + 1) rep2(j, 1, w + 1) {
			if (c[i][j] and !r[i][j]) {
				dfs(i, j);
				ans++;
			}
		}
		// dbl(ans);
		cout << ans << endl;
	}
}

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

using namespace std;

struct Pos { int y, x; };

int main(void)
{
    int w, h;
    int map[52][52] = {{0}};
    int nx[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
    int ny[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
    queue<Pos> poss;
    Pos now;
    int ans;
    while (true)
    {
        cin >> w >> h;
        if (!w && !h) break;
        ans = 0;
        for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) cin >> map[i][j];
        for (int i = 1; i <= h; i++)
        {
            for (int j = 1; j <= w; j++)
            {
                if (map[i][j])
                {
                    poss.push({i, j});
                    while (!poss.empty())
                    {
                        now = poss.front();
                        poss.pop();
                        if (!map[now.y][now.x]) continue;
                        else map[now.y][now.x] = 0;
                        for (int k = 0; k < 8; k++) if (map[now.y + ny[k]][now.x + nx[k]]) poss.push({now.y + ny[k], now.x + nx[k]});
                    }
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>

#define rep(i, n) for(int i = 0 ; i < n ; i++)

using namespace std;

int area[50][50], cnt = 0;
int w, h;

void dfs(int x ,int y)
{
  area[x][y] = 0;

  for(int dx = -1 ; dx <= 1 ; dx++){
    for(int dy = -1 ; dy <= 1 ; dy++){
      int nx = x + dx, ny = y + dy;

      if(0 <= nx && nx < w && 0 <= ny && ny < h && area[nx][ny] == 1){
	dfs(nx, ny);
      }
    }
  }
}

int main()
{
  while(true){
    cin >> w >> h;
    if(w == 0 && h == 0) break;

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

    rep(i, w){
      rep(j, h){
	if(area[i][j] == 1){
	  dfs(i, j);
	  cnt++;
	}
      }
    }
    
    cout << cnt << '\n';
    cnt = 0;
  }
}

C++: #include <iostream>
#include <vector>
using namespace std;
int ans;
int w, h;
//?§???????
vector<int> vx = { -1, 0, 1, -1, 1, -1, 0, 1 };
vector<int> vy = { 1, 1, 1, 0, 0, -1, -1, -1 };
vector<vector<int>> map;
void Search(int i,int j){
	//??¨????????????????????§??????
	for (int m = 0; m < vx.size(); m++){
		int x = j + vx[m];
		int y = i + vy[m];
		if (x >= 0 && x < w &&
			y >= 0 && y < h){
			if (map[y][x] == 1){
				map[y][x] = map[i][j];
				Search(y,x);
			}
		}
	}
}

int main(){
	while (1){
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		ans = 1;
		map = vector<vector<int>>(h,vector<int>(w));
		//???????????????
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				cin >> map[i][j];
			}
		}

		//??¢?´¢
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (map[i][j] != 1) continue;
				ans++;
				Search(i,j);
			}
		}
		cout << --ans << endl;
	}
	return 0;
}