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 <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

void solve(int cur_x, int cur_y, vector<vector<bool>> &visited,
        const int H, const int W, const vector<vector<int>> &field) {
    visited[cur_y][cur_x] = true;

    vector<pair<int, int>> diff_xy = {
        {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}, {-1, 0}, {-1, 1}
    };
    for (int i = 0; i < (int) diff_xy.size(); i++) {
        int next_x = cur_x + diff_xy[i].first;
        int next_y = cur_y + diff_xy[i].second;
        if (next_x < 0 || next_x >= W || next_y < 0 || next_y >= H) continue;
        if (visited[next_y][next_x]) continue;
        if (field[next_y][next_x] == 0) continue;
        solve(next_x, next_y, visited, H, W, field);
    }
}

int main(void) {
    while (1) {
        int w, h;
        cin >> w >> h;

        if (w == 0 && h == 0) break;

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

        vector<vector<bool>> visited(h, vector<bool>(w, false));
        int count = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (visited[i][j]) continue;
                if (field[i][j] == 0) continue;
                solve(j, i, visited, h, w, field);
                count++;
            }
        }

        cout << count << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
int GCD(int a,int b){return b?GCD(b,a%b):a;}
int LCM(int a,int b){return (a*b)/GCD(a,b);}
int abs(int x){return x>0?x:-x;}
 
const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};

int sea[50][50];
int w, h;
void dfs(int x, int y)
{
	sea[x][y] = 0;
	for (int i = 0; i < 8; i++){
		int nx = x + dx[i], ny = y + dy[i];
		if (0 <= nx and nx < h and 0 <= ny and ny < w and sea[nx][ny] == 1){
			dfs(nx, ny);
		}
	}

	return;
}

int main()
{
	while (1){
		int ans = 0;
		cin >> w >> h;
		if (!w and !h) return 0;
		REP(i, h){
			REP(j, w){
				cin >> sea[i][j];
			}
		}

		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (sea[i][j] == 1){
					dfs(i, j);
					ans++;
				}
			}
		}

		cout << ans << endl;
	}

}

C++: #include <cstdio>

int w, h, c[50][50], count = 0;

void search(int x, int y)
{
    c[y][x] = 0;

    for (int dx = -1; dx <= 1; ++dx)
    {
        for (int dy = -1; dy <= 1; ++dy)
        {
            if (x + dx < 0 || x + dx >= w || y + dy < 0 || y + dy >= h)
            {
                continue;
            }

            if (c[y + dy][x + dx] == 1)
            {
                search(x + dx, y + dy);
            }
        }
    }
}

bool sub_search()
{
    for (int i = 0; i < h; ++i)
    {
        for (int j = 0; j < w; ++j)
        {
            if (c[i][j] == 1)
            {
                search(j, i);
                count++;
                return true;
            }
        }
    }

    return false;
}

int main()
{
    while (true)
    {
        scanf("%d %d", &w, &h);
        if (w == 0 && h == 0)
        {
            break;
        }

        for (int i = 0; i < h; ++i)
        {
            for (int j = 0; j < w; ++j)
            {
                scanf("%d", &c[i][j]);
            }
        }

        count = 0;

        while (sub_search())
        {
        }

        printf("%d\n", count);
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
vector<pair<int, int>> directions = {
    {1,0}, {0,1}, {-1,0}, {0,-1}, {1,1}, {1,-1}, {-1,1}, {-1,-1}
};
void dfs(Graph &chart, int x, int y) {
    int H = chart.size(), W = chart[0].size();
    chart[x][y] = -1;
    for (int i = 0; i < directions.size(); i++) {
        int nx = x + directions[i].first;
        int ny = y + directions[i].second;
        if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
        if (chart[nx][ny] == 1) dfs(chart, nx, ny);
    }
}

int main() {
    while (true) {
        int H, W;
        cin >> W >> H;
        if (H == 0 && W == 0) break;
        Graph chart(H, vector<int>(W));
        for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) {
            cin >> chart[i][j];
        }
        int cnt = 0;
        for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) {
            if (chart[i][j] == 1) {
                dfs(chart, i, j);
                cnt++;
            }
        }
        cout << cnt << endl;
    }
    return 0;
}

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

int w,h;
int c[50][50];
vector<vector<int> > visited;
int dx[]={1,1,0,-1,-1,-1,0,1};
int dy[]={0,-1,-1,-1,0,1,1,1};

bool dfs(int x,int y){
    if(visited[x][y]) return false;
    visited[x][y]=1;

    for(int d=0;d<8;d++){
        int i=x+dx[d];
        int j=y+dy[d];
        if(0<=i&&i<h&&0<=j&&j<w){
            if(c[i][j]){
                dfs(i,j);
            }  
        }
    }
    return true;
}

int solve(int w,int h){

    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]){
                cnt+=dfs(i,j);
            }
        }
    }
    cout<<cnt<<endl;
}

int main(){
    visited.assign(50,vector<int>(50,0));
    while(1){
        cin>>w>>h;
        if(!(w))break;

        solve(w,h);
    }
    
    return 0;
}