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<algorithm>
#include<string>
#include<stack>
#include<queue>
using namespace std;

int w, h;
int map[50][50];
int ans;

void Suibotu(int y, int x)
{//??¨??????????????????????±?????????¶????????°???????°´????????????
	map[y][x] = 0;
	if(x<w-1 && map[y][x+1]==1)				//???
		Suibotu(y, x+1);
	if(x<w-1 && y<h-1 && map[y+1][x+1]==1)	//??????
		Suibotu(y+1, x+1);
	if(y<h-1 && map[y+1][x]==1)				//???
		Suibotu(y+1, x);
	if(x>0 && y<h-1 && map[y+1][x-1]==1)	//??????
		Suibotu(y+1, x-1);
	if(x>0 && map[y][x-1]==1)				//???
		Suibotu(y, x-1);
	if(x>0 && y>0 && map[y-1][x-1]==1)		//??????
		Suibotu(y-1, x-1);
	if(y>0 && map[y-1][x]==1)				//???
		Suibotu(y-1, x);
	if(x<w-1 && y>0 && map[y-1][x+1]==1)	//??????
		Suibotu(y-1, x+1);
}
int main(){
	cin >> w >> h;
	while(w+h>0){
		ans = 0;
		for(int i=0; i<h; i++)
			for(int j=0; j<w; j++)
				cin >> map[i][j];
			for(int i=0; i<h; i++)
			for(int j=0; j<w; j++)
				if(map[i][j]==1){
					ans++;
					Suibotu(i, j);
				}
		cout << ans << endl;
		cin >> w >> h;
	}
	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[50][50];
		stack< pair<int, int> > st;
		for( int i = 0; i < h; i++ ){
			for( int j = 0; j < w; j++ ){
				cin >> field[i][j];
			}
		}
		int sum = 0;
		for( int i = 0; i < h; i++ ){
			for( int j = 0; j < w; j++ ){
				if( field[i][j] == 1 ){
					sum++;
					field[i][j] = 0;
					st.push( pair<int, int>(i, j) );
					//cout << i << ' ' << j << endl;
					while( !st.empty() ){
						//cout << st.top().second << ' ' << st.top().first << endl;
						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( 0 <= x && x < w && 0 <= y && y < h && field[y][x] == 1 ){
								st.push( pair<int, int>(y, x) );
								field[y][x] = 0;
								//cout << y << ' ' << x << endl;
							}
						}
					}
				}
			}
		}
		cout << sum << endl;
	}
}

C++: #include <iostream>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <tuple>
#include <set>
#include <stack>
#include <queue>
#define REP(i, n) for(LL i = 0;i < n;i++)
#define REPR(i, n) for(LL i = n;i >= 0;i--)
#define FOR(i, m, n) for(LL i = m;i < n;i++)
#define FORR(i, m, n) for(LL i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()

#define INF 999999999
#define MOD 1000000007
using namespace std;
typedef long long LL;
//typedef LL int;
typedef pair<int, int> P;
typedef pair<LL, LL> LP;
typedef pair<int, P> PP;
typedef pair<LL, LP> LPP;
int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};

int main(){
    int w, h;
    while(cin >> w >> h, w + h){
        int c[51][51];
        REP(i, h){
            REP(j, w){
            cin >> c[i][j];
            }
        }

        int ans = 0;

        REP(i, h){
            REP(j, w){
                if(c[i][j] == 2) continue;
                else if(c[i][j] == 0) continue;
                else if(c[i][j] == 1){
                    queue<int> que;
                    que.push(i); que.push(j);
                    
                    while(!que.empty()){
                        int x, y;
                        y = que.front(); que.pop();
                        x = que.front(); que.pop();
                        
                        c[y][x] = 0;

                        if(y-1 >= 0 && y-1 < h && x-1 >= 0 && x-1 < w && c[y-1][x-1] == 1){
                            que.push(y-1); que.push(x-1);
                        }else if(y-1 >= 0 && y-1 < h && x >= 0 && x < w && c[y-1][x] == 1){
                            que.push(y-1); que.push(x);
                        }else if(y-1 >= 0 && y-1 < h && x+1 >= 0 && x+1 < w && c[y-1][x+1] == 1){
                            que.push(y-1); que.push(x+1);
                        }else if(y >= 0 && y < h && x-1 >= 0 && x-1 < w && c[y][x-1] == 1){
                            que.push(y); que.push(x-1);
                        }else if(y >= 0 && y < h && x+1 >= 0 && x+1 < w && c[y][x+1] == 1){
                            que.push(y); que.push(x+1);
                        }else if(y+1 >= 0 && y+1 < h && x-1 >= 0 && x-1 < w && c[y+1][x-1] == 1){
                            que.push(y+1); que.push(x-1);
                        }else if(y+1 >= 0 && y+1 < h && x >= 0 && x < w && c[y+1][x] == 1){
                            que.push(y+1); que.push(x);
                        }else if(y+1 >= 0 && y+1 < h && x+1 >= 0 && x+1 < w && c[y+1][x+1] == 1){
                            que.push(y+1); que.push(x+1);
                        }
                    }
                    ans++;
                }
            }
        }

        cout << ans << endl;
    }
}