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 <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <cstring>
#include <cctype>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <complex>
using namespace std;
#define REP(i,n) for(int i = 0; i < (int)n; i++)
#define FOR(i,a,b) for(int i = a; i < (int)b; i++)
const int INF = 1<<28;

int m[50][50];
int w, h;
int cnt;

void dfs(int x, int y, int z) {
	if(m[y][x]) {
		if(!z)
			cnt++;
		m[y][x] = false;
		if(y > 0 && m[y-1][x])
			dfs(x, y-1, 1);
		if(y > 0 && x > 0 && m[y-1][x-1])
			dfs(x-1, y-1, 1);
		if(x > 0 && m[y][x-1])
			dfs(x-1, y, 1);
		if(x > 0 && y < h-1 && m[y+1][x-1])
			dfs(x-1, y+1, 1);
		if(x < w-1 && m[y][x+1])
			dfs(x+1, y, 1);
		if(x < w-1 && 0 < y && m[y-1][x+1])
			dfs(x+1, y-1, 1);
		if(y < h-1 && m[y+1][x])
			dfs(x, y+1, 1);
		if(y < h-1 && x < w-1 && m[y+1][x+1])
			dfs(x+1, y+1, 1);
	}
	return;
}

int main() {
	while(cin >> w >> h, w|h) {
		REP(i, h) REP(j, w)
			cin >> m[i][j];

		cnt = 0;
		REP(i, h) REP(j, w)
			dfs(j, i, 0);

		cout << cnt << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for(int (i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int (i) = 1; (i) <= (n); (i++))
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define pb push_back
#define eb emplace_back
#define yn {puts("Yes");}else {puts("No");}
#define printd(x) printf("%.12f\n", ans);
template<class T> bool chmin(T& a, const T& b) {if (a > b) {a = b; return 1;} return 0;}
template<class T> bool chmax(T& a, const T& b) {if (a < b) {a = b; return 1;} return 0;}
using namespace std;
using ll = long long;
using P = pair<int, int>;

int w, h;
vector<int> ans;
vector<vector<bool>> visited(55, vector<bool>(55));
vector<vector<int>> grid(55, vector<int>(55));
int dx[] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};

void _init() {
	rep (i, 51) rep (j, 51) visited[i][j] = false;
}

int dfs(int sy, int sx) {
	visited[sy][sx] = true;
	rep (k, 8) {
		int x = sx + dx[k];
		int y = sy + dy[k];
		if (0 <= x && x < w && 0 <= y && y < h && grid[y][x] && !visited[y][x]) dfs(y, x);
	}

	return 1;
}

int main(void) {
	while (1) {
		cin >> w >> h;
		if (w == 0 || h == 0) break;

		_init();

		rep (i, h) rep (j, w) cin >> grid[i][j];

		int res = 0;
		rep (i, h) rep (j, w) if (!visited[i][j] && grid[i][j]) res += dfs(i, j);
		ans.pb(res);
	}

	rep (i, ans.size()) cout << ans[i] << endl;
	return 0;
}

C++: #include <cstdio>
using namespace std;

int w, h;
int c[50][50];

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

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];
        if(0 <= nx && nx < h && 0 <= ny && ny < w && c[nx][ny] == 1){
            dfs(nx, ny);
        }
    }
}

int main(){
    while(scanf("%d%d", &w, &h) != EOF && (w||h)){
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                scanf("%d", &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);
                }
            }

        }
        printf("%d\n", ans);
    }
    return 0;
}

C++: /*
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp
*/

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const ll MOD = 1000000007;

const int di[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dj[8] = {-1, 0, 1, -1, 1, -1, 0, 1};

int w, h;
int c[50][50];

int solve(){
    int cnt = 0;
    bool visited[h][w];
    memset(visited, false, sizeof visited);
    queue<pair<int, int> > Q;

    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(!visited[i][j] && c[i][j] == 1){
                cnt++;
                // bfs
                visited[i][j] = true;
                Q.push(make_pair(i, j));
                while(!Q.empty()){
                    pair<int, int> p = Q.front(); Q.pop();
                    int si = p.first;
                    int sj = p.second;

                    for(int k=0;k<8;k++){
                        int ni = si + di[k];
                        int nj = sj + dj[k];
                        if(ni < 0 || ni >= h || nj < 0 || nj >= w) continue;
                        if(!visited[ni][nj] && c[ni][nj] == 1){
                            visited[ni][nj] = true;
                            Q.push(make_pair(ni, nj));
                        }
                    }
                }
            }
        }
    }
    return cnt;
}

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    while(1){
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++) cin >> c[i][j];
        }
        int ans = solve();
        cout << ans << endl;
    }
}