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 <iostream>
#include <vector>
#include <string>
#include <utility>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <climits>
//#include "h.h"
using namespace std;

#define ll long long
#define pint pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define pm(first, second) pb(mp(first, second))
#define SPACE " "
#define fpf first.first
#define fps first.second
#define spf second.first
#define sps second.second

#define all(X) (X).begin(), (X).end()

//parent[i]:ノード番号iの親のノード番号
//親が存在しないときは-(属するグループのサイズ)を格納
//parent = vector<int>(N, -1);で初期化
vector<int> parent;
//ノードXの根を返す
int root(int X){
	if(parent[X] < 0)return X;
	return parent[X] = root(parent[X]);
}
//ノードXが属するグループのサイズ(属するノード数)を返す
int size(int X){
	return -parent[root(X)];
}
//Xの属するグループとYの属するグループを繋ぐ
//それぞれの根どうしを繋ぐ
bool connect(int X, int Y){
	//根同士をつなぐため指すノードを根にする
	X = root(X);
	Y = root(Y);
	//すでに繋がっているときは繋がずにfalseを返す
	if(X == Y){
		return false;
	}
	//大きいほうに小さいほうを繋ぎたいので逆ならスワップ
	if(size(X) < size(Y))swap(X, Y);
	//繋がれる方のサイズを更新
	parent[X] += parent[Y];
	//繋ぐ方の親を設定
	parent[Y] = X;
	return true;
}
int main(){

	while(true){
		int W, H;
		cin >> W >> H;
		if(W == 0 && H == 0)break;
		vector<vector<int>> C(H, vector<int>(W));
		int landNum = 0;
		for(int i = 0; i < H; i ++){
			for(int j = 0; j < W; j ++){
				cin >> C[i][j];
				if(C[i][j] == 1)landNum ++;
			}
		}
		parent = vector<int>(H * W, -1);
		for(int i = 0; i < H; i ++){
			for(int j = 0; j < W; j ++){
				if(C[i][j] == 1){
					if(i + 1 < H && C[i + 1][j] == 1)connect((i + 1) * W + j, i * W + j);
					if(i - 1 >= 0 && C[i - 1][j] == 1)connect((i - 1) * W + j, i * W + j);
					if(j + 1 < W && C[i][j + 1] == 1)connect(i * W + (j + 1), i * W + j);
					if(j - 1 >= 0 && C[i][j - 1] == 1)connect(i * W + (j - 1), i * W + j);
					if(i + 1 < H && j + 1 < W && C[i + 1][j + 1] == 1)connect((i + 1) * W + (j + 1), i * W + j);
					if(i + 1 < H && j - 1 >= 0 && C[i + 1][j - 1] == 1)connect((i + 1) * W + (j - 1), i * W + j);
					if(i - 1 >= 0 && j + 1 < W && C[i - 1][j + 1] == 1)connect((i - 1) * W + (j + 1), i * W + j);
					if(i - 1 >= 0 && j - 1 >= 0 && C[i - 1][j - 1] == 1)connect((i - 1) * W + (j - 1), i * W + j);
				}
			}
		}
		map<int, int> m;
		for(int i = 0; i < H; i ++){
			for(int j = 0; j < W; j ++){
				if(C[i][j] == 1)m[root(i * W + j)] ++;
			}
		}
		int ans = 0;
		for(auto itr = m.begin(); itr != m.end(); itr ++){
			if(itr -> second > 0)ans ++;
		}
		cout << ans << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<cstdio>
#include<queue>
#include<deque>
#include<map>
#include<stack>
#include<set>

using namespace std;

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

int w,h;
vector<vector<int> > c(50,vector<int>(50,0));

void dfs(int y,int x){
    c[y][x]=0;
    for(int dy=-1;dy<=1;dy++){
        for(int dx=-1;dx<=1;dx++){
            int ny=y+dy,nx=x+dx;
            if(ny>=0&&ny<h&&nx>=0&&nx<w&&c[ny][nx]==1){
                dfs(ny,nx);
            }
        }
    }
    return ;
}

int main(){
    cin>>w>>h;
    while(!(w==0&&h==0)){
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin>>c[i][j];
            }
        }
        int cnt=0;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(c[i][j]==1){
                    dfs(i,j);
                    cnt++;
                }
            }
        }
        cout<<cnt<<endl;
        cin>>w>>h;
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>

using namespace std;

void search(int w, int h, const vector< vector<int> >& area_map, vector< vector<int> >& visited, int W, int H) {
    visited[h][w] = 1;
    int dw[] = {1, 1, 0, -1, -1, -1, 0, 1}, dh[] = {0, 1, 1, 1, 0, -1, -1, -1};
    for(int i = 0; i < 8; i++) {
        int nw = w + dw[i], nh = h + dh[i];
        if(0 <= nw && nw < W && 0 <= nh && nh < H && visited[nh][nw] != 1 && area_map[nh][nw] == 1) {
            search(nw, nh, area_map, visited, W, H);
        }
    }
}

int main() {
    while(true) {
        int w, h; 
        cin >> w >> h;
        if(w == 0 && h == 0) break;

        vector< vector<int> > area_map, visited;
        for(int i = 0; i < h; i++) {
            vector<int> temp;
            for(int j = 0; j < w; j++) {
                int e; cin >> e;
                temp.push_back(e);
            }
            area_map.push_back(temp);
            visited.push_back(vector<int>(w));
        }
        int ans = 0;
        for(int i = 0; i < h; i++) {
            for(int j = 0; j < w; j++) {
                if(area_map[i][j] == 1 && visited[i][j] != 1) {
                    search(j, i, area_map, visited, w, h);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<string.h>
using namespace std;

int h,w;

int v[50][50];

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

void calc(int y,int x){
	if(v[y][x]==0) return;
	v[y][x]=0;
	for(int r=0;r<8;r++){
		int nx=x+dx[r],ny=y+dy[r];
		if(nx<0 || ny<0 || nx>=w || ny>=h)
			continue;
		calc(ny,nx);
	}
}


void solve()
{
	memset(v,0,sizeof(v));
	for(int y=0;y<h;y++){
		for(int x=0;x<w;x++){
			int t;cin>>t;v[y][x]=t;
		}
	}
	int res=0;

	for(int y=0;y<h;y++)
		for(int x=0;x<w;x++){
			if(v[y][x]==0) continue;
			res++;
			calc(y,x);
		}
		cout<<res<<endl;
}



int main()
{
	while(cin>>w>>h){
		if(w==0) break;
		solve();
	}
	return 0;
}