Go: package main

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

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		return []byte(f1())
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

// -----------------------------------------------------------------------------

func main() {
	defer stdout.Flush()

	for {
		ans, ok := solve()
		if !ok {
			break
		}
		println(ans)
	}

}

func solve() (int, bool) {
	w := readInt()
	h := readInt()
	if w == 0 && h == 0 {
		return 0, false
	}

	c := make([][]int, h+2)
	c[0] = make([]int, w+2)
	c[h+1] = make([]int, w+2)
	for i := 1; i-1 < h; i++ {
		c[i] = make([]int, w+2)
		for j := 1; j-1 < w; j++ {
			c[i][j] = readInt()
		}
	}

	var ans int
	for i := 1; i-1 < h; i++ {
		for j := 1; j-1 < w; j++ {
			if 0 < c[i][j] {
				ans++
				dfs(c, i, j)
			}
		}
	}
	return ans, true
}

func dfs(c [][]int, y, x int) {
	c[y][x] = 0
	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{-1, -1, -1, 0, 0, 1, 1, 1}
	for i := range dx {
		nx := x + dx[i]
		ny := y + dy[i]
		if 0 < c[ny][nx] {
			dfs(c, ny, nx)
		}
	}
}

// -----------------------------------------------------------------------------

C++: #include <iostream>
#include <iomanip>
#include <vector>

using namespace std;
typedef std::vector<std::vector<int> > Field;
typedef std::vector<std::vector<bool> > Seen;

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

// DFS
bool dfs(const Field& field, Seen& seen, const int ih, const int iw, const int H, const int W) {
    seen[ih][iw] = true;
    if (field[ih][iw] == 0) return false;
    // search oct
    for (int i = 0; i < 8; ++i) {
        const int curh = ih + dx[i];
        const int curw = iw + dy[i];

        // check the boundaries
        if (curh >= H || curh < 0) continue;
        if (curw >= W || curw < 0) continue;

        if (seen[curh][curw]) continue;

        dfs(field, seen, curh, curw, H, W);
    }
    return true;
}

int search(const Field& field, const int H, const int W) {
    Seen seen(H, std::vector<bool>(W, false));
    int cnt = 0;
    for (int ih = 0; ih < H; ++ih) {
        for (int iw = 0; iw < W; ++iw) {
            if (seen[ih][iw]) continue;
            if (field[ih][iw] == 0) continue;
            if (dfs(field, seen, ih, iw, H, W)) ++cnt;
        }
    }
    return cnt;
}

int main() {
    vector<int> res;
    for (;;) {
        int W, H; cin >> W >> H;
        if ( (W == 0 || H == 0) ) break;
        Field field(H, std::vector<int>(W, 0));
        for (int ih = 0; ih < H; ++ih) {
            for (int iw = 0; iw < W; ++iw) {
                cin >> field[ih][iw];
            }
        }
        res.push_back(search(field, H, W));
    }
    for (unsigned int i = 0; i < res.size(); ++i) {
        cout << res[i] << "\n";
    }
    return 0;
}

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

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

void dfs(int y, int x)
{
	fld[y][x] = 0;
	for(int i=0; i<8; i++)
	{
		int ny = y+dy[i];
		int nx = x+dx[i];

		if(fld[ny][nx])
		{
			dfs(ny,nx);
		}
	}
}


int main()
{
	int w,h,c;
	while(cin >> w >> h, w||h)
	{
		memset(fld,0,sizeof(fld));

		for(int y=1; y<=h; y++)
		for(int x=1; x<=w; x++)
		{
			cin >> fld[y][x];
		}

		c=0;
		for(int y=1; y<=h; y++)
		for(int x=1; x<=w; x++)
		{
			if(fld[y][x])
			{
				c++;
				dfs(y,x);
			}
		}

		cout << c << endl;
	}
}

C++: 
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <cctype>
#include <complex>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <cassert>

using namespace std;

void dfs(int x, int y, int h, int w, vector<vector<int> > &f){
	
	f[x][y] = 0;
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			int x2 = x+i;
			int y2 = y+j;
			if((0<=x2 && x2<=h-1) && (0<=y2 && y2<=w-1)){
				if(f[x2][y2]) dfs(x2, y2, h, w, f);
			}
		}
	}
	
	return;
}

bool solve(){
	int w, h;
	cin>> w>> h;
	if(w==0 && h==0) return false;
	
	vector<vector<int> > field(h, vector<int>(w,0));
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			int c;
			cin>> c;
			field[i][j] += c;
		}
	}
	
	int ans = 0;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			if(field[i][j]){
				dfs(i, j, h, w, field);
				ans++;
			}
		}
	}
	
	cout<< ans<< endl;
	return true;
}

int main(){
	cout.setf(ios::fixed);
	cout.precision(10);
	while(solve());

	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<iomanip>
#include<algorithm>
#include<queue>
#include<stack>
#include<list>
#include<map>
#include<deque>
#include<math.h>
using namespace std;
#define ll long long
ll mp[100][100];
ll visited[100][100];
void dfs(ll w,ll h,ll i,ll j){
    ll a,b;
    visited[i][j]=1;
    for(a=i-1;a<=i+1;a++){
        for(b=j-1;b<=j+1;b++){
            if(a>=0&&a<h&&b>=0&&b<w&&mp[a][b]==1&&visited[a][b]==0)dfs(w,h,a,b);
        }
    }
    return ;
}

int main(){
    while(true){
        ll w,h;
        cin >> w >> h;
        if(w==0&&h==0) return 0;
        ll i,j;
        for(i=0;i<h;i++){
            for(j=0;j<w;j++){
                cin >> mp[i][j];
                visited[i][j]=0;
            }
        }
        ll ans =0;
        for(i=0;i<h;i++){
            for(j=0;j<w;j++){
                if(mp[i][j]==1&&visited[i][j]==0){
                    ans++;
                    dfs(w,h,i,j);
                }
            }
        }
        cout << ans <<endl;
    }
}