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;
typedef long long ll;
typedef pair< ll, ll > Pi;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define rep3(i,i0,n) for(int i=i0;i<(n);i++)
#define pb push_back
#define mod 1000000007
#define INF 200000000000
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()

bool compare(Pi a, Pi b) {
    if(a.second != b.second){
        return a.second > b.second;
        
    }else{
        return a.first < b.first;
    }
}
bool In_map(ll y,ll x,ll h,ll w){
    if(y<0 || x<0 || y>=h || x>=w){
        return 0;
    }else{
        return 1;
    }
}
const vector<ll> dx{1,0,-1,0,1,1,-1,-1};
const vector<ll> dy{0,1,0,-1,1,-1,1,-1};


ll cnt=0;
vector<vector<bool>> seen(110,vector<bool>(110));
vector<vector<bool>> seen2(110,vector<bool>(110));
vector<vector<bool>> G;
ll W,H;
void dfs(ll h, ll w){
    seen[h][w] = 1;
    cnt++;
    rep(dir,8){
        ll nh = h + dy[dir];
        ll nw = w + dx[dir];

        if(!In_map(nh,nw,H,W)) continue;
        if(G[nh][nw] == 0) continue;
        if(seen[nh][nw]) continue;
        dfs(nh,nw);
    }



}


int main() {
    while(1){
    seen = seen2;
    G.clear();
    cin>>W>>H;
    if(W==0 && H==0) break;
    ll num=0;
    rep(i,H){
        G.emplace_back();
        rep(j,W){
            bool c;
            cin>>c;
            G[i].pb(c);
        }
    }
    
    ll cnt=0;
    rep(i,H){
        rep(j,W){
            if(G[i][j] == 0) continue;
            if(seen[i][j]) continue;
            dfs(i,j);
            cnt++;
            
        }
    }

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

}

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

#define rep(i, n) for(int i = 0; i < n; i++)

typedef long long ll;
using namespace std;
int mod = 1000000007;

int grid[52][52];
bool visited[52][52];

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

void init(){
    rep(i, 52) rep(j, 52) {grid[i][j] = 0; visited[i][j] = false;}
}

void dfs(int px, int py, int sx, int sy){
    visited[sx][sy] = true;
    rep(i, 8){
        int nx = sx+dx[i];
        int ny = sy+dy[i];
        if(grid[nx][ny] == 1 && !visited[nx][ny]){
            dfs(sx, sy, nx, ny);
        }
    }
}

int main(void){
    while(true){
        int res = 0;
        init();
        int W, H; cin >> W >> H;
        if(W == 0) break;
        rep(i, H){
            rep(j, W){
                cin >> grid[i+1][j+1];
            }
        }
        for(int i = 1; i <= H; i++){
            for(int j = 1; j <= W; j++){
                if(grid[i][j] == 1 && !visited[i][j]){
                    dfs(-1, -1, i, j);
                    res++;
                }
            }
        }
        cout << res << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;

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;
void search(int h, int w, vector<vector<int>> &c, vector<vector<bool>> &reached) {
    reached.at(h).at(w) = true;

    for (int direction = 0; direction < 8; ++direction) {
        int nh = h + dx[direction];
        int nw = w + dy[direction];
        if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
        if (c.at(nh).at(nw) == 0) continue;
        if (reached.at(nh).at(nw) == true) continue;
        search(nh, nw, c, reached);
    }
}

int main() {
    while (true) {
        int ans = 0;
        cin >> W >> H;
        if (H == 0 && W == 0) return 0;
        vector<vector<int>> c(H, vector<int>(W));
        vector<vector<bool>> reached(H, vector<bool>(W));
        rep(i,H) rep(j,W) cin >> c.at(i).at(j);

        rep(i,H) rep(j,W) {
            if (c.at(i).at(j) == 1 && reached.at(i).at(j) == false) {
                ++ans;
                search(i, j, c, reached);
            }
        }
        cout << ans << endl;
    }
}