Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

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);
}