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 <cctype>
#include <utility>
#include <vector>
using namespace std;

#define gcu getchar_unlocked
#define ii inline int in
ii(int c) {
	int n = 0;
	bool minus = false; if (c == '-') minus = true, c = gcu();
	do {n = 10 * n + (c - '0'), c = gcu();} while (c >= '0');
	//return n; }
	return minus ? -n : n; }
ii() {return in(gcu());}
inline bool scan(int &n) {int c=gcu();return c==EOF?false:(n=in(c),true);}
//void scan(string &s) {for (char c; !isspace(c = gcu()); s += c);}
#undef ii
#define pcu putchar_unlocked
template <typename T>
void out(T n) {
	static char buf[20];
	char *p = buf;
	if(n < 0) pcu('-'), n *= -1;
	if (!n) *p++ = '0';
	else while (n) *p++ = n % 10 + '0', n /= 10;
	while (p != buf) pcu(*--p); }
void out(const char *s){while(*s)pcu(*s++);}
void out(char c){pcu(c);}
//void out(string &s){for (char c: s) pcu(c);}
template <typename head, typename... tail>
void out(head&& h, tail&&... t){out(h);out(move(t)...);}
template <typename T>
//void out(vector<T> &v){for(T &x:v)out(' '),out(x);out('\n');}
void out(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);out('\n');}
#undef svo

typedef vector<vector<int>> map;

bool walk(int x, int y, map &m) {
	static const int
		dx[] = {1, 1, 0, -1, -1, -1, 0, 1},
		dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
	if (x < 0 || x >= (int) m[0].size() ||
		y < 0 || y >= (int) m.size() ||
		m[y][x] == 0)
		return false;
	m[y][x] = 0;
	int i = 0;
	for (; i < 8; i++)
		walk(x + dx[i], y + dy[i], m);
	return true;
}

int main() {
	int W, H;
	while(scan(W) && scan(H) && (W || H)) {
		map m(H, vector<int>(W));
		for (auto &v: m)
			for (int &i :v)
				i = in();
		int c = 0;
		for (int i = 0; i < H; i++)
			for (int j = 0; j < W; j++)
				if (walk(j, i, m))
					c++;
		//for (auto v : m)
		//	out(v);
		//out('\n');
		out(c, '\n');
	}
}

C++: #include <iostream>
#include <iomanip>
#include <sstream>
#include <stdio.h>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#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)
#define INF 114514810
#define ll long long
//#define scanf scanf_s

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

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

void dfs(int i, int j)
{
	if (isl[i][j] == 1)
	{
		isl[i][j] = 0;
		REP(k, 8) dfs(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++;
				dfs(i, j);
			}
		}
		cout << cnt << endl;
	}
	return 0;
}

C++: // AOJ1160
#include <bits/stdc++.h>
#define endl "\n"
#define rep(i,n) for(int i=0;i<int(n);i++)
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ipair;
template <class T>ostream &operator<<(ostream &o,const vector<T>&v)
{o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}
namespace you {
    std::string to_string(char val) {
        return std::string(1, val);
        // return std::string{val};
        // you::to_string(char)
    }
}
int w,h;
vector<vector<int>> ma(60,vector<int>(60));
int ans;
void dfs(int y,int x){
    if(y<0||h<=y||x<0||w<=x) return;
    if(ma[y][x]==0) return;
    ma[y][x]=0; //陸を海にしちゃう
    for(int i=-1;i<=1;i++){
        for(int j=-1;j<=1;j++){
            dfs(y+i,x+j);
        }
    }
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(12);

    while(cin>>w>>h){
        ans=0;
        if(w==0 && h==0) break;
        rep(y,h){
            rep(x,w){
                cin>>ma[y][x];
            }
        }
        rep(y,h){
            rep(x,w){
                if(ma[y][x]==0) continue;
                dfs(y,x);
                ans++;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <vector>
#include <utility>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>
#include <cstring>
 
typedef long long ll;

#define rep(i,n) for(ll i = 0; i < n; i++)
 
using namespace std;
 
const ll INF = ll(1e18)+1;
const double PI=3.14159265358979323846;

int w,h,C[100][100], dx[8]={-1,1,0,0,-1,-1,1,1}, dy[8]={0,0,1,-1,-1,1,-1,1};
bool seen[100][100];

void dfs(int x, int y){
  seen[x][y] = true;
  rep(i,8){
    int movex = x+dx[i], movey = y+dy[i];
    if((movex<0 && w<=movex) || (movey<0 && h<=movey) || \
       seen[movex][movey] || C[movex][movey] == 0) continue;
    dfs(movex, movey);
  }
}

int main(){
  while(1){
    cin >> w >> h;
    if(!w && !h) break;
    rep(i,h) rep(j,w){
      cin >> C[i][j];
    }
    int ans = 0;
    rep(i,h) rep(j,w){
      if(seen[i][j] || C[i][j] == 0) continue;
      else{
	dfs(i,j);
	ans++;
      }
    }
    cout << ans << endl;
    memset(C, 0, sizeof(C));
    memset(seen, false, sizeof(seen));
  }
}