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 <bits/stdc++.h>
using namespace std;
int main(void){
    int w,h;
    while (cin>>w>>h) {
        if (w==0&&h==0) {
            break;
        }
        char c;
        std::vector<vector<char>> vec(h,vector<char>(w));
        bool seen[h][w];
        for (int i=0;i<h;i++) {
            for (int j=0;j<w;j++) {
                cin>>c;
                vec[i][j]=c;
                seen[i][j]=false;
            }
        }
        int count=0;
        for (int i=0;i<h;i++) {
            for (int j=0;j<w;j++) {
                if (vec[i][j]=='1'&&seen[i][j]==false) {
                    seen[i][j]=true;
                    std::stack<pair<int,int>> stk;
                    stk.push(make_pair(i,j));
                    while (stk.size()!=0) {
                        int x=stk.top().first;
                        int y=stk.top().second;
                        if (x+1<h&&seen[x+1][y]==false&&vec[x+1][y]=='1') { //下
                            stk.push(make_pair(x+1,y));
                            seen[x+1][y]=true;
                        } else if (y+1<w&&seen[x][y+1]==false&&vec[x][y+1]=='1') { //右
                            stk.push(make_pair(x,y+1));
                            seen[x][y+1]=true;
                        } else if (x+1<h&&y-1>=0&&seen[x+1][y-1]==false&&vec[x+1][y-1]=='1') { //左下
                            stk.push(make_pair(x+1,y-1));
                            seen[x+1][y-1]=true;
                        } else if (x+1<h&&y+1<w&&seen[x+1][y+1]==false&&vec[x+1][y+1]=='1') { //右下
                            stk.push(make_pair(x+1,y+1));
                            seen[x+1][y+1]=true;
                        } else if (x-1>=0&&y-1>=0&&seen[x-1][y-1]==false&&vec[x-1][y-1]=='1') { //左上
                            stk.push(make_pair(x-1,y-1));
                            seen[x-1][y-1]=true;
                        } else if (x-1>=0&&y+1<w&&seen[x-1][y+1]==false&&vec[x-1][y+1]=='1') { //右上
                            stk.push(make_pair(x-1,y+1));
                            seen[x-1][y+1]=true;
                        } else if (x-1>=0&&seen[x-1][y]==false&&vec[x-1][y]=='1') { //上
                            stk.push(make_pair(x-1,y));
                            seen[x-1][y]=true;
                        } else if (y-1>=0&&seen[x][y-1]==false&&vec[x][y-1]=='1') { //左
                            stk.push(make_pair(x,y-1));
                            seen[x][y-1]=true;
                        } else {
                            stk.pop();
                        }
                    }
                    count++;
                }
            }
        }
        cout<<count<<endl;
    }
}

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

int dx[8] = {1,0,-1,0,1,1,-1,-1};
int dy[8] = {0,1,0,-1,1,-1,1,-1};
bool seen[60][60];
int H,W;

void dfs(const vector<vector<int> > &Grid, int y, int x){
    seen[y][x] = true;
    for(int i = 0; i < 8; i++){
        int ny = y + dy[i];
        int nx = x + dx[i];
        if(ny < 0 || ny >= H || nx < 0 || nx >= W) continue;
        if(seen[ny][nx]) continue;
        if(Grid[ny][nx] == 0) continue;
        dfs(Grid,ny,nx);
    }
}

int main(){
    while(true){
        cin >> W >> H;
        if(!H && !W) break;
        vector<vector<int> > Grid(H,vector<int>(W));
        for(int i = 0; i < H; i++){
            for(int j = 0; j < W; j++){
                cin >> Grid[i][j];
            }
        }
        memset(seen,false,sizeof(seen));
        int cnt = 0;
        for(int i = 0; i < H; i++){
            for(int j = 0; j < W; j++){
                if(Grid[i][j] == 0) continue;
                if(seen[i][j]) continue;
                dfs(Grid,i,j);
                cnt++;
            }
        }
        cout << cnt << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int H,W,k[100][100],n[100][100],color=1,i,j,l,m,o,r;
int main(){while(true){memset(n,0,sizeof(n));r=0;cin>>H>>W;if(H==0){break;}for(i=0;i<H;i++){for(j=0;j<W;j++){cin>>k[i][j];}}
for(i=0;i<H;i++){for(j=0;j<W;j++){if(r==1){color++;}r=0;if(k[i][j]==1 && n[i][j]==0){n[i][j]=color;for(l=0;l<100;l++){
	for(m=0;m<H;m++){for(o=0;o<W;o++){if(n[m][o]==color){
		for(int p=-1;p<=1;p++){for(int q=-1;q<=1;q++){if(m-p>=0 && m-p<H){if(o-q>=0 && o-q<W){n[m][o]=color;}}}}
	}}}
}}}}
cout<<color<<endl;}
}