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 <iostream>
using namespace std;

int w, h;
bool state[51][51];

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

bool isRegion(int x, int y){
    return x >= 0 && x < w && y >= 0 && y < h;
}

void dfs(int x, int y){
    state[x][y] = 0;
    
    for(int i = 0; i < 8; i++){
        int nx = x + dx[i], ny = y + dy[i];
        if(isRegion(nx, ny) && state[nx][ny]){
            dfs(nx, ny);
        }
    }
}

int main(int argc, const char * argv[]) {
    while(1){
        cin >> w >> h;
        if(!w && !h) break;
        for(int y = 0; y < h; y++){
            for(int x = 0; x < w; x++){
                cin >> state[x][y];
            }
        }
        
        int cnt = 0;
        for(int y = 0; y < h; y++){
            for(int x = 0; x < w; x++){
                if(state[x][y]){
                    dfs(x, y);
                    cnt++;
                }
            }
        }
        cout << cnt << endl;
    }
}

C++: #include <cstdio>
#include <stack>
#include <utility>

using namespace std;

typedef pair<int, int> point;

int w, h;
int c[50][50];
const int d[8][2] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};

int count()
{
    stack<point> s;
    bool v[50][50] = {};
    int n = 0;

    for (int y = 0; y < h; ++y) {
        for (int x = 0; x < w; ++x) {
            if (v[x][y] || c[x][y] == 0)
                continue;

            s.push(point(x, y));
            v[x][y] = true;
            ++n;
            while (!s.empty()) {
                int x = s.top().first, y = s.top().second;
                s.pop();

                for (int i = 0; i < 8; ++i) {
                    int xx = x + d[i][0], yy = y + d[i][1];
                    if (0 <= xx && xx < w && 0 <= yy && yy < h
                            && !v[xx][yy] && c[xx][yy] == 1) {
                        s.push(point(xx, yy));
                        v[xx][yy] = true;
                    }
                }
            }
        }
    }

    return n;
}

int main()
{
    for (;;) {
        scanf("%d %d", &w, &h);
        if (w == 0 && h == 0)
            break;

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

        printf("%d\n", count());
    }

    return 0;
}

C++: #include <bits/stdc++.h>
#include <string>
#include <vector>
#define rep(i,n) for (int i = 0;i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;


//AOJ How many islands?
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp

int dx[8] = {1,-1,0,0,1,1,-1,-1};
int dy[8] = {0,0,1,-1,1,-1,1,-1};
int h,w;
using Graph = vector<vector<int>>;
vector<vector<bool>> seen;
vector<vector<int>> maze;

void dfs(P p){
    seen[p.first][p.second] = true;

    for(int i = 0;i < 8;i++){
        int nx = p.second + dx[i];
        int ny = p.first + dy[i];
        if(nx < 0 || ny < 0 || h <= ny || w <= nx) continue;
        if(seen[ny][nx]) continue;
        if(maze[ny][nx] == 0) continue;

        dfs(make_pair(ny,nx));
    }
}

void recur(int w,int h) {
    int count = 0;
    maze.assign(h,vector<int>(w,0));
    seen.assign(h,vector<bool>(w,false));
    for(int i = 0;i < h;i++){
        for(int j = 0;j < w;j++){
            cin >> maze[i][j];
        }
    }

    for(int i = 0;i < h;i++){
        for(int j = 0;j < w;j++){
            if(maze[i][j]==0) continue;
            if(seen[i][j]) continue;
            dfs(make_pair(i,j));
            count++;
        }
    }
    
    cout << count << endl;
    w = 0;
    h = 0;
}

int main(){
    while(true){
        cin >> w >> h;
        if(w*w + h*h == 0) break;
        recur(w,h);
    }
    return 0;
}



// #include <bits/stdc++.h>
// #include <string>
// #include <vector>
// #define rep(i,n) for (int i = 0;i < (n); ++i)
// using namespace std;
// using ll = long long;
// using P = pair<int,int>;


// //AOJ How many islands?
// //http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp

// int dx[8] = {1,-1,0,0,1,1,-1,-1};
// int dy[8] = {0,0,1,-1,1,-1,1,-1};
// int h,w;
// using Graph = vector<vector<int>>;
// vector<vector<bool>> seen;
// vector<vector<int>> maze;

// void dfs(P p){
//     seen[p.first][p.second] = true;

//     for(int i = 0;i < 8;i++){
//         int nx = p.second + dx[i];
//         int ny = p.first + dy[i];
//         if(nx < 0 || ny < 0 || h <= ny || w <= nx) continue;
//         if(seen[ny][nx]) continue;
//         if(maze[ny][nx] == 0) continue;

//         dfs(make_pair(ny,nx));
//     }
// }

// int main() {
//     cin >> w >> h;
//     int count = 0;
//     maze.assign(h,vector<int>(w,0));
//     seen.assign(h,vector<bool>(w,false));
//     for(int i = 0;i < h;i++){
//         for(int j = 0;j < w;j++){
//             cin >> maze[i][j];
//         }
//     }

//     for(int i = 0;i < h;i++){
//         for(int j = 0;j < w;j++){
//             if(maze[i][j]==0) continue;
//             if(seen[i][j]) continue;
//             dfs(make_pair(i,j));
//             count++;
//         }
//     }
    
//     cout << count << endl;
// }

C++: #include <bits/stdc++.h>
#define rep(i,a,b) for (int64_t i = (a); i < (b); i++)
#define REP(i,n) rep(i,0,n)
#define ALL(a) begin(a),end(a)
#define sz(s) (s).size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
int dx[]={1,-1,0,0,1,-1,1,-1};
int dy[]={0,0,1,-1,1,-1,-1,1};

int w,h,ans=0;
bool c[55][55];

void bfs(int y,int x)
{
    c[y][x]=0;
    REP(i,8) 
    {
        int ny=y+dy[i];int nx=x+dx[i];
        if(c[ny][nx]&&ny>=0&&ny<h&&nx>=0&&nx<w)bfs(ny,nx);
    }
}

int main()
{

    while(cin>>w>>h,w>0)
    {
        REP(i,h)REP(j,w)cin>>c[i][j];
        ans=0;c[55][55]={};
        REP(i,h)REP(j,w)if(c[i][j]){bfs(i,j);ans++;}
        cout<<ans<<endl;
    }
}