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<utility>
#include<queue>
#include<map>
#define h first
#define w second
using namespace std;
typedef pair<int, int> P;

const int MAX_H = 55;
const int MAX_W = 55;

int grid[MAX_H][MAX_W];
bool is_checked[MAX_H][MAX_W];

void solve(int W, int H) {
    map<P, bool> is_exist;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> grid[i][j];
            is_checked[i][j] = false;
            is_exist[P(i, j)] = true;
        }
    }
    queue<P> Q;
    int land_cnt = 0;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (grid[i][j] == 1 && !is_checked[i][j]) {
                Q.push(P(i, j));
                is_checked[i][j] = true;
                while (!Q.empty()) {
                    P now_pos = Q.front();
                    Q.pop();
                    for (int k = -1; k <= 1; k++) {
                        for (int l = -1; l <= 1; l++) {
                            P new_pos = P(now_pos.h + k, now_pos.w + l);
                            if (!is_exist[new_pos]) continue;
                            if (is_checked[new_pos.h][new_pos.w]) continue;
                            if (grid[new_pos.h][new_pos.w] == 0) continue;
                            Q.push(new_pos);
                            is_checked[new_pos.h][new_pos.w] = true;
                        }
                    }
                }
                land_cnt++;
            }
        }
    }
    cout << land_cnt << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int W, H;
    while (true) {
        cin >> W >> H;
        if (W == 0 && H == 0) break;
        solve(W, H);
    }
    return 0;
}

C++: # include <bits/stdc++.h>
# define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
# define Find(v,key) ((v).find(key) != (v).end())
# define cauto const auto
# define int long long
using namespace std;
template <class Type> inline constexpr bool InRange(const Type& x, const Type& fst, const Type& lst) { return (fst <= x) && (x < lst); }
void _cin(){} template <class Head, class... Tail> void _cin(Head&& head, Tail&&... tail){ cin >> head; _cin(forward<Tail>(tail)...); }
#define Cin(Type, ...) Type __VA_ARGS__; _cin(__VA_ARGS__)
#define Cinvv(Type, xs, h, w) vector<vector<Type>> xs(h, vector<int>(w)); rep(i, h) rep(j, w) cin >> xs[i][j]

# define mp(f,s) make_pair(f, s)

using P = pair<int, int>;
# define Y first
# define X second

struct phash {
    inline size_t operator ()(const pair<int,int>& p) const {
        const auto h1 = hash<int>()(p.first);
        const auto h2 = hash<int>()(p.second);
        return h1 ^ (h2 << 1);
    }
};

bool can(const vector<vector<int>>& c, int h, int w, P pos) {
    return InRange(pos.Y, (int)0, h)
       and InRange(pos.X, (int)0, w)
       and c[pos.Y][pos.X] == 1;
}

signed main() {
    while (true) {
        Cin(int, w, h);
        if (w == 0 and h == 0) break;
        Cinvv(int, c, h, w);

        set<P> visited;
        int ans = 0;
        rep (y, h) rep (x, w) {
            stack<P> stk;
            P ini = mp(y, x);
            if (!Find(visited, ini) and can(c, h, w, ini)) ans++;
            else continue;
            stk.push(ini);
            visited.insert(ini);

            while (!stk.empty()) {
                P now = stk.top(); stk.pop();
                for (int dy = -1; dy <= 1; ++dy) {
                    for (int dx = -1; dx <= 1; ++dx) {
                        P nxt = mp(now.Y + dy, now.X + dx);
                        if (!can(c, h, w, nxt)) continue;
                        if (Find(visited, nxt)) continue;
                        stk.push(nxt);
                        visited.insert(nxt);
                    }
                }
            }
        }

        cout << ans << "\n";
    }

    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<cstdlib>

#define LOOP(n,cnt) for(int cnt=0;cnt<n;++cnt)

using namespace std;

template <typename TYPE>
void printArray(TYPE* array, int size){
  LOOP(size,i) cout << array[i] << "\t" << flush;
  cout << endl;
}

int StringToInt(string str){
  return atoi(str.c_str());
}

#define MAX_W 50
#define MAX_H 50
int w,h,c[MAX_W][MAX_H];
int count = 0;

void dfs(int sy, int sx){
  for(int i=-1;i<=1;i++){
    for(int j=-1;j<=1;j++){
      int y = sy+i;
      int x = sx+j;
      if(y>=0 && x>=0 && y<h && x<w){
	if(c[y][x] == 1){
	  c[y][x] = 0;
	  dfs(y,x);         
	}
      }
    }
  }
}

int main(){
  while(true){
    cin >> w;
    cin >> h;
    count = 0;
    if(w == 0 && h == 0) break;
    LOOP(h,y){
      LOOP(w,x){
	cin >> c[y][x];
      }    
    }
    
    LOOP(h,y){
      LOOP(w,x){
	if(c[y][x] == 1){
	  count++;
	  dfs(y,x);
	}
      }    
    }
    cout << count << endl;
  }
}