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 <algorithm>
using namespace std;

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 x, int y, vector<vector<bool> >& map){
    map[y][x] = false;
    for(int i=0; i<8; i++){
        if(map[y+dy[i]][x+dx[i]]){
            dfs(x+dx[i], y+dy[i], map);
        }
    }
}

int main(){
    while(1){
        int W,H;
        cin >> W >> H;
        if(W==0) break;

        vector<vector<bool> > map(H+2, vector<bool>(W+2,false));
        for(int y=1; y<=H; y++){
            for(int x=1; x<=W; x++){
                char tmp;
                cin >> tmp;
                if(tmp == '0'){
                    map[y][x]=false;
                }else{
                    map[y][x]=true;
                }
            }
        }

        int num=0;
        for(int y=1; y<=H; y++){
            for(int x=1; x<=W; x++){
                if(map[y][x]){
                    num++;
                    dfs(x,y,map);
                }
            }
        }
        cout << num << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
typedef pair<int,int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int w,h;
int nx,ny;
void dfs(vector<vector<int>> &G,int a,int b){
    G.at(a).at(b)=0;
    
    for(int k=-1;k<=1;k++){
        for(int l=-1;l<=1;l++){
            nx=a+k;
            ny=b+l;
        if(0<=nx&& nx<h && 0<=ny&& ny<w && G.at(nx).at(ny)==1) dfs(G,nx,ny);
        }
    }
    return ;
}



int main(){
    int w,h;
    cin>>w>>h;
    vector<vector<int>>G(h,vector<int>(w));
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>G.at(i).at(j);
        }
    }
    int count=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(G.at(i).at(j)==1){
                dfs(G,i,j);
                count++;
            }
        }
    }
cout<<count<<endl;
}

C++: #include<stdio.h>
#include<iostream>
#include <utility>
#include<stack>
#include<cstdio>
using namespace std;

int field[51][51];
int w,h;

void land(int x,int y){
	stack<pair<int,int> > field2;
	int dx[]={1,1,1,-1,-1,-1,0,0},dy[]={1,0,-1,1,0,-1,1,-1};
	int xx,yy,flag,stka=1;

	field2.push(pair<int,int>(x,y));
	field[x][y]=0;

	while(1){
		flag=0;
		for(int i=0;i<9;i++){
			if(field2.top().first+dx[i]>=0&&field2.top().first+dx[i]<=w&&field2.top().second+dy[i]>=0&&field2.top().second+dy[i]<=h){
				if(field[field2.top().first+dx[i]][field2.top().second+dy[i]]==1){
					xx=field2.top().first+dx[i],yy=field2.top().second+dy[i];
					field[field2.top().first+dx[i]][field2.top().second+dy[i]]=0;
					field2.push(pair<int,int>(xx,yy));
					stka+=1;
					flag=1;
					break;
				}
			}
		}
		if(flag==0){
			field2.pop();
			stka--;
			if(stka==0){
				break;
			}
		}
	}
	return;
}

int main(void){

	int kazu=0;

	while(1){

	scanf("%d %d",&w,&h);

	if(w==0&&h==0) break;

	kazu=0;

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			scanf("%d",&field[j][i]);
		}
	}

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){

			if(field[j][i]==1){
				land(j,i);
				kazu++;
			}

		}
	}

	printf("%d\n",kazu);
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int INF = 1000000000;
#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define rep(i,n) REP(i, 0, n)
bool fld[60][60];
int W, H;
void dfs(int x, int y){
    fld[y][x] = false;
    REP(i, -1, 2) REP(j, -1, 2){
        int nx = x + i, ny = y + j;
        if(nx < 0 || ny < 0 || W <= nx || H <= ny) continue;
        if(!fld[ny][nx]) continue;
        dfs(nx, ny);
    }
}
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int t;
    while(cin >> W >> H && W){
        memset(fld, 0, sizeof(fld));
        rep(i, H) rep(j, W) cin >> fld[i][j];
        int cnt = 0;
        rep(i, H) rep(j, W) if(fld[i][j]){
            dfs(j, i);
            cnt++;
        }
        cout << cnt << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define reps(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
	int w, h;
	int c[50][50] = {};

	void dfs(int i, int j) {
		c[i][j] = 0;

		for (int di = -1; di <= 1;di++) {
			for (int dj = -1; dj <= 1;dj++) {
				int ni = i + di;
				int nj = j + dj;

				if (0 <= ni&&ni < h && 0 <= nj&&nj < w && c[ni][nj] == 1) {
					dfs(ni, nj); 
				}
			}
		}
		return;
}

int main() {
	cin.sync_with_stdio(false);
	vector<int>ans;
	while (1) {
		cin >> w >> h;
		rep(i, h) {
			rep(j, w) {
				cin >> c[i][j];
			}
		}
		int res = 0;
		rep(i, h) {
			rep(j, w) {
				if (c[i][j] == 1) {
					dfs(i, j);
					res++;
				}
			}
		}
		if (w == 0&&h == 0) {
			break;
		}
		ans.push_back(res);


	}
	rep(i, ans.size()) {
		cout << ans[i] << endl;
	}

	return 0;
}