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 <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <sstream>

#define ll long long int
#define rep(i,x,y) for(int i=x;i<y;i++)
#define rel(i,x,y) for(int i=x-1;i>=y;i--)
#define all(x) x.begin(),x.end()
#define NMAX 50

using namespace std;
bool MAP[NMAX][NMAX];
bool alr[NMAX][NMAX];

int dx[3] = {-1,0,1};
int dy[3] = {-1,0,1};
int w,h;

void DFS(int y,int x){
	alr[y][x] = true;
	rep(i,0,9){
		if(0<=y+dy[i/3] && y+dy[i/3]<h && 0<=x+dx[i%3] && x+dx[i%3]<w && MAP[y+dy[i/3]][x+dx[i%3]] && !alr[y+dy[i/3]][x+dx[i%3]]){
			DFS(y+dy[i/3],x+dx[i%3]);
		}
	}
}

int main(){
	while(true){
	int count=0;
	cin >> w >> h;
	if(w == 0 && h == 0){ break; }
	rep(i,0,h){
		rep(j,0,w){
			alr[i][j] = false;
			cin >> MAP[i][j];
		}
	}
	rep(i,0,h){
		rep(j,0,w){
			if(MAP[i][j] && !alr[i][j]){
				DFS(i,j);
				count++;
			}
		}
	}
	cout << count << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,n1,n2) for(int i=n1;i<n2;i++)
#define speed_up    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

typedef long long int ll;
typedef pair<ll,ll> Pi;
const int INF=(ll)(1LL<<31)-1;
const ll INFl=(ll)9223372036854775807;
const int MAX=10000;
const ll MOD=(ll)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a, b)*b;}
//int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

int w,h;
int ls[55][55];
int alr[55][55];

void dfs(int a,int b){
	alr[a][b]=1;
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			if(i!=0||j!=0){
				if(a+i>=0&&a+i<h&&b+j>=0&&b+j<w){
					if(alr[a+i][b+j]==0&&ls[a+i][b+j]==1){
						dfs(a+i,b+j);
					}
				}
			}
		}
	}
	return;
}

int main(){
	while(1){
		cin>>w>>h;
		if(w==0&&h==0)break;
		for(int i=0;i<55;i++){
			for(int j=0;j<55;j++){
				alr[i][j]=0;
			}
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin>>ls[i][j];
			}
		}
		int ans=0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(alr[i][j]==0&&ls[i][j]==1){
					ans++;
					dfs(i,j);
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(),v.end()
#define _GLIBCXX_DEBUG

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vd = vector<double>;
using vvi = vector<vi>;
using vvll = vector<vll>;
using vvd = vector<vd>;
using vvvi = vector<vvi>;
using vvvll = vector<vvll>;
using vvvd = vector<vvd>;

const double pi=acos(-1);
const ll MOD=1e9+7;
const ll INF=(1LL<<60);

int w,h;
int graph[51][51];

void dfs(int x,int y,int x1,int y1){
    graph[x][y]=0;
    vi move={-1,0,1};
    for (int i:move){
        for (int j:move){
            if (i==0 && j==0)continue;
            else if (x+i<=0 || x+i>h || y+j<=0 || y+j>w)continue;
            else if (x+i==x1 && y+j==y1)continue;
            else if (graph[x+i][y+j]==0)continue;
            else dfs(x+i,y+j,x,y);
        }
    }
}

int main() {
    while (1){
        cin>>w>>h;
        if (w==0 && h==0)return 0;
        memset(graph,0,sizeof(graph));
        rep(i,h){
            rep(j,w){
                cin>>graph[i+1][j+1];
            }
        }
        int ans=0;
        for (int i=1;i<h+1;i++){
            for (int j=1;j<w+1;j++){
                if (graph[i][j]){
                    ans+=1;
                    dfs(i,j,0,0);
                }
            }
        }
        cout<<ans<<endl;
    }
    

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0; i<(n); i++)
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

void dfs(int i, int j, vector<vector<int>> &map){
    if(map.at(i).at(j)==0) return;
    map.at(i).at(j)=0;
    vector<int> x={-1,0,1,-1,1,-1,0,1},y={-1,-1,-1,0,0,1,1,1};
    rep(k,8){
        dfs(i+x.at(k),j+y.at(k),map);
    }
}

int main(){
    while(true){
        int w,h; cin>>w>>h;
        int res=0;
        if(w==0 && h==0) break;
        vector<vector<int>> map(h+2,vector<int>(w+2,0));
        rep(i,h){
            rep(j,w){
                cin>>map.at(i+1).at(j+1);
            }
        }
        rep(i,h+2){
            rep(j,w+2){
                if(map.at(i).at(j)==1){
                    dfs(i,j,map);
                    res++;
                }
            }
        }
        cout<<res<<endl;
    }
}