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;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define REP(i, d, n) for(int i=(d); i<(n); ++i)
#define all(v) v.begin(), v.end()

const int x[] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int y[] = {-1, 0, 1, -1, 1, -1, 0, 1};

int dfs(int i, int j, vector<vector<int> >& graph, vector<vector<int> >& visit, const int h, const int w){
    if(visit[i][j] == 1 || graph[i][j] == 0){
        return 0;
    }
    visit[i][j] = 1;
    rep(k, 8){
        int di = i + x[k];
        int dj = j + y[k];
        if(di < 0 || dj < 0 || di >= h || dj >= w){
            continue;
        }
        if(visit[di][dj] == 1){
            continue;
        }
        dfs(di, dj, graph, visit, h, w);
    }
    return 1;
}

int main() {

    while(true){
        int w, h;
        cin >> w >> h;
        if(w == 0 && h == 0){
            break;
        }
        vector<vector<int> > graph(h, vector<int>(w)); 
        rep(i, h){
            rep(j, w){
                cin >> graph[i][j];
            }
        }
        vector<vector<int> > visit(h, vector<int>(w));
        int ans = 0;
        rep(i, h){
            rep(j, w){
                ans += dfs(i, j, graph, visit, h, w);
            }
        }
        cout << ans << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int x[50][50], col[50][50], h, w, c, i, j, k, l, p, q;
queue<int>Q, R;
int main() {
	while (true) {
		c = 0;
		memset(col, 0, sizeof(col));
		cin >> w >> h;
		if (h == 0) { break; }
		for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { cin >> x[i][j]; } }
		for (i = 0; i < 50; i++) {
			for (j = 0; j < 50; j++) {
				if (x[i][j] == 1 && col[i][j] == 0) {
					c++;
					col[i][j] = c;
					Q.push(i * 100 + j);
					while (!Q.empty()) {
						while (!Q.empty()) {
							p = Q.front() / 100;
							q = Q.front() % 100;
							for (k = -1; k < 2; k++) {
								for (l = -1; l < 2; l++) {
									if (x[p + k][q + l] == 1 && col[p + k][q + l] == 0) {
										col[p + k][q + l] = c;
										R.push((p + k) * 100 + q + l);
									}
								}
							}
							Q.pop();
						}
						while (!R.empty()) {
							Q.push(R.front());
							R.pop();
						}
					}
				}
			}
		}
		cout << c << endl;
	}
}

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

int state[100][100];
bool reached[100][100];
int w,h;

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

void dfs(int x, int y) {

	if(x==0 || w<x || y==0 || h<y || state[x][y] == 0)
		return;
	if(reached[x][y] == true)
		return;

	reached[x][y]=true;

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

int main() {

	memset(state, 0, sizeof(state));
	memset(reached, false, sizeof(reached));

	cin >> w >> h;
	for(int i=1; i<=w; i++) {
		for(int j=1; j<=h; j++) {
			cin >> state[i][j];
		}
	}
	int count=0;
	for(int i=1; i<=w; i++) {
		for(int j=1; j<=h; j++) {
			if(reached[i][j]==true || state[i][j] == 0)
				continue;
			else{
				dfs(i,j);
				count++;
			}
		}
	}
	cout << count << endl;
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,o,n) for(long long i = o;i<n;i++)
#define oneforall ios::sync_with_stdio(false);cin.tie(0);
#define all(v) (v).begin(),(v).end()
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define int long long 
const long long inf=1e18;
void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
void out(){cout << "\n";} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);}
typedef vector<int> vi;
typedef vector<long long> vl;
typedef long long ll;
typedef vector<pair<long, long > > vpll;
typedef vector<pair<int, int > > vpii;
#define FORR(x,arr) for(auto& x:arr)
#define ZERO(a) memset(a,0,sizeof(a))





//3111111111111111111111111111111

	int x,x1;
	int morty[310][310];
	int tmpx[3] = {-1,1,0};
	void dfs(int tmp,int tmp1){
		morty[tmp][tmp1] = 0;
		FOR(i,0,3)FOR(i1,0,3){
			int nowtmp = tmp+tmpx[i];
			int nowtmp1 = tmp1+tmpx[i1];
			if(!(nowtmp == tmp and nowtmp1 == tmp1) and nowtmp>=0 and nowtmp<=x-1 and nowtmp1>=0 and nowtmp1<=x1-1 and morty[nowtmp][nowtmp1] == 1){
				dfs(nowtmp,nowtmp1);
			}
		}
	}




	void solve(){
		
		while(1){
			
			in(x1,x);
			if(x == 0 and x1 == 0)break;
			FOR(i,0,x)FOR(i1,0,x1){
				ini(tmp);
				if(tmp == 0){
					morty[i][i1] = 0;
				}
				else{
					morty[i][i1] = 1;
				}
			}
			int rick = 0;
			//FOR(i,0,x){
			//	FOR(i1,0,x1){
				//cout<<morty[i][i1]<<" ";			
			//	}
				//out();
			//}
			FOR(i,0,x)FOR(i1,0,x1){
				if(morty[i][i1] == 1){
					dfs(i,i1);
					rick++;
				}
			}
			out(rick);
		}








		
		
	}























int32_t main() {
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall 
	oneforall

	solve();
	


	return 0;
}