Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

C++: #include <iostream>
#include <vector>

using namespace std;

const int ddx[8] = {1,1,1,0,0,-1,-1,-1};
const int ddy[8] = {1,0,-1,1,-1,1,0,-1};

bool visited[60][60] ={false};
int v[60][60] = {0};
int h,w;

int dfs(int i ,int j){
    if(visited[i][j]) return 0;
    
    visited[i][j] = true;

    for(int x = 0; x < 8; x++){
        for(int y = 0; y < 8;y++){
            int posx = j + ddx[x];
            int posy = i + ddy[y];
            if(posx >= 0 && posx < w && posy >= 0 && posy < h){
                if(!visited[posy][posx] && v[posy][posx]){
                    dfs(posy, posx);
                }
            }
        }
    }
    return 1;
}

int main()
{
    while(cin >> w >> h, w and h){
        int ans = 0;

        for(int i = 0; i < 60 ;i++){
            for(int j = 0;j < 60; j++){
                visited[i][j] = false;
            }
        }

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

        for(int i = 0; i < h ;i++){
            for(int j = 0;j < w; j++){
                if(v[i][i] || !visited[i][j]) ans += dfs(i,j);
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
int W, H; 
char C[60][60] = {}; 
void search(int x, int y){
	C[x][y] = '0'; 
	for(int dx = -1; dx <= 1; dx++){
		for(int dy = -1; dy <= 1; dy++){
			int nx = x + dx; int ny = y + dy; 
			if(nx >= 0 && nx < H && ny >= 0 && ny < W && C[nx][ny] == '1') search(nx, ny); 
		}
	}
	return; 
}
int main() {
	for(int K; ; K++){
	cin >> W >> H; 
	if(W == 0){break;}
	for(int i = 0; i < H; i++){
		for(int j = 0; j < W; j++){
			cin >> C[i][j]; 
		}
	}
	int count = 0; 
	for(int i = 0; i < H; i++){
		for(int j = 0; j < W; j++){
			if(C[i][j] == '1'){search(i, j); count ++; }
		}
	}
	for(int i = 0; i < H; i++){
		for(int j = 0; j < W; j++){
			C[i][j] = '0'; 
		}
	}
	cout << count << endl; 
	}
}

C++: 
#include "bits/stdc++.h"
//#include <iostream>
//#include <vector>
//#include <string>
using namespace std;
#define repi(i,a,b) for(int i=(a), i##_len=(b); i<i##_len; ++i)
#define rep(i,n) repi(i,0,n)
#define repi_(i,a,b) for(int i=(a), i##_len=(b); i<=i##_len; ++i)
#define rep_(i,n) repi_(i,0,n)
#define all(x) (x).begin(),(x).end()
#define vsort(v) sort((v).begin(), (v).end())
#define gsort(v) tsort(v); reverse((v).begin(), (v).end())
#define vrev(v) reverse((v).begin(), (v).end())
#define siz(v) ((int)(v).size())
#define ers(v, n) (v).erase((v).begin() + n)
#define cnt(v, n) count(all(v), n)
#define vmin(v) *min_element(v.begin(), v.end())
#define vmax(v) *max_element(v.begin(), v.end())
#define el "\n"
#define co(x) cout<<(x)<<el
#define coc(c, a, b) if(c)co((a));else co((b))
#define cosp(x) cout<<(x)<<' '
#define YES(c) coc(c,"YES", "NO")
#define Yes(c) coc(c,"Yes", "No")
#define yes(c) coc(c,"yes", "no")
#define POSSIBLE(c) coc(c, "POSSIBLE", "IMPOSSIBLE")
#define Possible(c) coc(c, "Possible", "Impossible")
#define possible(c) coc(c, "possible", "impossible")
#define inf INT_MAX
#ifdef tqktmp_2
#define gc() getchar(); getchar()
#else
#define gc() 1
#endif
const unsigned int bf0 = (1 << 0);
const unsigned int bf1 = (1 << 1);
const unsigned int bf2 = (1 << 2);
const unsigned int bf3 = (1 << 3);
const unsigned int bf4 = (1 << 4);
const unsigned int bf5 = (1 << 5);
const unsigned int bf6 = (1 << 6);
const unsigned int bf7 = (1 << 7);

typedef long long lint;
typedef vector<int> ivec; typedef vector<string> svec;

const int
	dx8[8] = { 0,1,1,1,0,-1,-1,-1 },
	dy8[8] = { 1,1,0,-1,-1,-1,0,1 },
	dx4[4] = { 0,1,0,-1 },
	dy4[4] = { 1,0,-1,0 };

template<class T>inline bool maxi(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>inline bool mini(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }




lint w, h;
vector<ivec> m;
lint p, q;

int dfs(int x, int y) {//x, yからつながっている陸(1)をすべて2にし、1を返す
	if (m[x][y] != 1)return 0;
	m[x][y] = 2;
	rep(i, 8) {
		p = x + dx8[i], q = y + dy8[i];
		if (0 <= p && p < w && 0 <= q && q < h)dfs(p, q);
	}
	return 1;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(0);

	ivec in;
	lint ctr;
	while (cin >> h >> w) {
		if (h == 0 && w == 0)return 0;
		in.resize(h);
		rep(i, w) {
			rep(j, h) {
				cin >> in[j];
			}
			m.push_back(in);
		}
		ctr = 0;
		rep(i, w)rep(j, h) {
			ctr += dfs(i, j);
		}
		co(ctr);
		m.clear();
		vector<ivec>().swap(m);
	}
	gc();
}

C++: #include <iostream>
#include <cmath>
#include <vector>

using namespace std;

#define loop(i,a,b) for(int i=(a);i<int(b);i++)
#define rep(i,b) loop(i,0,b)

int c[200][200];
int vis[200][200];
int w,h;


void dfs(int ci, int cj){
    vis[ci][cj] = true;
    int di[] = {0,1,0,-1, 1,1,-1,-1};
    int dj[] = {1,0,-1,0, -1,1,-1,1};
    rep(d,8){
        int ni = ci+di[d];
        int nj = cj+dj[d];
        if(ni < 0 || nj < 0) continue;
        if(ni >= h || nj >= w) continue;
        if(vis[ni][nj]) continue;
        if(c[ni][nj]==0) continue;
        dfs(ni,nj);
    }
}

int main(){
    while(cin >> w >> h && w){
        rep(i,h)rep(j,w) cin >> c[i][j], vis[i][j] = 0;
        int ans = 0;
        rep(i,h)rep(j,w){
            if(vis[i][j]) continue;
            if(c[i][j] == 0) continue;
            dfs(i,j);
            ans++;
        }
        cout << ans << endl;
    }
}

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

using namespace std;

int c[50][50];

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

int ret;

int w,h;

void dfs(int x,int y){
	if(c[x][y] == 0) return;
	c[x][y] = 0;
	for(int i=0;i<8;i++){
		if(x+dx[i] < 0 || x+dx[i] >= w) continue;
		if(y+dy[i] < 0 || y+dy[i] >= h) continue;
		dfs(x+dx[i],y+dy[i]);
	}
}

int main(){
	while(cin >> w >> h,w){
		ret = 0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin >> c[j][i];
			}
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(c[j][i] == 1){
					ret++;
					dfs(j,i);
				}
			}
		}
		cout << ret << endl;
	}
	return 0;
}