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 <bits/stdc++.h>

using namespace std;

struct posi {
	int x;
	int y;
};

int main() {

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

		if (w == 0 && h == 0) { return 0; }
		vector<vector<int>> mapp(h + 2, vector<int>(w + 2));
		for (int i = 1; i <= h; i++) {
			for (int j = 1; j <= w; j++) {
				cin >> mapp[i][j];
			}
		}


		vector<int> dx{ 1,1,0,-1,-1,-1,0,1 };
		vector<int> dy{ 0,1,1,1,0,-1,-1,-1 };

		vector<vector<bool>> seen(h + 2, vector<bool>(w + 2, false));
		deque<posi> todo; //考え方はスタックだけどdequeの方が使い勝手がいい


		posi s; //今いる場所
		int counter = 0;
		for (int i = 1; i <= h; i++) {
			for (int j = 1; j <= w; j++) {
				if (mapp[i][j] == 0) { continue; }
				//seen.resize(h + 2, vector<bool>(w + 2, false));

				if (seen[i][j] == false) {
					counter++;
					seen[i][j] = true;
					todo.push_back({ i,j });

					while (todo.empty() != true) {
						s = todo.back();
						bool tugiflg = false; //次があったらtrue
						for (int k = 0; k < 8; k++) {
							if (seen[s.x + dx[k]][s.y + dy[k]] != true) {
								seen[s.x + dx[k]][s.y + dy[k]] = true;
								if (mapp[s.x + dx[k]][s.y + dy[k]] == 1) {
									todo.push_back({ s.x + dx[k],s.y + dy[k] });;
									tugiflg = true;
									break;
								}
							}
						}
						if (tugiflg == false) {
							todo.pop_back();
						}
					}
				}
			}
		}

		cout << counter << endl;
	}
	return 0;

}

C++: #include <algorithm>
#include <bitset>
#include <climits>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <tuple>
#include <utility>
#include <valarray>
#include <vector>

#define rep(i, a, n) for(int i = (a);i < (n);i++)
#define REP(i, n)    rep(i, 0, n)
#define all(e)       (e).begin(), (e).end()

#ifdef LOCAL
#define show(x) cerr<<__LINE__<<" : "<<#x<<" = "<<(x)<<endl;
#else
#define show() true
#endif

using namespace std;
using ll=long long;
template<class T> using V=vector<T>;

template<class T, class U>
void chmin(T &t, const U u) { if (t > u) t = u; }

template<class T, class U>
void chmax(T &t, const U u) { if (t < u) t = u; }

const int INF = (1 << 30) - 1;
const ll INFL = (ll) 5e15;
const int MOD = 1000000007;

using P=pair<int, int>;

class Solver {
public:
    bool solve() {
        cin >> W >> H;
        if (W == 0 && H == 0) return false;

        field.resize(W, V<int>(H));
        REP(y, H) {
            REP(x, W) {
                cin >> field[x][y];
            }
        }

        int c = 2;
        REP(y, H) {
            REP(x, W) {
                if (field[x][y] == 1) {
                    bfs(P(x, y), c);
                    ans++;
                }
            }
        }

        cout << ans << endl;
        return true;
    }

private:
    int H, W;
    V<V<int>> field;
    int ans = 0;

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

    void bfs(P s, int c) {
        queue<P> que;
        que.push(s);
        field[s.first][s.second] = c;
        while (que.size()) {
            P cur = que.front();
            que.pop();

            for (int i = 0; i < 8; ++i) {
                P next = {cur.first + dx[i], cur.second + dy[i]};
                if (check(next) && field[next.first][next.second] == 1) {
                    que.push(next);
                    field[next.first][next.second] = c;
                }
            }
        }
    }

    bool check(P p) {
        if (p.first < 0 || p.first >= W) return false;
        if (p.second < 0 || p.second >= H) return false;
        return true;
    }
};

int main() {

    while (Solver().solve());

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
bool d[51][51];
int a,b,s,_x,_y,dx[8]={0,-1,-1,-1,0,1,1,1},dy[8]={1,1,0,-1,-1,-1,0,1};
#define r(z,x) for(int z=0;z<x;z++)
void dfs(int x,int y){
	if(0>x||x>=b||0>y||y>=a)return;
	d[x][y]=0;
	r(i,8)if(d[_x=x+dx[i]][_y=y+dy[i]])dfs(_x,_y);
}
int main(){
	while(cin>>a>>b,a||b){
		s=0;
		r(i,b)r(j,a)cin>>d[i][j];
		r(i,b)r(j,a)if(d[i][j])dfs(i,j),s++;
		cout<<s<<endl;
	}
}

C++: #include <iostream>
#include <string.h>

using namespace std;
char field[52][52];
int dx[8] = {-1, -1, -1,  0, 0,  1, 1, 1};
int dy[8] = {-1,  0,  1, -1, 1, -1, 0, 1};

void DFS(int y, int x)
{
	field[y][x] = '0';
	for(int i = 0; i < 8; ++i)
	{
		int nx = x + dx[i];
		int ny = y + dy[i];
		if(field[ny][nx] == '1')
		{
			DFS(ny, nx);
		}
	}
}

void solve()
{
	int w, h;
	while(cin >> w >> h, w || h)
	{
		memset(field, 0, sizeof(field));
		for(int i = 1; i <= h; ++i)
		{
			for(int j = 1; j <= w; ++j)
			{
				cin >> field[i][j];
			}
		}
		int count = 0;
		for(int i = 1; i <= h; ++i)
		{
			for(int j = 1; j <= w; ++j)
			{
				if(field[i][j] == '1')
				{
					++count;
					DFS(i, j);
				}
			}
		}
		cout << count << endl;
	}
}

int main()
{
	solve();
	return(0);
}

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

int myMap[51][51];
int w, h;
int ans;

bool safe(int x, int y){
    return (0 <= x && 0 <= y) && (x < h && y < w);
}

void del(int x, int y){
    myMap[x][y] = 0;
    if (safe(x+1, y) && myMap[x+1][y]) del(x+1, y);
    if (safe(x+1, y+1) && myMap[x+1][y+1]) del(x+1, y+1);
    if (safe(x+1, y-1) && myMap[x+1][y-1]) del(x+1, y-1);
    if (safe(x, y+1) && myMap[x][y+1]) del(x, y+1);
    if (safe(x, y-1) && myMap[x][y-1]) del(x, y-1);
    if (safe(x-1, y-1) && myMap[x-1][y-1]) del(x-1, y-1);
    if (safe(x-1, y+1) && myMap[x-1][y+1]) del(x-1, y+1);
    if (safe(x-1, y) && myMap[x-1][y]) del(x-1, y);
    return;
}

int main(){

    while (1){
	cin >> w >> h;
	if (w == 0 && h == 0) break;

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

	ans = 0;	
	for (int i = 0; i < w; i++){
	    for (int j = 0; j < h; j++){
		if (myMap[i][j] == 1){
		    cout << i << " " << j << endl;
		    ans++;
		    del(i, j);
		}
	    }
	}

	cout << ans << endl;
    }
}