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 <queue>

using namespace std;

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

        // bfs
        int islandCount = 0;
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                queue<pair<int, int> > que;
                if (map[i][j] != 1) {
                    continue;
                }
                ++islandCount;
                que.push({i, j});
                while (!que.empty()) {
                    pair<int, int> p = que.front(); que.pop();
                    if (map[p.first][p.second] == -1) {
                        continue;
                    }
                    map[p.first][p.second] = -1;
                    for (int k = -1; k < 2; ++k) {
                        for (int l = -1; l < 2; ++l) {
                            if ((k | l) == 0) {
                                continue;
                            }
                            int ny = p.first + k;
                            int nx = p.second + l;
                            if (0 <= nx && nx < w &&
                                    0 <= ny && ny < h) {
                                if (map[ny][nx] == 1) {
                                    que.push({ny, nx});
                                }
                            }
                        }
                    }
                }
            }
        }
        cout << islandCount << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <utility>

using namespace std;

bool check(bool[50][50],pair<int,int>);
void rmv(bool[50][50],int,int);

int main(void){
	for(int w,h;cin>> w >> h,w||h;){
		bool fld[50][50]={0};
		int n;

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

		for(n=0;check(fld,make_pair(w,h));n++);
		cout << n << endl;
	}
	return 0;
}

bool check(bool fld[50][50],pair<int,int> wide){
	for(int i=0;i<wide.second;i++)
		for(int j=0;j<wide.first;j++)
			if(fld[i][j]){
				rmv(fld,i,j);
				return true;
			}

	return false;
}

void rmv(bool fld[50][50],int y,int x){
	if(x>=0&&y>=0&&x<=50&&y<=50)
		if(fld[y][x]){
			fld[y][x]=false;
			for(int i=-1;i<=1;i++)
				for(int j=-1;j<=1;j++)
					rmv(fld,y+i,x+j);
		}
}

C++: #include<iostream>
#include<vector>
#include<stdio.h>
using namespace std;
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define DEBUG(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

vector<vector<int> > map;
vector<vector<bool> > reach;
int w,h;

void dfs(int i, int j){
    if((reach[i][j]) or (map[i][j]==0))
        return;
    reach[i][j] = true;
    if(0<i && 0<j) dfs(i-1,j-1);
    if(0<i) dfs(i-1,j);
    if(0<i && j<w-1) dfs(i-1,j+1);
    if(0<j) dfs(i, j-1);
    if(j<w-1) dfs(i, j+1);
    if(i<h-1 && 0<j) dfs(i+1,j-1);
    if(i<h-1) dfs(i+1,j);
    if(i<h-1 && j<w-1) dfs(i+1,j+1);
}

int main(){

    while((cin >> w >> h) && (w>0 && h>0)){
        int res=0;
        for (int i = 0; i < h; i++) {
            vector<int> temp(w,0);
            for (int j = 0; j < w; j++)
                cin >> temp[j];
            map.push_back(temp);
        }
        for (int i = 0; i < h; i++){
            vector<bool> temp(w,false);
            reach.push_back(temp);
        }
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if(map[i][j]==1 && !reach[i][j]){
                    res++;
                    dfs(i,j);
                }
            }
        }
        cout << res << endl;
        map.clear();
        reach.clear();

    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <cstring>
#include <cctype>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <complex>
using namespace std;
#define REP(i,n) for(int i = 0; i < (int)n; i++)
#define FOR(i,a,b) for(int i = a; i < (int)b; i++)
const int INF = 1<<28;

int m[50][50];
int w, h;
int cnt;

void dfs(int x, int y, int z) {
	if(m[y][x]) {
		if(!z)
			cnt++;
		m[y][x] = false;
		if(y > 0 && m[y-1][x])
			dfs(x, y-1, 1);
		if(y > 0 && x > 0 && m[y-1][x-1])
			dfs(x-1, y-1, 1);
		if(x > 0 && m[y][x-1])
			dfs(x-1, y, 1);
		if(x > 0 && y < h-1 && m[y+1][x-1])
			dfs(x-1, y+1, 1);
		if(x < w-1 && m[y][x+1])
			dfs(x+1, y, 1);
		if(x < w-1 && 0 < y && m[y-1][x+1])
			dfs(x+1, y-1, 1);
		if(y < h-1 && m[y+1][x])
			dfs(x, y+1, 1);
		if(y < h-1 && x < w-1 && m[y+1][x+1])
			dfs(x+1, y+1, 1);
	}
	return;
}

int main() {
	while(cin >> w >> h, w|h) {
		REP(i, h) REP(j, w)
			cin >> m[i][j];

		cnt = 0;
		REP(i, h) REP(j, w)
			dfs(j, i, 0);

		cout << cnt << endl;
	}
	return 0;
}