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;
#define REP(i,n) for(int (i) = 0;(i) < (n) ; ++(i))
#define REPS(a,i,n) for(int (i) = (a) ; (i) < (n) ; ++(i))
#define REVERSE(i,n) for(int (i) = n-1;(i) >= 0 ; --i)
#define ALL(c) (c).begin() , (c).end()
#define LL long long
#define int LL
#define inf  (1LL << 60)
#define mod 1000000007
#define QUICK_CIN ios::sync_with_stdio(false); cin.tie(0);
#define lower lower_bound
#define upper upper_bound
#define ZERO(c,n) memset(&c[0],0,sizeof(int)*n)
#define ZERO2(c,n) memset(&c[0][0],0,sizeof(int)*n
#define pl(a) cout << ""#a": " << a << endl;
#ifdef _DEBUG
#define debug_io fstream cin("input.txt");ofstream cout("output.txt");
template<class T>fstream& operator>>(fstream& is, vector<T>& v)
{
	int n = v.size();
	REP(i, n) {
		is >> v[i];
	}
	return is;
}
#else
#define debug_io ;
#endif
#define debug_input debug_io
#define DOWN(T) std::greater<T>()
template<class T>istream& operator>>(istream& is, vector<T>& v)
{
	int n = v.size();
	REP(i, n) {
		is >> v[i];
	}
	return is;
}
using vs = vector<string>; using vi = vector<int>; using pii = pair<int, int>;
using psi = pair<string, int>; using vvi = vector<vi>; using pss = pair<string, string>;
using vpii = vector<pii>;
template<class T>bool valid(T x, T w) { return 0 <= x&&x < w; }
//int dx[4] = { 1, -1, 0, 0 }; int dy[4] = { 0, 0, 1, -1 };
//-----------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------

bool ed[55][55];
int dx[8] = { 0,0,1,-1,1,-1,1,-1 };
int dy[8] = { 1,-1,0,0,1,1,-1,-1 };

signed main()
{
	QUICK_CIN;
	debug_input;
	int w, h;
	while (cin >> w >> h, w + h) {
		vector<vi> tile(h,vi(w));
		REP(i, h) {
			REP(j, w) {
				cin >> tile[i][j];
			}
		}

		int ans = 0;
		REP(i, 55) {
			REP(j, 55) {
				ed[i][j] = false;
			}
		}

		REP(i, h) {
			REP(j, w) {
				if (tile[i][j] != 1)
					continue;
				queue<pii> q;
				q.push({ i,j });
				while (q.size()) {
					auto t = q.front();
					q.pop();
					auto y = t.first;
					auto x = t.second;
					tile[y][x] = 2;
					ed[y][x] = true;
					REP(k, 8) {
						auto nx = min(w-1,max(0LL,x + dx[k]));
						auto ny = min(h-1,max(0LL,y + dy[k]));
						if (!ed[ny][nx]&&tile[ny][nx]==1) {
							q.push({ ny,nx });
						}
					}
				}
				ans++;
			}
		}
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <iomanip>
#include <sstream>
#include <stdio.h>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <math.h>
#include <utility>
#include <string>
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <functional>

using namespace std;

#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)

int w, h;
int isl[55][55];
int di[] = { -1 - 1, -1, 0, 0, 1, 1, 1 }; int dj[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

void ume(int i, int j)
{
	if (i >= 0 && i < h&&j >= 0 && j < w && isl[i][j] == 1)
	{
		isl[i][j] = 0;
		REP(k, 8)
		{
			ume(i + di[k], j + dj[k]);
		}
	}

}

int main()
{
	while (cin >> w >> h, w)
	{
		REP(i, h)REP(j, w) cin >> isl[i][j];
		int cnt = 0;
		REP(i, h)REP(j, w)
		{
			if (isl[i][j] == 1)
			{
				cnt++;
				ume(i, j);

			}
		}
		cout << cnt << endl;
	}


	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <numeric>
#include <list>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <tuple>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define show(a, n) rep(i,n) {cout<<a[i]<<' ';} cout<<endl;

using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<long long, long long> pll;

const int INF = 1 << 30;
const long long INFL = 1LL << 62;
const int MOD = 1000000007;
const int MAX = 100000;
const int N = 100010;

void dfs(vector<vi>& c, vector<vi>& d, int i, int j, int cnt) {
	stack<pii> st;
	st.push(pii(i, j));
	int di[] = {0, 1, 1, 1, 0, -1, -1, -1};
	int dj[] = {1, 1, 0, -1, -1, -1, 0, 1};
	while(!st.empty()) {
		pii p = st.top();
		st.pop();
		int si = p.first;
		int sj = p.second;
		rep(k, 8) {
			if(c[si+di[k]][sj+dj[k]] == 0) continue;
			if(d[si+di[k]][sj+dj[k]] != 0) continue;
			d[si+di[k]][sj+dj[k]] = cnt;
			st.push(pii(si+di[k], sj+dj[k]));
		}
	}
}

int cnt(vector<vi>& c) {
	int h = c.size();
	int w = c[0].size();
	vector<vi> d(h, vi(w, 0));
	int cnt = 0;
	rep(i, h) rep(j, w) if(c[i][j] && d[i][j] == 0) dfs(c, d, i, j, ++cnt);
	return cnt;
}

int main() {
//	cout << fixed << setprecision(15);
	int w, h;
	vi ans;
	while(true) {
		cin >> w >> h;
		if(w == 0 && h == 0) break;
		vector<vi> c(h + 2, vi(w + 2, 0));
		rep(i, h) rep(j, w) cin >> c[i+1][j+1];
		ans.push_back(cnt(c));
	}
	for(auto i : ans) cout << i << endl;
}