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<algorithm>//sort,二分探索,など
#include<bitset>//固定長bit集合
#include<cmath>//pow,logなど
#include<complex>//複素数
#include<deque>//両端アクセスのキュー
#include<functional>//sortのgreater
#include<iomanip>//setprecision(浮動小数点の出力の誤差)
#include<iostream>//入出力
#include<map>//map(辞書)
#include<numeric>//iota(整数列の生成),gcdとlcm(c++17)
#include<queue>//キュー
#include<set>//集合
#include<stack>//スタック
#include<string>//文字列
#include<utility>//pair
#include<list>
#include<vector>//可変長配列
using namespace std;
#define rep(i,a) for(int i=0;i<a;i++)
#define mp make_pair
#define pb push_back
#define ll __int64
#define __int64 long long
#define P pair<int,int>
#define PP pair<P,int>
#define F first
#define S second
int dx[8] = { 1,0,-1,0,1,-1,-1,1 };
int dy[8] = { 0,1,0,-1,1,1,-1,-1 };


int w, h;
int t[100][100];
bool visit[100][100];

void dfs(int ny, int nx) {
    if (visit[ny][nx])
        return;
    visit[ny][nx] = 1;

    rep(i, 8) {
        int x = nx + dx[i], y = ny + dy[i];
        if (x < 0 || y < 0 || x >= w || y >= h)
            continue;

        if(t[y][x]==1)
            dfs(y, x);
    }
}
int main() {
    while (cin >> w >> h, w || h) {
        rep(i, h)rep(j, w)cin >> t[i][j];
        rep(i, h)rep(j, w)visit[i][j] = 0;
        int ans = 0;
        rep(i, h) {
            rep(j, w) {
                if (!visit[i][j] && t[i][j] == 1) {
                    dfs(i, j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

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

int n;
vector<vector<int> > C;
vector<vector<int> > D;

void bfs(int initCX, int initCY){
    queue<int> Q;
    Q.push(initCX);
    Q.push(initCY);
    D[initCX][initCY] = 0; // ?????????
    while(!Q.empty()){
        // ??????????´???????????????????
        int cx, cy;
        cx = Q.front();
        Q.pop();
        cy = Q.front();
        Q.pop();
        for(int i = -1; i <= 1; ++i){
            for(int j = -1; j <= 1; ++j){ // ??????????????????????????????
                if(C[cx + i][cy + j] == 1 && D[cx + i][cy + j] == -1){ // ???????????£??????????¨??????????
                    D[cx + i][cy + j] = D[cx][cy] + 1;
                    // ????????????????¨??????????????????????
                    Q.push(cx + i);
                    Q.push(cy + j);
                }
            }
        }
    }
}

int main(){
    int w, h;
    // index?????????????°???????????????£????????§???????????§h??¨w?????\?????????????????????????????????????????????????????????
    while(cin >> h >> w, w != 0 || h != 0){
        C = vector<vector<int> >(w+2, vector<int>(h+2, 0));
        for(int i=1; i<=w; ++i){
            for(int j=1; j<=h; ++j){
                cin >> C[i][j];
            }
        }
        D = vector<vector<int> >(w+2, vector<int>(h+2, 0));
        for(int i=1; i<=w; ++i){
            for(int j=1; j<=h; ++j){
                D[i][j] = -1;
            }
        }
        int n = 0;
        for(int i=1; i<=w; ++i){
            for(int j=1; j<=h; ++j){
                if(C[i][j] == 1 && D[i][j] == -1){ // ???????????£??????????¨??????????
                    bfs(i, j);
                    ++n;
                }
            }
        }
        cout << n << endl;
    }
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <tuple>
using namespace std;
using ll=long long;
using R=double;

int w,h;

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

void dfs(int w, int h){
	field[h][w] = 0;

	for(int nh=h-1;nh<h+2;nh++){
	    for(int nw=w-1;nw<w+2;nw++){
	      if(field[nh][nw]==1&&0<=nh&&0<=nw&&nh<h&&nw<w)dfs(nw,nh);
	    }
	  }
	return;
}

int main(){
	while(1){
		cin >> w >> h;
		if(w==0 and h==0){
			break;
		}

		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin >> field[i][j];
			}
		}

		int count = 0;

		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				if(field[i][j]==1){
					count++;
					dfs(j,i);
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}

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

int n,m,a[50][50],b[50][50],ans;
bool inRange(int z,int l,int r){
	return z>=l&&z<r;
}
void fill(int x,int y){
	if(!a[x][y]||b[x][y])return;
	b[x][y]=true;
	for(int xx=-1;xx<=1;xx++)if(inRange(x+xx,0,n)){
		for(int yy=-1;yy<=1;yy++)if(inRange(y+yy,0,m)){
			fill(x+xx,y+yy);
		}
	}
}
int main(){
	while(scanf("%d%d",&m,&n),n*m){
		ans=0;
		memset(b,0,sizeof(int)*2500);
		for(int i=0;i<n;i++)
			for(int j=0;j<m;j++)
				scanf("%d",&a[i][j]);
		for(int i=0;i<n;i++)
			for(int j=0;j<m;j++)
				if(!b[i][j]&&a[i][j])
					ans++,fill(i,j);
		printf("%d\n",ans);
	}

	return 0;
}

C++: #include <iostream>
using namespace std;
 
void f(int n){
if(n%100==0){
cerr<<n<<endl;
}
f(n+1);
}
 
int main(){
f(1);
}