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>
#include<utility>
using namespace std;

typedef pair<int, int> P;

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

int main() {
	int w, h;
	while (cin >> w >> h, w) {
		vector<vector<int>>ma(w, vector<int>(h));
		for (int i = 0; i < h;i++) {
			for (int j = 0; j < w;j++) {
				cin >> ma[j][i];
			}
		}
		int ans = 0;
		for (int i = 0; i < w;i++) {
			for (int j = 0; j < h;j++) {
				if (ma[i][j] == 1) {
					ans++;
					queue<P> q;
					q.push(P(i, j));
					ma[i][j] = 0;
					while (!q.empty()) {
						P p = q.front(); q.pop();
						for (int k = 0; k < 8; k++) {
							int nx = p.first + dx[k], ny = p.second + dy[k];
							if (nx < w && nx>=0 && ny < h && ny>=0 && ma[nx][ny]==1) {
								ma[nx][ny] = 0; q.push(P(nx, ny));
							}
						}
					}
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>

using namespace std;

typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;

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

void dfs(VVI &table, int i, int j) {
    table[i][j] = 0;
    for (int loop = 0; loop < 8; loop++) {
        int new_i = i + dy[loop];
        int new_j = j + dx[loop];
        if (table[new_i][new_j] == 1) {
            dfs(table, new_i, new_j);
        }
    }
    return;
}

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

C++: #include <iostream>
#include <stack>
using namespace std;
int dx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
int dy[] = { -1, -1, -1, 0, 1, 1, 1, 0 };

int main(){
	int w, h;
	while( cin >> w >> h && w && h ){
		int field[52][52];
		stack< pair<int, int> > st;
		for( int i = 1; i <= h; i++ ){
			for( int j = 1; j <= w; j++ ){
				cin >> field[i][j];
			}
		}
		int sum = 0;
		for( int i = 1; i <= h; i++ ){
			for( int j = 1; j <= w; j++ ){
				if( field[i][j] == 1 ){
					sum++;
					field[i][j] = 0;
					st.push( pair<int, int>(i, j) );
					while( !st.empty() ){
						int x0 = st.top().second;
						int y0 = st.top().first;
						st.pop();
						for( int k = 0; k < 8; k++ ){
							int x = x0 + dx[k];
							int y = y0 + dy[k];
							if( field[y][x] == 1 ){
								st.push( pair<int, int>(y, x) );
								field[y][x] = 0;
							}
						}
					}
				}
			}
		}
		cout << sum << endl;
	}
}

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

#define pb         push_back

const int INF = 1000000000;
const int MOD = 1000000007;

int ans;
int ma[100][100];
int w, h;

void rec(int y, int x);

int main(void) {
	while(1){
		ans = 0;
		cin >> w >> h;
		if(w == 0 && h == 0) break;

		for(int i=0; i<h; i++){
			for(int j=0; j<w; j++){
				int t;
				cin >> t;
				ma[i][j] = t;
			}
		}

		for(int i=0; i<h; i++){
			for(int j=0; j<w; j++){
				if(ma[i][j] == 1){
					ans++;
					rec(i, j);
				}
			}
		}
		printf("%d\n", ans);

	}
	
	return 0;
}

void rec(int y, int x){
	for(int i=-1; i<=1; i++){
		for(int j=-1; j<=1; j++){
			if(i == 0 && j == 0) continue;

			int ny = y+i;
			int nx = x+j;
			if(ny >= 0 && ny < h && nx >= 0 && nx < w && ma[ny][nx] == 1){
				ma[ny][nx] = 0;
				rec(ny, nx);
			}
		}
	}
}

C++: #ifdef BUILT_LOCAL

#include "niklib.h"

#endif

#include <iostream>
#include <utility>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <string>
#include <cstdio>
#include <map>
#include <set>

using namespace std;

int w, h, c[50][50];

void rec(int y, int x) {
    if (y < 0 || x < 0 || y >= h || x >= w) return;
    if (c[y][x] == 0)
        return;
    c[y][x] = 0;
    for (int i = -1; i <= 1; i++) {
        for (int j = -1; j <= 1; j++) {
            rec(y + i, x + j);
        }
    }
}

int main() {
    while (1) {
        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]);
            }
        int r = 0;
        for (int i = 0; i < h; i++)
            for (int j = 0; j < w; j++) {
                if (c[i][j] == 0) continue;
                r++;
                rec(i, j);
            }
        printf("%d\n", r);
    }
}