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 <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);
}

C++: #include <iostream>
#include <fstream>
#include <stdio.h>

using namespace std;

int w,h;
int c[50][50];

typedef struct Point{
    int x;
    int y;
}Point;

bool solve(Point p, int *a){
    
    int res = *a;
    if(c[p.y][p.x] == 1){
        res++;
        c[p.y][p.x] = 0;
        for(int i=-1; i<2; i++){
            for(int j=-1; j<2; j++){
                if(!(i==0 && j==0)){
                    if(0<=p.y+i && p.y+i <h && 0<=p.x+j && p.x+j <w){
                        if(c[p.y+i][p.x+j] == 1){
                        solve(Point{p.x+j,p.y+i}, &res);
                        }
                    }
                }
            }
        }
    }
    
    if(res>0)
    return true;
    
    return false;
}

int main(int argc, const char * argv[]) {
    // insert code here...
    /////////
    //write//
    /////////
    
    while(1){
        cin >> w >> h;
        
        if(w==0 && h==0)break;
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                cin >> c[i][j];
            }
        }
        
        int a = 0;
        int cnt = 0;
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                if(solve(Point{j,i}, &a)){
                    //cout << "x:" << j << ",y:" << i << endl;
                    cnt++;
                }
            }
        }
        
        cout << cnt << endl;
    }
    
    return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i = 0;i<n;i++)
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0;}

int w,h;
int c[55][55];

void dfs(int x,int y) {
    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) continue;
            if(c[nx][ny] == 0) continue;
            if(c[nx][ny] == 1) c[nx][ny] = 0;
            dfs(nx,ny);
        }
    }
}

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

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;++i)
vector<vector<int>> C(50,vector<int>(50));
int cnt=0,w,h;

void dfs(int x,int y){
    C.at(x).at(y) = 0;
    for(int i=-1;i<2;++i){
        for(int j=-1;j<2;++j){
            int nh = x + i;
            int nw = y + j;
            if(0<=nw&&nw<w&&0<=nh&&nh<h&&C.at(nh).at(nw)==1){
            dfs(nh,nw);
            }
        }
        
    }
}

int main(){
    while(true){
        cin >> w >> h;
        if(w==0&&h==0){
            return 0;
        }
        rep(i,50){
            rep(j,50){
                C.at(i).at(j) = 0;
            }
        }
        cnt = 0;
        rep(i,h){
            rep(j,w){
                cin >> C.at(i).at(j);
            }
        }
       rep(i,h){
            rep(j,w){
                if(C.at(i).at(j)==1){
                    dfs(i,j);
                    ++cnt;
                }   
            }
        }
        cout << cnt << endl;    
    }
}

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

#define FOR(i,a,b) for(int i=(a);i<(int)(b);i++)
#define rep(i,n) FOR(i,0,n)
#define ZERO(a) memset(a,0,sizeof(a))

const int M = 55;
int w,h;
int f[M][M];
int dx[] = {1,1,1,0,0,-1,-1,-1};
int dy[] = {0,1,-1,1,-1,0,1,-1};

void dfs(int x, int y) {
    if(x == w || y == h) return ;
    f[y][x] = 0;
    rep(i,8) {
        int nx = x+dx[i], ny = y+dy[i];
        if(f[ny][nx] == 1)
            dfs(nx, ny);
    }
}

int main() {
    while(cin>>w>>h && w) {
        ZERO(f);
        rep(i,h) rep(j,w) cin >> f[i][j];

        int ans = 0;
        rep(i,h) rep(j,w) {
            if(f[i][j] == 0) continue;
            dfs(j,i);
            ans++;
        }

        cout << ans << endl;
    }

    return 0;
}