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>
#define rep(i,N) for(int i=0;i<(N);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
typedef long long ll;
typedef pair<int,int> P;
typedef set<int> S;
ll n;
int maze[55][55];
int attac[55][55];
int dx[] = {1,-1,0,0,1,-1,1,-1};
int dy[] = {0,0,1,-1,1,1,-1,-1};
int w,h;
void bfs(int i,int j){
    queue<P> que;
    que.push({i,j});
    //cout<<"111"<<endl;
    while(!que.empty()){
        P now;
        now = que.front();
        que.pop();
        int x = now.first;
        int y = now.second;
        rep(k,8){
            //cout<<"aaa"<<endl;
            int nx = x + dx[k];
            int ny = y + dy[k];
            if(nx<h && nx>=0 && ny<w && ny>=0 && attac[nx][ny]==0 && maze[nx][ny]==1){
                //cout<<"ああ"<<endl;
                attac[nx][ny]=1;
                que.push({nx,ny});
            }
        }
    }
}
int main(){
    while (1){
        int ans=0;
        cin>>w>>h;
        if(w==0 && h==0) break;
        rep(i,55){
            rep(j,55) attac[i][j]=0;
        }
        rep(i,h){
            rep(j,w) cin>>maze[i][j];
        }
        rep(i,h){
            rep(j,w){
                if(maze[i][j]==1 && attac[i][j]==0){
                    ans++;
                    bfs(i,j);
                }
            }
        }
        cout<<ans<<endl;
    }
    
    return 0;
}

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