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++: //http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp
//How Many Islands?
#include <bits/stdc++.h>
#if LOCAL
#include "dump.hpp"
#else
#define dump(...)
#endif
using namespace std;
using ll=long long;
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
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;}

//2重配列上でdfs
bool solve2(){
    ll w,h; cin>>w>>h;
    if(w==0&&h==0)return false;//終わり
    vector<vector<ll>> chw(h,vector<ll>(w));
    FOR(y,0,h){
        FOR(x,0,w){
            ll c; cin>>c;
            chw.at(y).at(x)=c;
        }
    }

    auto available_node = [&](ll x,ll y)->bool{
        if(x<0||x>=w)return false;
        if(y<0||y>=h)return false;
        return chw.at(y).at(x);
    };

    function<void(ll,ll)> dfs = [&](ll x,ll y){
        if(not available_node(x,y)) return;
        chw.at(y).at(x)=0;//訪問済、flag落す
        pair<ll,ll> ofstbl[] = {
            {0,1},{0,-1},{1,0},{-1,0},
            {1,1},{1,-1},{-1,1},{-1,-1},        
        };
        for(const auto&p:ofstbl){
            dfs(x+p.first,y+p.second);
        } 
    };
    ll ans=0;
    FOR(y,0,h){
        FOR(x,0,w){
            if(available_node(x,y)){
                ++ans;
                dfs(x,y);
            }
        }
    }
    cout<<ans<<endl;

    return true;
}
int main(){
    while(solve2()){
    }
    return 0;
}

//template graph使ってるけど冗長
template< typename T >
struct edge {
	int src, to;
	T cost;
	edge(int _to, T _cost) : src(-1), to(_to), cost(_cost) {}
	edge(int _src, int _to, T _cost) : src(_src), to(_to), cost(_cost) {}
	edge &operator=(const int &x) {
		to = x;
		return *this;
	}
	operator int() const { return to; }
};

template< typename T>
struct node {
	vector<edge<T>> edges;
    bool flag=false;;
};

template< typename T >
using Edges = vector< edge< T > >;
template< typename T >
using WeightedGraph = vector< node< T > >;
using UnWeightedGraph = vector< vector< int > >;
template< typename T >
using Matrix = vector< vector< T > >;

bool solve(){
    ll w,h; cin>>w>>h;
    if(w==0&&h==0)return false;//終わり
    vector<vector<ll>> chw(h,vector<ll>(w));
    ll node_num=0;
    FOR(y,0,h){
        FOR(x,0,w){
            ll c; cin>>c;
            if(c==1)++node_num;
            chw.at(y).at(x)=c;
        }
    }

    auto pos2id = [&](ll x,ll y)->ll{
        return y*w+x;
    };
    auto available_node = [&](ll x,ll y)->bool{
        if(x<0||x>=w)return false;
        if(y<0||y>=h)return false;
        return chw.at(y).at(x);
    };
    ll node_max=w*h;
    WeightedGraph<ll> graph(node_max);
    pair<ll,ll> ofstbl[] = {
        {0,1},{0,-1},{1,0},{-1,0},
        {1,1},{1,-1},{-1,1},{-1,-1},        
    };
    FOR(y,0,h){
        FOR(x,0,w){
            if (available_node(x,y)) {
                ll xy=pos2id(x,y);
                auto&& node=graph.at(xy);
                node.flag=true;
                for(const auto& o:ofstbl){
                    ll nx =x+o.first;
                    ll ny =y+o.second;
                    if(available_node(nx,ny)){
                        ll nxy=pos2id(nx,ny);
                        node.edges.emplace_back(nxy,1);
                    }
                }
            }
        }
    }

    function<void(ll)> dfs = [&](ll id){
        auto& node=graph.at(id);
        if (node.flag==false)return; //訪問済orない
        node.flag=false;
        for(const auto& e :graph.at(id).edges){
            dfs(e);
        }
    };

    ll ans=0;
    FOR(i,0,node_max){
        if(graph.at(i).flag){
            ++ans;
            dfs(i);
        }
    }
    cout<<ans<<endl;

    return true;
}

int main_() {
#if LOCAL&01
    //std::ifstream in("./test/sample-1.in");
    std::ifstream in("./input.txt");
    std::cin.rdbuf(in.rdbuf());
#else
    cin.tie(0);
    ios::sync_with_stdio(false);
#endif
    while(solve()){
    }
    return 0;
}

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

int c[52][52];
int dfs(int x,int y){
	if( c[y][x] == 0 ) return 0;
	c[y][x] = 0;
	for(int i = -1 ; i <= 1 ; i++)
		for(int j = -1 ; j <= 1 ; j++)
			dfs(x+i,y+j);

}
int main(){
	int w,h;
	while(cin >> w >> h and w){
		memset(c,0,sizeof(c));
		for(int i = 0 ; i < h ; i++){
			for(int j = 0 ; j < w ; j++){
				cin >> c[i+1][j+1];
			}
		}
		int ans = 0;
		for(int i = 1 ; i <= h ; i++){
			for(int j = 1 ; j <= w ; j++){
				if( c[i][j] ){
					ans++;
					dfs(j,i);
				}
			}
		}
		cout << ans << endl;
	}

}

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

typedef vector<vector<int> > VII;
typedef pair<int, int> PII;

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

void bfs (int i, int j, VII &table) {
    queue<PII> que;
    que.push(make_pair(i, j));
    table[i][j] = 0;
    while (1) {
        if (que.empty() == 1) {
            break; 
        }
        int now_y = que.front().first;
        int now_x = que.front().second;
        que.pop();

        for (int k = 0; k < 8; k++) {
            if (table[now_y + dy[k]][now_x + dx[k]] == 1) {
                que.push(make_pair(now_y + dy[k], now_x + dx[k]));
                    table[now_y + dy[k]][now_x + dx[k]] = 0;
            }
        }
    }
    return;
}

int main(void) {
    int w;
    int h;
    while (cin >> w >> h) {
        if (w == 0 && h == 0) {
            break;
        }
        VII table(h + 2, vector<int>(w + 2, 0));
        for (int i = 1; i < h + 1; i++) {
            for (int j = 1; j < w + 1; j++) {
                cin >> table[i][j];
            }
        }
        int number = 0;
        for (int i = 1; i < h + 1; i++) {
            for (int j = 1; j < w + 1; j++) {
                if (table[i][j] == 1) {
                    bfs(i, j, table);
                    number++;
                }
            }
        }
        cout << number << endl;
    }
    return 0;
}