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 <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll MOD = 1000000007;

#define vec                vector<int>
#define vecll              vector<ll>
#define vecd               vector<double>
#define vecst              vector<string>
#define vecb               vector<bool>
#define vec2(var, n, m)    vector<vector<int>> var(n, vector<int>(m, 0))
#define vecll2(var, n, m)  vector<vector<ll>> var(n, vector<ll>(m, 0))

#define rep(i,n)         for(ll i=(ll)0; i<(ll)n; i++)
#define REP(i,m,n)       for(ll i=(ll)m; i<(ll)n; i++)
#define arr(var, n)      vec var(n); rep(i,n){cin >> var[i];}
#define arrll(var, n)    vecll var(n); rep(i,n){cin >> var[i];}
#define arrst(var, n)    vecst var(n); rep(i,n){cin >> var[i];}
#define all(var)         (var).begin(), (var).end()
#define sortall(var)     sort(all(var))
#define uniqueall(v)     v.erase(unique(v.begin(), v.end()), v.end());
#define f_sum(var)       accumulate(all(var), 0)
#define f_sumll(var)     accumulate(all(var), 0LL)
#define chmin(v1, v2)    v1 = min(v1, v2)
#define chmax(v1, v2)    v1 = max(v1, v2)
#define pb(var)          push_back(var)
#define prt(var)         cout << (var) << "\n"
#define prtd(n, var)     cout << fixed << setprecision(n) << var << "\n"
#define prtfill(n, var)  cout << setw(n) << setfill('0') << var;
#define prt2(v1, v2)     cout << v1 << " " << v2 << "\n"
#define prt3(v1, v2, v3) cout << v1 << " " << v2 << " " << v3 << "\n"
#define prtall(v)        rep(i,v.size()){cout<<v[i]<<(i!=v.size()-1?" ":"\n");}
void prtok(bool ok){prt(ok ? "yes" : "no");}
//----------------------------------------------------------------
bool ch[51][51];
int h, w;

void dfs(vector<vector<bool>> mp, int i, int j){
  //prt3("--->", i, j);
  ch[i][j] = true;
  bool up=true, down=true, left=true, right=true;

  if(i==0) up=false;
  if(i==h-1) down=false;
  if(j==0) left=false;
  if(j==w-1) right=false;

  if(up && left){if(mp[i-1][j-1] && !ch[i-1][j-1]) dfs(mp, i-1, j-1);}
  if(up) {if(mp[i-1][j] && !ch[i-1][j]) dfs(mp, i-1, j);}
  if(up && right) {if(mp[i-1][j+1] && !ch[i-1][j+1]) dfs(mp, i-1, j+1);}
  if(left) {if(mp[i][j-1] && !ch[i][j-1]) dfs(mp, i, j-1);}
  if(right) {if(mp[i][j+1] && !ch[i][j+1]) dfs(mp, i, j+1);}
  if(down && left) {if(mp[i+1][j-1] && !ch[i+1][j-1]) dfs(mp, i+1, j-1);}
  if(down) {if(mp[i+1][j] && !ch[i+1][j]) dfs(mp, i+1, j);}
  if(down && right) {if(mp[i+1][j+1] && !ch[i+1][j+1]) dfs(mp, i+1, j+1);}
}

int main(void) {
  while(1){
    rep(i,51){
      rep(j,51){
        ch[i][j]=false;
      }
    }
    cin >> w >> h;

    if(w==0 && h==0){break;}
    vector<vector<bool>> mp(h, vector<bool>(w, false));
    rep(i,h){
      rep(j,w){
        int tmp;
        cin >> tmp;
        mp[i][j] = (tmp>0 ? true : false);
      }
    }

    int ans=0;
    rep(i,h){
      rep(j,w){
        if(!ch[i][j] && mp[i][j]){
          ans++;
          dfs(mp, i, j);
        }
      }
    }
    prt(ans);
  }
}

C++: #include <iostream>

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

void dfs(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,ny= y+dy;
            if(0<=nx&&nx<h&&0<=ny&&ny<w&&c[nx][ny] == 1){
                dfs(nx,ny);
            }
        }
    }
    return;
}

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

C++: #include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<functional>
#define rep(i, n) for(int i = 0; i < n; i++)
#define repp(i, a, b) for(int i = a; i <= b; i++)
using namespace std;
using ll = long long;
using vel = vector<ll>;
using vvel = vector<vel>;

vvel maze;
ll w, h;

ll dfs(ll y, ll x) {
    if(maze[y][x]!=1) return 0;
    maze[y][x] = -1;
    repp(dy, -1, 1) {
        repp(dx, -1, 1) {
            if(y+dy < 0 || y+dy >= h || x+dx < 0 || x+dx >= w ) continue;
            if(dx == 0 && dy == 0) continue;
            dfs(y+dy, x+dx);
        }
    }
    return 1;
};

int main() {
    while(cin >> w >> h, w) {
        maze.assign(h, vel(w, 0));
        rep(y, h) rep(x, w) cin >> maze[y][x];
        ll ans = 0;
        rep(y, h) {
            rep(x, w) {
                if(maze[y][x] == 1) 
                    ans += dfs(y, x);
            }
        }
        cout << ans << endl;
    }
}

C++: // AOJ 1160
#include<iostream>
#define rep(i,a) for( int i = 0; i != (a); ++i )

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

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

void dfs( int y, int x )
{
	c[y][x] = false;

	rep( i, 8 )
	{
		int nx = x+dx[i], ny = y+dy[i];

		if( nx >= 0 && nx < w && ny >= 0 && ny < h && c[ny][nx] )
			dfs( ny, nx );
	}

	return;
}

int main()
{
	while( std::cin >> w >> h, w|h )
	{
		rep( i, h )
			rep( j, w )
				std::cin >> c[i][j];

		int ans = 0;
		rep( i, h )
			rep( j, w )
				if( c[i][j] )
					dfs( i, j ), ++ans;

		std::cout << ans << std::endl;
	}

	return 0;
}