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;
#define P pair<ll,ll>
#define FOR(I,A,B) for(ll I = (A); I < (B); ++I)
#define FORR(I,A,B) for(ll I = (B-1); I >= (A); --I)
#define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9
#define REV(x) (reverse(x.begin(),x.end())) //reverse
ll gcd(ll a,ll b){if(a<b)swap(a,b);if(a%b==0)return b;else return gcd(b,a%b);}
ll lcm(ll a,ll b){ll c=gcd(a,b);return ((a/c)*(b/c)*c);}//saisyo kobaisu
#define NEXTP(x) next_permutation(x.begin(),x.end())
const ll INF=1e18+7;
const ll MOD=1e9+7;

class UnionFind{
public:
	vector<int> par;
	//0-indexed
	void init(int n = 1) {
		par.resize(n);
		for (int i = 0; i < n; ++i) par[i] = i;
	}
	int root(int x = 1){
		if(par[x]==x)return x;
		else{
			return par[x] = root(par[x]);
		}
	}
	void unite(int x = 1,int y = 1){
		x = root(x);
		y = root(y);
		if(x==y)return;
		par[x] = y;
	}
};

bool solve(){
	ll H,W;
	cin >> W >> H;
	UnionFind island;
	island.init(W*H);
	ll dx[] = {0,1,0,-1,1,1,-1,-1};
	ll dy[] = {1,0,-1,0,1,-1,1,-1};
 	if(H==0&&W==0)return false;
	ll tile[H][W];
	ll zero = 0;
	FOR(i,0,H)FOR(j,0,W)cin>>tile[i][j];
	FOR(i,0,H)FOR(j,0,W){
		if(tile[i][j]==1){
			ll x = i;
			ll y = j;
			FOR(k,0,8)if(x+dx[k]<H&&x+dx[k]>=0&&y+dy[k]<W&&y+dy[k]>=0){
				ll nx = x+dx[k];
				ll ny = y+dy[k];
				if(tile[nx][ny]==1)
					island.unite(x*W+y,nx*W+ny);
			}
		}else{
			zero++;
		}
	}
	set<ll> s;
	FOR(i,0,H*W)s.insert(island.root(i));
	cout << s.size() - zero << endl;
	return true;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	while(solve());
}

C++: #include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>

using namespace std;

namespace {
    const int MAX_W = 50;
    const int MAX_H = 50;
    int W, H;
    int F[MAX_H][MAX_W];
    bool input() {
        cin >> W >> H;
        if (W == 0 && H == 0) return false;
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                cin >> F[i][j];
            }
        }
        return true;
    }

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

    void solve() {
        bool used[H][W];
        memset(used, 0, sizeof(used));
        int ans = 0;
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                if (F[i][j] == 0) continue;
                if (used[i][j]) continue;
                typedef pair<int, int> P;
                queue<P> Q;
                Q.push(P(i, j));
                ans++;
                used[i][j] = true;
                while (!Q.empty()) {
                    P c = Q.front(); Q.pop();
                    int y = c.first, x = c.second;
                    for (int k = 0; k < 8; k++) {
                        int ny = y + dy[k];
                        int nx = x + dx[k];
                        if (ny < 0 || ny >= H) continue;
                        if (nx < 0 || nx >= W) continue;
                        if (F[ny][nx] == 0) continue;
                        if (used[ny][nx]) continue;
                        used[ny][nx] = true;
                        Q.push(P(ny, nx));
                    }
                }
            }
        }
        cout << ans << endl;
    }
}

int main() {
    while (input()) solve();
    return 0;
}

C++: #include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>

using namespace std;
#define ll long long
#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--)
const ll mod = 1e9 + 7;


int w;
int h;
vector<vector<int> > c(50, vector<int>(50));
vector<vector<bool> > reached(50, vector<bool>(50,false));

void dfs(int y, int x){
    reached.at(y).at(x) = true;

    for(int dy = -1; dy <= 1; dy++){
        for(int dx = -1; dx <= 1; dx++){
            int nx = x + dx;
            int ny = y + dy;
			if(ny < 0 || h <= ny || nx < 0 || w <= nx || c.at(ny).at(nx) == 0) continue;          
            if(reached.at(ny).at(nx)) continue;
            dfs(ny,nx);

       }
    }
    
}




int main (){
while(cin>>w>>h,w!=0&&h!=0){
	
  	
    REP(i,h){//y
        REP(j,w){//x
          	reached.at(i).at(j) = false;
            cin >> c.at(i).at(j);
        }
    }
    int ans = 0;
    REP(i,h){
        REP(j,w){
            if(!reached.at(i).at(j) && c.at(i).at(j) == 1){
                dfs(i,j);
                ans++;
            }
            
        }
    }

    cout << ans << endl;




}

    return 0;
}