Go: package main

import (
	"fmt"
)

func main() {
	var h, w int
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			return
		}
		matrix := make([][]int, h)
		Sean := make([][]bool, h)
		for i := 0; i < h; i++ {
			matrix[i] = make([]int, w)
			Sean[i] = make([]bool, w)
			for j := 0; j < w; j++ {
				fmt.Scan(&matrix[i][j])
			}
		}
		var count int
		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				if matrix[i][j] == 1 && Sean[i][j] == false {
					search(i, j, h, w, matrix, Sean)
					count++
				}
			}
		}
		fmt.Println(count)
	}
}
func search(x int, y int, h int, w int, matrix [][]int, Sean [][]bool) {
	if x < 0 || y < 0 || x >= h || y >= w || matrix[x][y] == 0 {
		return
	}
	if Sean[x][y] == true {
		return
	}

	Sean[x][y] = true

	search(x+1, y, h, w, matrix, Sean)
	search(x-1, y, h, w, matrix, Sean)
	search(x, y+1, h, w, matrix, Sean)
	search(x, y-1, h, w, matrix, Sean)
	search(x+1, y-1, h, w, matrix, Sean)
	search(x-1, y+1, h, w, matrix, Sean)
	search(x+1, y+1, h, w, matrix, Sean)
	search(x-1, y-1, h, w, matrix, Sean)
}

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;
    }
}