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##_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;
  }
}

C++: #include <iostream>
#include <cstdio>
#include <iomanip>

#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>

#include <numeric>
#include <algorithm>
#include <functional>

#include <cctype>

#include <complex>
#include <string>
#include <sstream>

using namespace std;

#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(),c.rend()
#define rep(i,n) for(unsigned int i=0;i<(n);i++)
#define tr(it,container) for(typeof(container.begin()) it = container.begin(); \
                                                  it != container.end(); ++it)
#define mp(a,b) make_pair((a),(b))

typedef long long ll;
typedef complex<double> P;
const int dx[] = {1,0,-1,0};
const int dy[] = {0,-1,0,1};
const double EPS = 1e-9;
const int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
const int daysleap[] = {31,29,31,30,31,30,31,31,30,31,30,31};

const int dx2[] = {1,1,1,0,0,0,-1,-1,-1};
const int dy2[] = {1,0,-1,1,0,-1,1,0,-1};

void go(vector<vector<int> > &F,int h,int w){
    if(F[h][w]==0) return;
    F[h][w] = 0;
    for(int i=0;i<9;i++){
        go(F,h+dy2[i],w+dx2[i]);
    }
}
int main(){
    while(true){
        int w,h;
        cin >> w >> h;
        if(w==0) break;
        vector<vector<int> > F(h+2,vector<int>(w+2,0));
        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                int x;
                cin >> x;
                F[i][j] = x;
            }
        }
        int c=0;
        for(int i=0;i<h+2;i++){
            for(int j=0;j<w+2;j++){
                if(F[i][j] == 1){
                    go(F,i,j);
                    c++;
                }
            }
        }
        cout << c << endl;
    }
    return 0;
}

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

vector<int> dx = {-1, -1, -1, 0, 1, 1, 1, 0};
vector<int> dy = {-1, 0, 1, 1, 1, 0, -1, -1}; 
int w, h;
int seen[55][55];

void dfs(const Graph &G, int x, int y){
    seen[y][x] = 1;
    for(int i = 0; i < 8; i++){
        if(x + dx[i] < 0 || x + dx[i] >= w || y + dy[i] < 0 || y + dy[i] >= h || G[y + dy[i]][x + dx[i]] == 0) continue;
        if(seen[y + dy[i]][x + dx[i]] == 1) continue;
        dfs(G, x + dx[i], y + dy[i]);
    }
}

int main(){
    while(true){
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        Graph G(h);
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                int k; cin >> k;
                G[i].push_back(k);
            }
        }
        for(int i = 0; i < 55; i++){
          for(int j = 0; j < 55; j++){
            seen[i][j] = 0;
          }
        }
        int cnt = 0; 
        for(int l = 0; l < h; l++){
            for(int m = 0; m < w; m++){
                if(G[l][m] == 1 && seen[l][m] == 0){
                    dfs(G, m, l);
                    cnt++;
                }
            }
        }
        cout << cnt << endl;
    }
}