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>
using namespace std;
#define int long long
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define endl '\n'

const int N = 51;
int w,h;
int board[N][N];

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

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

void solve(){
    rep(i,h){
        rep(j,w) {
            cin >> board[i][j];
        }
    }

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

signed main() {
    while(true){
        cin >> w >> h;
        if(w == 0 && h == 0) return 0;
        solve();
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using PL = pair<ll, ll>;
// Welcome to my source code!

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

void dfs(int cx, int cy) {
    for (int dx = -1; dx <= 1; dx++) {
        for (int dy = -1; dy <= 1; dy++) {
            int nx = cx + dx, ny = cy + dy;
            if (0 <= nx && nx < h && 0 <= ny && ny < w && c[nx][ny] != 0) {
                c[nx][ny] = 0;
                dfs(nx, ny);
            }
        }
    }
}

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

C++: #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<map>
#include<tuple>
#include<bitset>
#include<iomanip>
#define rep(index,num) for(int index=0;index<num;index++)
#define rep1(index,num) for(int index=1;index<=num;index++)
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define kaigyo cout<<endl
#define eps 1e-15
#define mp(a1,a2) make_pair(a1,a2)
typedef long long ll;
using namespace std;
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl=1e+18+1;
int INF=1e+9+1;
struct UnionFind{
  vector<int> par;
  vector<int> sizes;
  UnionFind(int n) : par(n), sizes(n, 1) {
    rep(i,n) par[i] = i;
  }
  int root(int x) {
    if (x == par[x]) return x;
    return par[x] = root(par[x]);
  }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return;
    if (sizes[x] < sizes[y]) swap(x, y);
    par[y] = x;
    sizes[x] += sizes[y];
  }
  bool same(int x, int y) {
    return root(x) == root(y);
  }
  int size(int x) {
    return sizes[root(x)];
  }
};
int main(){
	//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp
	while(1){
		int w,h;
		scan(w);scan(h);
		if(w==0&&h==0) break;
		int A[51][51];
		rep(i,h){
			rep(j,w){
				scan(A[i][j]);
			}
		}
		UnionFind uf(h*w);
		rep(i,h){
			rep(j,w-1){
				if(A[i][j]==1&&A[i][j+1]==1) uf.unite(i*w+j,i*w+j+1);
			}
		}
		rep(i,h-1){
			rep(j,w){
				if(A[i][j]==1&&A[i+1][j]==1) uf.unite(i*w+j,(i+1)*w+j);
			}
		}
		rep(i,h-1){
			rep(j,w-1){
				if(A[i][j]==1&&A[i+1][j+1]==1) uf.unite(i*w+j,(i+1)*w+j+1);
				if(A[i+1][j]==1&&A[i][j+1]==1) uf.unite((i+1)*w+j,i*w+j+1);
			}
		}
		int ans=0;
		rep(i,h){
			rep(j,w){
				if(A[i][j]==0) continue;
				if(uf.root(i*w+j)==i*w+j) ans++;
			}
		}
		prin(ans);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double pi=3.141592653589793;
typedef unsigned long long ull;
typedef long double ldouble;
const ll INF=1e18;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)

// AOJ How Many Islands? 1160
int h, w;
vector<vector<bool> > square(50, vector<bool>(50));

// 幅優先探索
void dfs(int x, int y){

    // 一度通過した部分は塀とする
    square.at(x).at(y) = 0;

    rep2(dx, -1, 2){
        rep2(dy, -1, 2){
            int nx = x + dx;
            int ny = y + dy;
            if(0 <= nx && nx < w && 0 <= ny && ny < h && square.at(nx).at(ny) == 1) {
                dfs(nx, ny);
            }
        }
    }

    return;
}

int main(){
    h = 1;
    w = 1;
    while(true){
        cin >> w >> h;
        if(h == 0 && w == 0) break;
        int answer = 0;
    
        rep(i, h){
            rep(j, w) {
                int a;
                cin >> a;
                square.at(j).at(i) = a;
            }
        }
        rep(i, h){
            rep(j, w){
                if(square.at(j).at(i) == 1) {
                    dfs(j, i);
                    //cout << "x:" << j << " y:" << i << endl;
                    answer++;
                    //rep(i, h) {rep(j, w) {cout << square.at(j).at(i);} cout << "\n";} cout << "------\n";
                }
            }
        }
        cout << answer << endl;
    }
    
    


}