Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

C++: #include "bits/stdc++.h"

using namespace std;
using ll = long long int;
using ullong = unsigned long long;
#define rep(i,n) for(int i = 0; i < n; i++)
#define FOR(i, a, b)  for(int i = (a); i < (b) ; i++)
#define pb push_back
#define SORT(v,n) sort(v, v+n)
#define ALL(x) (x).begin(),(x).end()
#define debug(x) cerr << #x << ": " << x << '\n'
#define elif else if
//#define llint

#ifdef llint
#define int ll
static const int INF = numeric_limits<long long>::max();
#else
static const int INF = numeric_limits<int>::max();
#endif // llint
static const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
int dx[8] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int dy[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };

int w, h;

void dfs(int x,int y,vector<vector<int>>& map) {
	map[y][x] = 0;
	//rep(y, h) {
	//	rep(x, w) {
	//		cout << map[y][x] << " ";
	//	}
	//	cout << endl;
	//}
	//cout << endl;
	//cout << "x : " << x << " y : " << y << endl;
	rep(i, 8) {
		int tx = x + dx[i];
		int ty = y + dy[i];
		//cout << "nx : " << tx << " ny : " << ty << endl;
		if (tx >= 0 and tx < map[0].size() and ty >= 0 and ty < map.size()) {
			if(map[ty][tx] == 1)
				dfs(tx, ty, map);
		}
	}
	return;
}

signed main() {
	ios::sync_with_stdio(true);
	while (true) {
		cin >> w >> h;
		if (w == 0 and h == 0) break;
		vector<vector<int>>  map(h);
		rep(i, h) {
			rep(j, w) {
				int x;
				cin >> x;
				map[i].pb(x);
			}
		}
		int ans = 0;
		rep(y,h){
			rep(x, w) {
				if (map[y][x] == 1) {
					dfs(x, y, map);
					ans++;
				}
			}
		}
		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++)
using ll = long long;
using pint = pair<int,int>;

int w, h;
int field[55][55];

void dfs(int sh, int sw){
    for(int dx = -1; dx <= 1; dx++){
        for(int dy = -1; dy <= 1; dy++){
            int nh = sh + dx, nw = sw + dy;
            if(nh < 0 || h <= nh || nw < 0 || w <= nw) continue;
            if(field[nh][nw] == 0) continue;
            field[nh][nw] = 0;
            dfs(nh, nw);
        }
    }
}

void solve(){
    int cnt = 0;
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(field[i][j] == 0) continue;
            field[i][j] = 0;
            dfs(i,j);
            cnt++;
        }
    }

    cout << cnt << endl;
}

int main(){
    while(cin >> w >> h){
        if(h == 0 || w == 0) break;
        rep(i,h)rep(j,w) cin >> field[i][j];
        solve();
    }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>

using namespace std;

int c[52][52];

void er(int i,int j){
    c[i][j]--;
    if(c[i+1][j-1])
        er(i+1,j-1);
    if(c[i][j+1])
        er(i,j+1);
    if(c[i+1][j])
        er(i+1,j);
    if(c[i+1][j+1])
        er(i+1,j+1);
    if(c[i-1][j+1])
        er(i-1,j+1);
    if(c[i-1][j])
        er(i-1,j);
    if(c[i-1][j-1])
        er(i-1,j-1);
    if(c[i][j-1])
        er(i,j-1);
}

int main(void){
    int w,h;
    int i,j;
    int a;
    while(true){
        cin >> w >> h;
        if(w == 0){
            break;
        }
        for(i = 0;i < 52;i++){
            for(j = 0;j < 52;j++){
                c[i][j] = 0;
            }
        }
        for(i = 1;i <= h;i++){
            for(j = 1;j <= w;j++){
                cin >> c[i][j];
            }
        }
        a = 0;
        for(i = 1;i <= h;i++){
            for(j = 1;j <= w;j++){
                if(c[i][j]){
                    er(i,j);
                    a++;
                }
            }
        }
        cout << a << endl;
    }
}

C++: #include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <complex>
#include <map>
#include <climits>
using namespace std;

#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair

static const double PI=6*asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vint;
static const int INF=1<<24;


int main(){
	int n,m;
	while(cin>>m>>n,n||m){
		int a[n][m];
		rep(i,n){
			rep(j,m){
				cin>>a[i][j];
				//cout<<a[i][j]<<" ";
			}
			//cout<<endl;
			
		}
		//cout<<endl;
		int ans=0;
		rep(i,n){
			rep(j,m){
				if(a[i][j]==1){
					a[i][j]=0;
					ans++;
					queue<pii> q;
					q.push(mkp(i,j));
					while(!q.empty()){
						int qs= q.size();
						rep(k,qs){
							pii tp=q.front();
							q.pop();
							int v[8][2]={0,1,0,-1,1,0,-1,0,1,1,1,-1,-1,1,-1,-1};
							rep(l,8){
								int x=tp.F+v[l][0],y=tp.S+v[l][1];
								if(x>=0&&y>=0&&x<n&&y<m){
									if(a[x][y]==1){
										q.push(mkp(x,y));
										a[x][y]=0;
									}
								}
							}
						}
					}/*
					
					rep(k,n){
						rep(l,m){
							cout<<a[k][l]<<" ";
						}
						cout<<endl;
					}
					cout<<endl;
					*/
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}