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 <iostream>
#include <vector>
#include <iomanip>
#include <cassert>
#include <cmath>
using namespace std; typedef long long ll; ll an=0; const ll INF = 1e18; const ll MOD = 1e9+7;
typedef pair<int,int> P;
vector<int> v[50];

void dfs(int x, int y, int r, int c) {
  if (!v[x][y]) return;
  //cout<<"("<<x<<","<<y<<","<<r<<","<<c<<") ";
  v[x][y] = 0;
  for(int p=-1;p<=1;p++) {
    for(int q=-1;q<=1;q++) {
      int f=x+p, g=y+q;
      if (0 <= f && f < c && 0 <= g && g < r && v[f][g]) {
        dfs(f,g,r,c);
      }
    }
  }
}
int main(){
  while(true) {
    int r,c; cin>>r>>c; if (r==0) return 0;
    for(int i=0;i<c;i++) {v[i].clear(); v[i].resize(r);}
    int an=0;
    for(int i=0;i<c;i++) {
      for(int j=0;j<r;j++) {
        int x; cin>>x; v[i][j]=x;
      }
    }
    for(int i=0;i<c;i++) {
      for(int j=0;j<r;j++) {
        if (v[i][j]) {an++, dfs(i,j,r,c);}
      }
    }
    cout<<an<<endl;
  }
}

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

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

void dfs(vector< vector<int> > &plane, int x, int y){
  plane[x][y] = 0;
  for(int i=0; i<8; i++){
    if(0 <= x+dx[i] && x+dx[i] < h && 0 <= y+dy[i] && y+dy[i] < w)
      if(plane[x + dx[i]][y + dy[i]] == 1)
        dfs(plane, x + dx[i], y + dy[i]);
  }

  return;
}

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

    vector< vector<int> > plane(h, vector<int>(w));
    for(int i=0; i<h; i++)
      for(int j=0; j< w; j++)
        cin >> plane[i][j];

    int ans = 0;
    for(int i=0; i<h; i++){
      for(int j=0; j<w; j++){
        if(plane[i][j] == 1){
          dfs(plane, i, j);
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
  
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;

#define rep(i, n) for (int i = 0; i < n; ++i)
#pragma region Debug
template <typename T>
void view(const std::vector<T> &v)
{
    for (const auto &e : v)
    {
        std::cout << e << " ";
    }
    std::cout << std::endl;
}
template <typename T>
void view(const std::vector<std::vector<T>> &vv)
{
    for (const auto &v : vv)
    {
        view(v);
    }
}
#pragma endregion

vector<vector<int>> m;

void dfs(int x, int y, int h, int w)
{
    m[y][x] = 0;

    for (int i = max(0, y - 1); i <= min(h - 1, y + 1); ++i)
    {
        for (int j = max(0, x - 1); j <= min(w - 1, x + 1); ++j)
        {
            if (m[i][j])
            {
                // cout << y << " " << x << "->" << i << " " << j << endl;
                dfs(j, i, h, w);
            }
        }
    }
}

int main()
{
    int w = 1, h = 1;
    while (true)
    {
        cin >> w >> h;
        if (w == 0 && h == 0)
        {
            break;
        }
        m = vector<vector<int>>(h, vector<int>(w));
        rep(i, h) rep(j, w) cin >> m[i][j];

        int ans = 0;
        rep(i, h) rep(j, w)
        {
            if (m[i][j])
            {
                ans++;
                dfs(j, i, h, w);
            }
        }

        cout << ans << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
#define LOOP(n) for (int _i = 0; _i < (n); _i++)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, r, n) for (int i = (r); i < (n); ++i)
#define ALL(obj) begin(obj), end(obj)
using namespace std;
using ll = long long;
using ull = unsigned long long;

const int INF = 2100100100;
const int MOD = 1e9 + 7;

// 多次元 vector 生成
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
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 N, M, K, Q, W, H, R, C;
int table[55][55];
bool used[55][55];

void dfs(int x, int y) {
    used[x][y] = true;
    vector<int> dx = {1, -1, 0, 0, 1, 1, -1, -1};
    vector<int> dy = {0, 0, 1, -1, 1, -1, 1, -1};
    REP(i, 8) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (0 <= nx && nx < H && 0 <= ny && ny < W && !used[nx][ny] && table[nx][ny] == 1) {
            dfs(nx, ny);
        }
    }
}

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

    vector<int> ans;
    while (true) {
        cin >> W >> H;
        if (W == 0 && H == 0) break;
        memset(used, 0, sizeof(used));
        REP(i, H) {
            REP(j, W) { cin >> table[i][j]; }
        }

        int cnt = 0;
        REP(i, H) {
            REP(j, W) {
                if (table[i][j] == 1 && !used[i][j]) {
                    dfs(i, j);
                    cnt++;
                }
            }
        }
        ans.push_back(cnt);
    }
    for (auto i : ans) {
        cout << i << endl;
    }

    return 0;
}