Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

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

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for(ll i=0;i<(ll)(n);i++)
#define rep2(i, m, n) for(ll i=m;i<(ll)(n);i++)
#define rrep(i, n, m) for(ll i=n;i>=(ll)(m);i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

vector<vector<ll>> grid;
vector<vector<bool>> visited;
ll H, W;

void dfs(ll i, ll j) {
    if (visited[i][j]) return;
    visited[i][j] = true;
    rep(d, 8) {
        ll y = i + ddx[d];
        ll x = j + ddy[d];
        if (0 <= x && x < W && 0 <= y && y < H && grid[y][x]) {
            dfs(y, x);
        }
    }
    return;
};

void solve() {
    while (true) {
        cin >> W >> H;
        if (W == 0 && H == 0) return;
        grid.assign(H, vector<ll>(W, 0));
        rep(i, H) {
            rep(j, W) {
                cin >> grid[i][j];
            }
        }
        ll ans = 0;
        visited.assign(H, vector<bool>(W, false));
        rep(i, H) {
            rep(j, W) {
               if (grid[i][j] && !visited[i][j])  {
                   ans++;
                   dfs(i, j);
               }
            }
        }
        cout << ans << '\n';
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <cmath>
#include <string>
#define FI first
#define SE second
#define PF push_front
#define PB push_back
#define PPF pop_front
#define PPB pop_back
#define MA make_pair
#define ll long long
#define PA pair<int,int>
#define VE vector<int>
#define VP vector<PA>
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define ROF(i,a,b) for(int i=b-1;i>=a;i--)
#define YES(i) cout<<(i?"YES":"NO")<<endl
#define Yes(i) cout<<(i?"Yes":"No")<<endl
using namespace std;
//
const int INF=1e9+7;
const int mod=1e9+7;
//
struct poi{
	int X;int Y;int Z;
	bool operator<(const poi&R)const{
		return X==R.X ? Y==R.Y ? Z<R.Z : Y<R.Y : X<R.X;
	}
};
/*
ll GCD(ll A,ll B){
  return (B==0)?(A):(GCD(B,A%B));
}
ll LCM(ll A,ll B){
  return A/GCD(A,B)*B;
}
*/
int W,H;
int C[50][50];
void DFS(int x,int y){
	C[x][y]=0;
	FOR(i,-1,2){
		FOR(j,-1,2){
			if(0<=x+i&&x+i<H&&0<=y+j&&y+j<W&&C[x+i][y+j]==1){
				DFS(x+i,y+j);
			}
		}
	}
}
int main(){
	while(1){
		cin>>W>>H;
		if(W==0&&H==0){
			break;
		}
		//
		FOR(i,0,H){
			FOR(j,0,W){
				cin>>C[i][j];
			}
		}
		int ans=0;
		FOR(i,0,H){
			FOR(j,0,W){
				if(C[i][j]==1){
					DFS(i,j);
					ans++;
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstring>
using namespace std;
#define REP(i,n) for(int i=0; i<(int)(n); i++)

int w,h;
int island[50][50];
int visited[50][50];
const int dx[] = {0,-1,-1,-1,0,1,1,1};
const int dy[] = {-1,-1,0,1,1,1,0,-1};

void umeru(int x, int y){
	
	visited[y][x] = 1;
	//printf("%d,%d\n",x,y);
	REP(k,8){
		int tx = x + dx[k];
		int ty = y + dy[k];
		if( tx<0 || ty<0 || tx>=w || ty>=h )continue;
		if( island[ty][tx]==0 || visited[ty][tx]==1 )continue;
		umeru(tx,ty);
	}
	return;
}
int main(){
	while(cin>>w>>h,w|h){
		int ans = 0;
		memset(visited,0,sizeof(visited));
		REP(i,h)REP(j,w)cin>>island[i][j];
		REP(i,h)REP(j,w)if( island[i][j] && !visited[i][j] ){
			umeru(j,i);
			ans++;
		}
		cout << ans << endl;
	}
	return 0;
}