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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<eps)
int w,h;
int f[55][55];
int dx[8]={0,0,1,1,1,-1,-1,-1};
int dy[8]={1,-1,1,0,-1,1,-1,0};
void dfs(int x,int y)
{
	f[x][y]=0;
	for(int i=0;i<8;i++)
	{
		int nx=x+dx[i],ny=y+dy[i];
		if(f[nx][ny]==1)dfs(nx,ny);
	}
}
void print()
{
	for(int i=1;i<=h;i++)
	{
		for(int j=1;j<=w;j++)
		{
			cout << f[i][j] << ' ';
		}
		cout << endl;
	}
}
int main()
{
	while(1)
	{
		int ans=0;
		memset(f,0,sizeof(f));
		cin >> w >> h;
		if(w==0&&h==0)break;
		for(int i=1;i<=h;i++)
		{
			for(int j=1;j<=w;j++)
			{
				cin >> f[i][j];
			}
		}
		for(int i=1;i<=h;i++)
		{
			for(int j=1;j<=w;j++)
			{
				if(f[i][j]==1)
				{
					ans++;
					dfs(i,j);
					//print();
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;


vector<int> dx{-1,0,1};
vector<int> dy{-1,0,1};

vector<vector<int>> G;
int w,h;

void dfs(vector<vector<int>> &G, int x, int y){
    if (G[y][x] == 1){
        G[y][x] = 0;

        REP(i,3){
            REP(j,3){
                int next_x = x+dx[i];
                int next_y = y+dy[j];
                if (i==1 && j==1)continue;
                if (0<=next_x && next_x<w && 0<=next_y && next_y<h){
                    dfs(G, next_x, next_y);
                }
            }
        }
    }
}

int main() {
    while(1){
        
        cin >> w >> h;
        if(w==0 && h==0) break;
        G.assign(h, vector<int>(w, 0));
        REP(i,h){
            REP(j,w){
                cin >> G[i][j];
            }
        }

        int count = 0;
        REP(i,h){
            REP(j,w){
                if (G[i][j] == 1){
                    count ++;
                    dfs(G, j, i);//G,w,h
                
                }
            }
        }
        cout << count << endl;
    }
    return 0;
}

C++: // テ・ツ淞コテヲツ慊ャテ」ツδ?」ツδウテ」ツδ療」ツδャテ」ツδシテ」ツδ?(テァツクツョテ・ツーツ湘ァツ可?

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define int long long
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
typedef pair<int, int> pii;
typedef long long ll;
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

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

int W, H;
int board[60][60];
bool used[60][60];

signed main() {
    while(cin >> W >> H, W || H) {
        memset(used, false, sizeof(used));
        rep(i,0,H) rep(j,0,W) cin >> board[i][j];

        int ans = 0;
        rep(i,0,H) rep(j,0,W) {
            if(used[i][j] || board[i][j] == 0) continue;
            ans++; used[i][j] = true;

            queue<pii> q;
            q.push(pii(i, j));

            while(!q.empty()) {
                pii t = q.front(); q.pop();
                int x = t.first, y = t.second;
                rep(k,0,8) {
                    int nx = x + dx[k], ny = y + dy[k];
                    if(nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
                    if(used[nx][ny] || board[nx][ny] == 0) continue;
                    used[nx][ny] = true;
                    q.push(pii(nx, ny));
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

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

using namespace std;

#define llong long long 
int w,h;
int sum;
int area[50][50];
int nw[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int nh[8] = {1, 1, 0, -1, -1, -1, 0, 1};

void solve(int h, int w){
	if(area[h][w] == 0)return;
	area[h][w] == 0;
	for(int i = 0; i < 8; i++){
		int sh = h + nh[i];
		int sw = w + nw[i];
		if(sh < 0 || sw < 0 || sh >= h || sw >= w) continue;
		solve(sh, sw);
	}
	return;
}

int main(){

	while(cin >> w >> h, w){
		sum = 0;
		fill_n(*area, 50 * 50, 0);
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin >> area[i][j];
			}
		}	

		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				if(area[i][j] == 1){
					sum++;
					solve(i, j);
				}
			}
		}

		cout << sum << endl;
}
	return 0;
}