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++: /*
    Problem 25
    https://onlinejudge.u-aizu.ac.jp/problems/1160
*/
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 55;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
int h, w;
vector<vector<bool>> visited(MAX, vector<bool>(MAX));
vector<vector<int>> G(MAX, vector<int>(MAX));
/* function */
void init() {
    // for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) G[i][j] = false;
    // for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) visited[i][j] = false;
    for (int i = 0; i < h*w; i++) G[i/w][i%w] = false;
    for (int i = 0; i < h*w; i++) visited[i/w][i%w] = false;
}
void inputG() { // for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cin >> G[i][j];
    for (int i = 0; i < h*w; i++) cin >> G[i/w][i%w];}
bool check(int x, int y) {
    if (x < 0 || h < x || y < 0 || w < y) return false;
    if (visited[x][y] || !G[x][y]) return false;
    return true;
}
void bfs(int sx, int sy) {
    queue<pii> q;
    q.push(pii(sx, sy));
    visited[sx][sy] = true;
    while (!q.empty()) {
        pii p = q.front(); q.pop();
        int x = p.first, y = p.second;
        // 八方向を探索
        for (int dx = -1; dx <= 1; dx++) for (int dy = -1; dy <= 1; dy++) {
            int nx = x + dx, ny = y + dy;
            if (check(nx, ny)) {
                q.push(pii(nx, ny));
                visited[nx][ny] = true;
            }
        }
    }
}
void dfs(int x, int y) {
    // 訪れた
    visited[x][y] = true;
    // 八方向を探索
    for (int dx = -1; dx <= 1; dx++) for (int dy = -1; dy <= 1; dy++) {
        int nx = x + dx, ny = y + dy;
        // 範囲外 or 訪れたことがある(visited[i][j] == true) or 海(G[i][j] = 0)
        if (check(nx, ny))
            // 再帰的に探索
            dfs(nx, ny);
    }
}
void solve() {
    int ans = 0;
    for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) {
        if (!visited[i][j] && G[i][j]) {
            ans++;
            //bfs(i, j);
            dfs(i, j);
        }
    }
    cout << ans << '\n';
}

/* main */
int main(){
    while (1) {
        cin >> w >> h;
        if (h == 0) return 0;
        init();
        inputG();
        solve();
    }
}

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

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)   FOR(i,0,n)
#define ALL(a)     (a).begin(),(a).end()
#define VI         vector<int>
#define MOD 1000000007

typedef long long int ll;

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

const ll INF=(ll)1e19;
//const int INF=(1<<30);
int h,w;
int m[50][50];

int dx[] = {0,1,-1,0,1,-1,1,-1};
int dy[] = {1,0,0,-1,1,-1,-1,1};
bool visited[50][50];

int dfs(int x, int y, int d){
  if(m[x][y]==0)return d;
  if(visited[x][y])return d;
  visited[x][y]=true;

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

    if(nx < 0 || nx >= h || ny <0 || ny >= w)continue;
    if(m[nx][ny]==0)continue;
    if(visited[nx][ny])continue;

    dfs(nx, ny, d);
  }

  return d+1;
}

int main(){
  while(true){

    cin >> w >> h;
    if(h+w==0)break;
    REP(i,h)REP(j,w)cin >> m[i][j];
    REP(i,h)REP(j,w)visited[i][j]=false;

    int d = 0;
    REP(i,h)REP(j,w){
      d = dfs(i,j,d);
    }
    cout << d << endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
using Int = long long;
Int const INF = 1e10;

int w,h; int fie[50][50];
void dfs(int y,int x){
    fie[y][x]=0;
    for (int i = -1; i <= 1; i++){
        for (int j = -1; j <= 1; j++){
            int ny=y+i,nx=x+j;
            if(ny>=0&&ny<h&&nx>=0&&nx<w&&fie[ny][nx]==1)dfs(ny,nx);
        }
    }
}

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

    while(true){
        cin>>w>>h; if(w+h==0)break;
        for (int i = 0; i < h; i++){
            for (int j = 0; j < w; j++) cin>>fie[i][j];
        }
        int ans=0;
        for (int i = 0; i < h; i++){
            for (int j = 0; j < w; j++){
                if(fie[i][j]==1){
                    dfs(i,j);
                    ans++;
                }
            }
        }
        cout<<ans<<'\n';
    }
}

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


int islands[50][100];
int w,h;

void dfs(int y,int x){

  islands[y][x] = 0;

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

  for(int i = 0;i < 8;i++){
    int nx = x + dx[i];
    int ny = y + dy[i];
    if(0 <= nx && nx < w && 0 <= ny && ny < h && islands[ny][nx] == 1)dfs(ny,nx);
  }

  return ;
}


int main(){

  vector<int> v;

  while(scanf("%d %d",&w,&h),w != 0 && h != 0){

    int ans = 0;

    for(int i = 0;i < h;i++){
      for(int j = 0;j < w;j++){
        scanf("%d",&islands[i][j]);
      }
    }

    for(int i = 0;i < h;i++){
      for(int j = 0;j < w;j++){
        if(islands[i][j] == 1){
          dfs(i,j);
          ans++;
        }
      }
    }


    v.push_back(ans);
  }

  for(int i = 0;i < v.size();i++){
    printf("%d\n",v[i]);
  }

  return 0;
}