Go: // AOJ 1160: How Many Islands? http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160

package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"strconv"
)

var SEA = 0
var LAND = 1

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func discoverIslands(M, V [][]int, H, W int) int {
	// 島を探す
	queue := list.New()
	islands := 0
	for i := 1; i <= H; i++ {
		for j := 1; j <= W; j++ {
			// 訪問済みの土地ならスキップ
			if V[i][j] == 1 {
				continue
			}

			// 訪問中の土地が海なら訪問済みにしてスキップ
			if M[i][j] == SEA {
				V[i][j] = 1
				continue
			}

			// 未訪問の陸地が見つかったので
			// 島の数を1つ増やし、この陸地から辿れる陸地を訪問していく
			islands++
			queue.PushBack([]int{i, j})

			for queue.Len() > 0 {
				cur := queue.Remove(queue.Front()).([]int)
				h, w := cur[0], cur[1]
				if V[h][w] == 1 {
					continue
				}
				V[h][w] = 1

				// 全方向に未訪問の陸地がないか探索
				for v := -1; v <= 1; v++ {
					for l := -1; l <= 1; l++ {
						dh := h + v
						dw := w + l
						if M[dh][dw] == LAND && V[dh][dw] == 0 {
							queue.PushBack([]int{dh, dw})
						}
					}
				}
				/*
					if M[h-1][w] == LAND {
						queue.PushBack([]int{h - 1, w})
					}
					if M[h-1][w+1] == LAND {
						queue.PushBack([]int{h - 1, w + 1})
					}
					if M[h][w+1] == LAND {
						queue.PushBack([]int{h, w + 1})
					}
					if M[h+1][w+1] == LAND {
						queue.PushBack([]int{h + 1, w + 1})
					}
					if M[h+1][w] == LAND {
						queue.PushBack([]int{h + 1, w})
					}
					if M[h+1][w-1] == LAND {
						queue.PushBack([]int{h + 1, w - 1})
					}
					if M[h][w-1] == LAND {
						queue.PushBack([]int{h, w - 1})
					}
					if M[h-1][w-1] == LAND {
						queue.PushBack([]int{h - 1, w - 1})
					}
				*/
			}
		}
	}
	return islands
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	for {
		W, H := nextInt(sc), nextInt(sc)

		if W == 0 && H == 0 {
			break
		}
		// Initialize map and visted list
		M := make([][]int, H+2)
		V := make([][]int, H+2)
		for i := 0; i < H+2; i++ {
			M[i] = make([]int, W+2)
			V[i] = make([]int, W+2)
		}
		for i := 1; i <= H; i++ {
			for j := 1; j <= W; j++ {
				M[i][j] = nextInt(sc)
			}
		}

		islands := discoverIslands(M, V, H, W)

		fmt.Println(islands)
	}
}

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

}