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>
using namespace std;
#define int long long
#define rep(i, a, b) for (int i = (a); i < ((int)b); ++i)
#define rrep(i, a, b) for (int i = (int)(b) - 1; i >= ((int)a); --i)
#define sz(c) ((int)c.size())
#define all(c) c.begin(),c.end()

using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

void dump_impl(string str) {}
template <class H, class... T>
void dump_impl(string s, const H &head, const T&... tail) {
    int par = 0;
    rep(i, 0, sz(s)) {
        char ch = s[i];
        if (ch == ',' && par == 0) {
            cerr << " = " << head << ", ";
            dump_impl(s.substr(i + 1), tail...);
            return;
        } else {
            cerr << ch;
            if (ch == '(') par++;
            if (ch == ')') par--;
        }
    }
}
#define dump(...) do { cerr << "\x1b[33;1m"; dump_impl(#__VA_ARGS__ ",", __VA_ARGS__); cerr << "\x1b[0m" << endl; } while (0)

signed main() {
    int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
    int dy[] = {-1, 0, 1, 0, -1, 1, 1, -1};
    while (1) {
        int W, H;
        cin >> W >> H;
        if (W == 0 && H == 0) break;
        vvi A(H, vi(W));
        rep(i, 0, H) {
            rep(k, 0, W) {
                cin >> A[i][k];
            }
        }
        vector<vector<bool>> vis(H, vector<bool>(W));
        int ans = 0;
        rep(y, 0, H) {
            rep(x, 0, W) {
                if (vis[y][x]) continue;
                if (A[y][x] == 0) continue;
                queue<pii> q;
                q.push({y, x});
                vis[y][x] = true;
                while (sz(q)) {
                    int py, px;
                    tie(py, px) = q.front(); q.pop();
                    rep(dir, 0, 8) {
                        int yy = py + dy[dir];
                        int xx = px + dx[dir];
                        if (yy < 0 || xx < 0 || yy >= H || xx >= W) continue;
                        if (vis[yy][xx]) continue;
                        if (A[yy][xx] == 0) continue;
                        q.push({yy, xx});
                        vis[yy][xx] = true;
                    }
                }
                ans++;
            }
        }
        cout << ans << endl;
    }
}

C++: #include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <functional>
#include <vector>
#include <queue>
#include <string>
#include <complex>

#define REP(i,n) for (int i = 0; i < (n); i++)
#define FOR(i,s,n) for (int i = (s); i < (n); i++)
#define ALLOF(c) c.begin(), c.end()
#define RANGEOF(c,i,n) c.begin() + (i), c.begin() + (i) + (n)
#define EPS 1e-9
#define INF 1000000000
using namespace std;

template <class T> T read() { T t; cin >> t; return t; }
template <class T> vector<T> read(int n) { vector<T> v; REP(i,n) { v.push_back(read<T>()); } return v; }
template <class T> vector<vector<T>> read(int n, int m)
{
	vector<vector<T>> v;
	REP(i,n) { v.push_back(read<T>(m)); }
	return v;
}

template <class T> void write(const T &t) { cout << t << endl; }
template <class T> void write(const T &t, const T &t2) { cout << t << ' ' << t2 << endl; }
template <class T> void write(const vector<T> &v)
{
	ostringstream ss; 
	for (auto x : v) ss << x << ' ';
	auto s = ss.str();
	cout << s.substr(0, s.length() - 1) << endl;
}

struct _Reader { template <class T> _Reader operator ,(T &rhs) { cin >> rhs; return *this; } };
#define READ(t,...) t __VA_ARGS__; _Reader(), __VA_ARGS__

void solve();
int main() { solve(); return 0; }

/// template end ///



void solve()
{
	while (true)
	{
		READ(int, H, W);
		if (!(W|H)) { break; }
		auto field = read<char>(W, H);
		for (auto &f : field) { f.insert(f.begin(), '0'); f.push_back('0'); }
		field.insert(field.begin(), vector<char>(H+2, '0'));
		field.push_back(vector<char>(H+2, '0'));
		W += 2; H += 2;

		int count = 0;
		REP(sx,W) REP(sy,H) if (field[sx][sy] == '1')
		{
			typedef complex<int> P;
			queue<P> q;
			q.push(P(sx, sy));
			while (!q.empty())
			{
				P cur = q.front(); q.pop();
				if (field[cur.real()][cur.imag()] == '0') { continue; }
				field[cur.real()][cur.imag()] = '0';
				P d[] = { P(-1,-1), P(0,-1), P(1,-1), P(-1, 0), P(1, 0), P(-1,1), P(0,1), P(1,1) };
				REP(i, 8)
				{
					P next = cur + d[i];
					if (field[next.real()][next.imag()] == '1') q.push(next);
				}
			}
			count++;
		}
		write(count);
	}
}

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

int w,h;
bool map[50][50];

void Del(int x,int y){

  map[y][x]=false;

  if(x!=w-1){
    if(map[y][x+1]==true){
      Del(x+1,y);
    }

    if(y!=0){
      if(map[y-1][x+1]==true){
        Del(x+1,y-1);
      }
    }
    if(y!=h-1){
      if(map[y+1][x+1]==true){
        Del(x+1,y+1);
      }
    }

  }


  if(x!=0){
    if(map[y][x-1]==true){
      Del(x-1,y);
    }
    if(y!=0){
      if(map[y-1][x-1]==true){
        Del(x-1,y-1);
      }
    }
    if(y!=h-1){
      if(map[y+1][x-1]==true){
        Del(x-1,y+1);
      }
    }

  }


  if(y!=h-1){
    if(map[y+1][x]==true){
      Del(x,y+1);
    }
  }
  if(y!=0){
    if(map[y-1][x]==true){
      Del(x,y-1);
    }
  }

}


int main(){


  int sum;

  while(cin >> w >> h){
    if(w==0 && h==0){ break; }

    for(int i=0; i<h; i++){
      for(int j=0; j<w; j++){
        cin >> map[i][j];
      }
    }

    sum = 0;

    for(int i=0; i<h; i++){
      for(int j=0; j<w; j++){
        if(map[i][j]==1){
          sum++;
          Del(j,i);
        }
      }
    }

    cout << sum << endl;

  }



  return 0;
}