Go: package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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));
  }
}

C++: #include<iostream>

using namespace std;
int w, h;
int c[50][50];
int dx[]={0, 0, 1, 1, 1, -1, -1, -1};
int dy[]={1, -1, 0, 1, -1, 0, 1, -1};
int used[50][50];

void rec(int i, int j){
   used[i][j]=1;
   for(int k=0; k<8; k++){
      int ni=i+dy[k];
      int nj=j+dx[k];
      if(!(0<=ni&&ni<h&&0<=nj&&nj<w)) continue;
      if(c[ni][nj]&&!used[ni][nj]) rec(ni, nj);
   }
}

int main(){

   while(1){
      cin>> w>> h;
      if(!(w||h)) break;
      for(int i=0; i<h; i++){
         for(int j=0; j<w; j++){
            cin>> c[i][j];
         }
      }

      fill((int*)used, (int*)used+50*50, 0);
      int cnt=0;
      for(int i=0; i<h; i++){
         for(int j=0; j<w; j++){
            if(c[i][j]&&!used[i][j]){
               cnt++;
               rec(i, j);
            }
         }
      }

      cout<< cnt<< endl;
   }

   return 0;
}