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;
const int size = 12;
//const int size = 14;
char a[size][size];
int cnt = 0;
void dfs(int x, int y){
    //現在地を０にする
    a[x][y] = 0;
    //4近傍を探索
    //x+1, y
    int nx = x+1, ny=y;
    if(0 <= nx and nx < 12 and 0 <= ny and ny < 12 and a[nx][ny] == '1') dfs(nx, ny);
    //x-1, y
    nx = x-1, ny = y;
    if(0 <= nx and nx < 12 and 0 <= ny and ny < 12 and a[nx][ny] == '1') dfs(nx, ny);
    //x, y+1
    nx = x, ny = y+1;
    if(0 <= nx and nx < 12 and 0 <= ny and ny < 12 and a[nx][ny] == '1') dfs(nx, ny);
    //x, y-1
    nx = x, ny = y - 1;
    if(0 <= nx and nx < 12 and 0 <= ny and ny < 12 and a[nx][ny] == '1') dfs(nx, ny);
    return;
}

int main(){
    while (1) {
    cnt = 0;
    //マップを入力
    for(int i=0; i < 12; i++){
        for(int j=0; j < 12; j++) if(!(cin >> a[i][j])) return 0;
    }


    //サーチ
    for(int i=0; i < 12; i++){
        for(int j=0; j < 12; j++){
            if(a[i][j] == '1'){
                cnt += 1;
                dfs(i, j);
            }
        }
    }

    cout << cnt << endl;
}
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

//  四方向へのベクトル
const int dx[8] = {-1,0,1,0,1,1,-1,-1};
const int dy[8] = {0,-1,0,1,1,-1,1,-1};

int H,W;
//探索用
bool seen[501][5001];
int MAX = 0;
//using Graph = vector<vector<int>>;
bool roop;
int dfs(const vector<vector<int>> &G,int nh,int nw){
    seen[nh][nw] = true;
    
    for(int i=0;i<8;i++){
        int next_h = nh + dy[i];//8方向へ移動
        int next_w = nw + dx[i];

        if(next_h<0 || next_h >=H ||next_w<0||next_w >= W) continue;
        if(seen[next_h][next_w]) continue;
        if(G[next_h][next_w] == 0) continue;

        dfs(G,next_h,next_w);
    }

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

        vector<vector<int>> G(H);
        for(int i=0;i<H;i++){
            for(int k=0;k<W;k++){
                int a;
                cin >> a;
                G[i].push_back(a);
            }
        }
        memset(seen,0,sizeof(seen));

        for(int i=0;i<H;i++){
            for(int k=0;k<W;k++){
                if(G[i][k] == 1&& !seen[i][k]){
                    dfs(G,i,k);
                    cnt++;
                }
            }
        }

        cout <<cnt <<endl;
    }
    return 0;
    
}

C++: #include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;

bool c[51][51];
bool checked[51][51];
int w, h;

void dfs(int y, int x) {
    if (checked[y][x] || !c[y][x]) return;
    checked[y][x] = true;
    repd(i, -1, 2) {
        repd(j, -1, 2) {
            if (i == 0 && j == 0) continue;
            int yy = y + i, xx = x + j;
            if (yy < 0 || yy >= h || xx < 0 || xx >= w) continue;
            dfs(yy, xx);
        }
    }
}

int main()
{
    while (1) {
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        rep(i, h) {
            rep(j, w) {
                int k;
                cin >> k;
                c[i][j] = k;
                checked[i][j] = false;
            }
        }

        int cnt = 0;
        rep(i, h) {
            rep(j, w) {
                if (!checked[i][j] && c[i][j]) {
                    dfs(i, j);
                    cnt++;
                }
            }
        }

        cout << cnt << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<math.h>
#include<string>
#include<limits.h>
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define nrep(i,a,b) for(int i=a;i<b;i++)
#define mrep(i,a) for(int i=a;i<=0;i++)
#define INF LLONG_MAX
using namespace std;

ll w, h, ans;
vector<pair<ll, ll> > idou(8);

void dfs(vector<vector<ll> > &map, ll tate, ll yoko) {
	map[tate][yoko] = ans;
	rep(i, 8) {
		ll t = tate + idou[i].first;
		ll y = yoko + idou[i].second;
		if (0 <= t && t < h && 0 <= y && y < w && !map[t][y]) dfs(map, t, y);
	}
}

int main() {
	//???
	idou[0].first = -1;
	idou[0].second = 0;
	//??????
	idou[1].first = -1;
	idou[1].second = 1;
	//???
	idou[2].first = 0;
	idou[2].second = 1;
	//??????
	idou[3].first = 1;
	idou[3].second = 1;
	//???
	idou[4].first = 1;
	idou[4].second = 0;
	//??????
	idou[5].first = 1;
	idou[5].second = -1;
	//???
	idou[6].first = 0;
	idou[6].second = -1;
	//??????
	idou[7].first = -1;
	idou[7].second = -1;

	while (1) {
		cin >> w >> h;
		if (!w && !h) break;
		ans = 0;
		vector<vector<ll> > map(h, vector<ll>(w, -1));
		rep(i, h) rep(j, w) {
			ll temp; cin >> temp;
			if (temp) map[i][j] = 0;
		}rep(i, h) rep(j, w) {
			if (!map[i][j]) {
				ans++;
				dfs(map, i, j);
			}
		}cout << ans << endl;
	}
	return 0;
}