Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

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