Go: package main

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

var sc = bufio.NewScanner(os.Stdin)
var route [][]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func dfs(h, w int) {
	if route[h][w] == 0 {
		return
	}

	route[h][w] = 0
	for _, x := range []int{-1, 0, 1} {
		for _, y := range []int{-1, 0, 1} {
			if route[h+x][w+y] == 0 {
				continue
			}
			dfs(h+x, w+y)
		}
	}
}

func main() {
	writer := bufio.NewWriter(os.Stdout)
	for {
		w, h := scanInt(), scanInt()
		if w == 0 && h == 0 {
			break
		}

		route = make([][]int, h+2)
		route[0] = make([]int, w+2)
		for i := 1; i <= h; i++ {
			route[i] = make([]int, w+2)
			for j := 1; j <= w; j++ {
				route[i][j] = scanInt()
			}
		}
		route[h+1] = make([]int, w+2)

		total := 0
		for i := 1; i <= h; i++ {
			for j := 1; j <= w; j++ {
				if route[i][j] == 1 {
					total++
					dfs(i, j)
				}
			}
		}
		fmt.Fprintln(writer, total)
	}
	writer.Flush()
}

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

using namespace std;

typedef long long LL;
typedef vector<int>    VI;
typedef vector<vector<int>>    VVI;
typedef vector<bool>   VB;
typedef vector<string> VS;

#define MP make_pair
#define PB push_back
#define ALL(x)  (x).begin(),(x).end()
#define rep(i, N)  for (int i = 0; i < (int)(N); i++)
#define REP(i, init, N)  for (int i = (init); i < (int)(N); i++)
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define RANGE(x, min, max) min <= x && x <= max

const int DX[] = {-1, 0, 1, -1, 1, -1, 0, 1};
const int DY[] = {-1, -1, -1, 0, 0, 1, 1, 1};

int w, h;
VVI cs;

void dfs(int y, int x) {
	cs[y][x] = 0;
	rep (i, 8) {
		int ny = y + DY[i];
		int nx = x + DX[i];
		if (RANGE(nx, 0, w - 1) && RANGE(ny, 0, h - 1) && cs[ny][nx]) {
			dfs(ny, nx);
		}
	}
}

int main(void) {
	while (1) {
		cs.clear();
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		cs.resize(h, VI (w));
		rep (i, h) rep (j, w) {
			cin >> cs[i][j];
		}
		int ans = 0;
		rep (i, h) rep (j, w) {
			if (!cs[i][j]) continue;
			dfs(i, j);
			ans++;
		}
		cout << ans << endl;
	}
    return 0;
}

C++: #define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <iterator>
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
 
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())

typedef unsigned long long ull;
const int INF = (int)1e8;

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

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

int main(){
 
	while(cin>>w>>h,w||h){
		 FOR(i,h) FOR(j,w) cin>>s[i][j];
		 int ans = 0;
		 FOR(i,h) FOR(j,w){
			 if(s[i][j]){
				 dfs(i,j);
				 ans++;
			 }
		 }
		 cout << ans << endl;
	}
    return 0;
}

C++: // A0J 1160 How Many Islands?

#include <iostream>

# define REP(i,n) for(int i=0; i<n;i++)

using namespace std;

int map[51][51];
int moumita[51][51];
int w,h;

bool check(int j, int k){
	if (j>=0 && j<h && k>=0 && k<w && map[j][k]==1 && !moumita[j][k]) return true;
	else return false;
}

int dfs(int j, int k){
	moumita[j][k] = true;
	
	if (check(j-1,k-1)) dfs(j-1,k-1);
	if (check(j-1,k)) dfs(j-1,k);
	if (check(j-1,k+1)) dfs(j-1,k+1);
	if (check(j,k-1)) dfs(j,k-1);
	if (check(j,k+1)) dfs(j,k+1);
	if (check(j+1,k-1)) dfs(j+1,k-1);
	if (check(j+1,k)) dfs(j+1,k);
	if (check(j+1,k+1)) dfs(j+1,k+1);
	
	return 1;
}

int main(){
	
	int ans;
	
	ans = 0;
	
	cin >> w >> h;
	
	REP(j,h){
		REP(k,w){
			cin >> map[j][k];
			moumita[j][k] = false;
		}
	}
	
	REP(j,h){
		REP(k,w){
			if (check(j,k))	ans += dfs(j,k);
		}
	}
	
	cout << ans << endl;
	
	return 0;
	
}

C++: // 2009à\IB Í¢­Â éH
#include <iostream>

using namespace std;

int w, h;
int c[50][50];
// 8ßTÖÌÚ®ðè`
char dx[] = {-1,-1,-1,0,1,1,1,0};
char dy[] = {-1,0,1,1,1,0,-1,-1};

// ÄAÅ»ÝÌ¤©çBÅ«é¤ðÁµÁÄ¢­
void dfs(int x, int y){
	c[x][y] = 0;
	for(int i=0;i<8;i++){
		int nx = x+dx[i], ny = y+dy[i];
		// (nx,ny)ªtB[hàÉ é©ÆC»±ª¤©ð»è
		if(nx<0||h<=nx||ny<0||w<=ny||!c[nx][ny]) continue;
		dfs(nx,ny);
	}
}

int main(){
	while(cin >> w >> h, w){
		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]) ans++, dfs(i,j);
		cout << ans << endl;
	}
}

C++: #include<iostream>

using namespace std;

int idirs[] = {0, 0, 1, 1, 1, -1, -1, -1};
int jdirs[] = {1, -1, 0, 1, -1, 0, 1, -1};

int map[52][52];

void dfs(int i, int j) {
    if (!map[i][j]) return;
    map[i][j] = 0;
    for (int k = 0; k < 9; ++k) {
        dfs(i + idirs[k], j + jdirs[k]);
    }
}

int main() {
    while (1) {
        int w, h;

        cin >> w >> h;

        if (w == 0) break;

        for (int i = 0; i < 52; ++i)
            for (int j = 1; j < 52; ++j)
                map[i][j] = 0;

        for (int i = 1; i <= h; ++i) {
            for (int j = 1; j <= w; ++j) {
                cin >> map[i][j];
            }
        }

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

        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define mp make_pair

const vector<int> dx = {-1, -1, -1, 0, 0, 1, 1, 1};
const vector<int> dy = {-1, 0, 1, -1, 1, -1, 0, 1};

int main() {
    while (true) {
        int w, h;
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        vector<vector<int>> grid(h, vector<int>(w));
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                cin >> grid[i][j];
            }
        }
        int ans = 0;
        while (true) {
            stack<pair<int, int>> sk;
            bool flag = false;
            for (int i = 0; i < h; ++i) {
                for (int j = 0; j < w; ++j) {
                    if (grid[i][j]) {
                        sk.push(mp(i, j));
                        flag = true;
                        break;
                    }
                }
                if (flag) break;
            }
            if (!flag) {
                cout << ans << endl;
                break;
            }
            while (!sk.empty()) {
                pair<int, int> v = sk.top();
                grid[v.first][v.second] = 0;
                sk.pop();
                for (int i = 0; i < 8; ++i) {
                    pair<int, int> nv = mp(v.first + dx[i], v.second + dy[i]);
                    if (nv.first < 0 || nv.first >= h || nv.second < 0 || nv.second >= w) continue;
                    if (grid[nv.first][nv.second] == 0) continue;
                    grid[nv.first][nv.second] = 0;
                    sk.push(mp(nv.first, nv.second));
                }
            }
            ans++;
        }
    }
}