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<cstdio>
#include<vector>
#include<algorithm>
#include<functional>
#include<string>
#include<cstring>
#include<cmath>
using namespace std;
int cnt;
int w,h;
char map[52][52];
void func(int y,int x)
{
    map[y][x]='0';
    int nx,ny;
    for(int dy=-1;dy<=1;dy++)
    {
        for(int dx=-1;dx<=1;dx++)
        {
            ny=y+dy;
            nx=x+dx;
            if(map[ny][nx]=='1'&&0<=ny&&0<=nx&&ny<h&&nx<w)
            {
                func(ny,nx);
            }
        }
    }
    return;
}
int main()
{
    while(cin>>w>>h,w)
    {
        cnt=0;
        for(int i=0;i<52;i++)
        {
            for(int j=0;j<52;j++)
            {
                map[i][j]='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')
                {
                    func(i,j);
                    cnt++;
                }
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

C++: #include<iostream>
#include<map>
#include<vector>
#include<list>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<numeric>
#include<climits>
#include<ctime>
#include<cstring>
#define REP(i,p,n) for(int i=p;i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define rep_split(tok,a_str,re) for(char *tok = strtok((char *)a_str.c_str(),re); tok != NULL; tok = strtok(NULL,re))
#define ALL(c) (c).begin(), (c).end()
#define dump(a) cerr << #a << "=" << (a) << endl

using namespace std;

int W,H;
int sima_cnt;
int sima_size;
int sima[51][51];
bool is_visit[51][51];

int dw[8] = {1,1,0,-1,-1,-1,0,1};
int dh[8] = {0,1,1,1,0,-1,-1,-1};

void saiki(int w, int h) {
	int aw=0,ah=0;
	rep(i,8) {
		aw = w + dw[i];
		ah = h + dh[i];
		if ((0<=aw && aw<W) && (0<=ah && ah<H)) {
			//次の島を訊ねる条件
			if (sima[aw][ah] && !is_visit[aw][ah]) { 
				is_visit[aw][ah] = true;
				sima_size++;
				saiki(aw,ah);
			}
			else { 
				is_visit[aw][ah] = true;
			}
		}
	}
}

int main() {
	while(cin >> W >> H) {
		if (W==0 && H==0) { break; }
		sima_cnt = 0;
		sima_size = 0;
		rep(h,51) {
			rep(w,51) {
				sima[w][h]=0;
				is_visit[w][h]=0;
			}
		}

		rep(h,H) {
			rep(w,W) {
				cin >> sima[w][h];
			}
		}

		rep(h,H) {
			rep(w,W) {
				sima_size = 0;
				if (sima[w][h] && !is_visit[w][h]) { sima_size++; }
				saiki(w,h);
				if (sima_size>0) {
					sima_cnt++;
				}
			}
		}
		cout << sima_cnt << endl;
	}

	return 0;
}

C++: #include<iostream>
using namespace std;
int w, h, c[50][50], ans = 0;
void solve(int p, int q) {
	c[p][q] = 0;
	for (int i = -1; i < 2; i++) {
		for (int j = -1; j < 2; j++) {
			if (p + i >= 0 && p + i < h && q + j >= 0 && q + j < w) {
				if (c[p + i][q + j]) {
					solve(p + i, q + j);
				}
			}
		}
	}
}
int main() {
	while (cin >> w >> h && w + h) {
		ans = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> c[i][j];
			}
		}
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (c[i][j]) {
					//cout << i << " " << j << endl;
					ans++;
					solve(i, j);
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using mii = map<int, int>;
const int INF = 1e9;
const ll LINF = 1e18;
#define MP(a,b) make_pair((a),(b))
#define MT(...) make_tuple(__VA_ARGS__)

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define REPS(i,x) for(int i=1;i<=(int)(x);i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RREPS(i,x) for(int i=((int)(x));i>0;i--)

#define ALL(x) (x).begin(),(x).end()
#define IN(type,a) type a;cin >> a
#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define COUT(x) cout << (x) << endl
#define DCOUT(x,n) cout << fixed << setprecision(n) << (x) << endl
int w,h;
void dfs(int y, int x, vector<vector<int>>& a, vector<vector<bool>>& check){
    if ((x<0)||(y<0)||(y>=h)||(x>=w)) return;
    if (a[y][x]==0) return;
    if (check[y][x]) return;
    check[y][x] = true;
    for (int i = -1; i<2; i++) {
        for (int j = -1; j<2; j++) {
                dfs(y+i, x+j, a, check);
        }
    }
    
}
int main() {
    while (true) {
        cin >> w >> h;
        if (w==0) {
            break;
        }
        vector<vector<int>> a(h,vector<int>(w));
        vector<vector<bool>> check(h,vector<bool>(w,false));
        int count = 0;
        REP(i, h){
            REP(j, w){
                cin >> a[i][j];
            }
        }
        
        REP(i, h){
            REP(j, w){
                if ((a[i][j]==1)&&(!check[i][j])) {
                    count++;
                    dfs(i, j, a, check);
                }
            }
        }
        COUT(count);
    }

}