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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
using P = pair<int,int>;

/* 大文字を小文字に変換 */
char tolower(char c) {return (c + 0x20);}
/* 小文字を大文字に変換 */
char toupr(char c) {return (c - 0x20);}

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"

// イテレータ要素のインデックス
distance(A.begin(), itr);
*/

const int MAX_H = 55, MAX_W = 55;
const int INF = 1000100100;

int color[MAX_H][MAX_W], c[MAX_H][MAX_W];
int w, h;


void init(){
    rep(i,MAX_H){
        rep(j,MAX_W){
            color[i][j] = INF;
            c[i][j] = 0;
        }
    }
}

void dfs(int hi, int wi, int col){
    if(color[hi][wi] == INF) color[hi][wi] = col;

    for(int i=-1; i<=1; i++){   // 高さ
        for(int j=-1; j<=1; j++){   // 幅
            int nh = hi+i, nw = wi+j;
            if(nh<0 || nw<0 || nh>=h || nw>=w) continue;
            if(c[nh][nw] == 1 && color[nh][nw] == INF) dfs(nh, nw, col);
        }
    }
}

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

        init();

        rep(i,h){
            rep(j,w){
                cin >> c[i][j];
            }
        }

        int col = 0;
        rep(i,h){
            rep(j,w){
                if(color[i][j] == INF && c[i][j] == 1){
                    dfs(i, j, col);
                    ++col;
                }
            }
        }
        set<int> s;
        rep(i,h){
            rep(j,w){
                if(color[i][j] != INF) s.insert(color[i][j]);
            }
        }
        cout << s.size() << endl;
    }
}

C++: #include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end())
#define VRSORT(v) sort(v.rbegin(), v.rend())
#define ll long long
#define pb(a) push_back(a)
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

typedef vector<unsigned int>vec;
typedef vector<vec> mat;

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; }
template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};

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

struct Edge{int to,weight; Edge(int t, int w) : to(t), weight(w) {}};
using DirectedGraph = vector<vector<Edge>>;
using UndirectedGraph = vector<vector<int>>;

int H,W;
char field[51][51];
bool seen[51][51];

void dfs(int h,int w){
    if(seen[h][w]) return;

    seen[h][w] = true;

    REP(i,8){
        int nh=h+dy[i];
        int nw=w+dx[i];

        if(nh<0 || nh>=H || nw<0 || nw>=W || field[nh][nw]=='0') continue;
        dfs(nh,nw);
    }
    return;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(1){
        cin>>W>>H;
        if(!H && !W) return 0;
        REP(i,H) REP(j,W) cin>>field[i][j];
        
        memset(seen,0,sizeof(seen));

        int ans=0;
        REP(i,H) REP(j,W){
            if(field[i][j]=='1' && !seen[i][j]){
                //cout<<i<<":"<<j<<endl;
                dfs(i,j);
                ans++;
            }
        }

        cout<<ans<<endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
static const int N = 50;

int W, H;
bool visited[N][N];
int map[N][N];

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

void dfs(int h, int w) {
    visited[h][w] = true;

    for (int i = 0; i < 8; ++i) {
        int nh = h + dx[i];
        int nw = w + dy[i];

        if (nh < 0 || nh > H-1 || nw < 0 || nw >W-1) continue;
        if (visited[nh][nw]) continue;
        if (map[nh][nw] == 0) continue;

        dfs(nh, nw);
    }
}

int main() {
    while (true) {
        cin >> W >> H;
        if (W == 0 && H == 0) break;

        for (int h = 0; h < H; ++h) {
            for (int w = 0; w < W; ++w) {
                cin >> map[h][w];
            }
        }

        memset(visited, 0, sizeof(visited));

        int count = 0;
        for (int h = 0; h < H; ++h) {
            for (int w = 0; w < W; ++w) {
                if (visited[h][w] || map[h][w] == 0) continue;
                dfs(h, w);
                ++count;
            }
        }

        cout << count << endl;
    }


    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define ALL(x) (x).begin(), (x).end()
#define REP(i ,n) for(int i = 0; i < (int)(n); i++)
#define pb push_back
typedef vector<int>vint;
typedef vector<ll>vll;
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

int h,w;
vector<vector<int>> c(51,vector<int>(51));
int di[] = {-1,0,1,0,-1,-1,1,1};
int dj[] = {0,-1,0,1,-1,1,-1,1};

bool dfs(int i,int j){
    if(0 == c[i][j])return false;
    c[i][j] = 0;
    for(int s=0; s<8; s++){
        int ni = i+di[s];
        int nj = j+dj[s];
        if(ni < 0 || nj < 0 || ni >= h || nj >= w)continue;
        if(0 == c[ni][nj])continue;
        dfs(ni,nj);
    }
    return true;
}

int main()
{
    bool flag = true;
    vint ans;
    while(flag){
        cin >> w >> h;
        if(0 == w && 0 == h){
            flag = false;
            break;
        }
        REP(i,h){
            REP(j,w){
                cin >> c[i][j];
            }
        }
        int cnt=0;
        REP(i,h){
            REP(j,w){
                if(dfs(i,j)){
                    cnt++;
                }
            }
        }
        ans.pb(cnt);
    }
    for(auto a:ans){
        cout << a << endl;
    }
}