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++: #pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace::std;
#define rep(i, n) for(lint i = 0; i < (lint)(n); i++)
#define repi(i,a,b) for(lint i=lint(a);i<lint(b);i++)
typedef long long lint;
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
template<typename T=lint> T in(){
    return *istream_iterator<T>(cin);
}

vector<lint> dx={-1,1,0,0,1,1,-1,-1};
vector<lint> dy={0,0,-1,1,1,-1,1,-1};
int main(){
    while(1){
        lint w,h;
        cin>>w>>h;
        if(w==0&&h==0)break;
        vector<vector<lint>>v(h,vector<lint>(w));
        rep(i,h)rep(j,w){
            cin>>v[i][j];
        }
        lint ans=0;
        rep(i,h)rep(j,w){
            if(v[i][j]==0)continue;
            ans++;
            stack<pair<lint,lint>>s;
            s.emplace(i,j);
            while(!s.empty()){
                lint r,c;
                tie(r,c)=s.top();
                s.pop();
                rep(k,8){
                    if(r+dy[k]<0||h<=r+dy[k]||c+dx[k]<0||w<=c+dx[k])continue;
                    if(v[r+dy[k]][c+dx[k]]==1){
                        v[r+dy[k]][c+dx[k]]=0;
                        s.emplace(r+dy[k],c+dx[k]);
                    }
                }
            }
        }
        cout<<ans<<endl;
    }
}

C++: 
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#define unsigned long long 
#define rep(i,a,b) for(ull (i)=(a);(i)<(b);i++)
void dfs(int w, int h);
using namespace std;
int field[50][50];
int x, y;
int cnt;
int main(void){
	while (true){
		cin >> x >> y;
		if (x == 0)
			break;
		cnt = 0;
		for (int i = 0; i < y; i++){
			for (int j = 0; j < x; j++){
				cin >> field[i][j];
			}
		}
		for (int i = 0; i < y; i++){
			for (int j = 0; j < x; j++){
				if (field[i][j] == 1){
					cnt++;
					dfs(i, j);
				}
			}
		}
		cout << cnt << endl;
	}
	return 0;
}


void dfs(int h, int w){
	field[h][w] = 0;

	if (w < x - 1){
		if (field[h][w + 1] == 1){
			dfs(h, w + 1);
		}
	}
	if (w < x - 1&&h<y-1){
		if (field[h+1][w + 1] == 1){
			dfs(h+1 ,w + 1);
		}
	}
	if (h<y - 1){
		if (field[h+ 1][w ] == 1){
			dfs(h +1 ,w );
		}
	}
	if (h<y - 1&&w>0){
		if (field[h + 1][w-1] == 1){
			dfs(h + 1, w-1);
		}
	}
	if (w>0){
		if (field[h ][w - 1] == 1){
			dfs(h , w - 1);
		}
	}
	if (w>0&&h>0){
		if (field[h-1][w - 1] == 1){
			dfs(h-1, w - 1);
		}
	}
	if (h>0){
		if (field[h - 1][w] == 1){
			dfs(h - 1, w);
		}
	}
	if (h>0&&w<x-1){
		if (field[h - 1][w+1] == 1){
			dfs(h - 1, w+1);
		}
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

vector<int> dx = {1, 1, 1, -1, -1, -1, 0, 0}, dy = {1, -1, 0, 1, -1, 0, 1, -1};
int w, h;
vector<vector<bool>> field;

void dfs(int x, int y)
{
  field.at(y).at(x) = false;
  rep(direction, 8)
  {
    int nx = x + dx.at(direction), ny = y + dy.at(direction);
    if (0 <= nx && nx < w && 0 <= ny && ny < h && field.at(ny).at(nx))
    {
      dfs(nx, ny);
    }
  }
}

int main()
{
  while (1)
  {
    cin >> w >> h;
    if (!w && !h)
    {
      break;
    }
    field.resize(h, vector<bool>(w));
    field.assign(h, vector<bool>(w, false));
    rep(y, h)
    {
      rep(x, w)
      {
        int c;
        cin >> c;
        if (c)
        {
          field.at(y).at(x) = true;
        }
      }
    }
    int ans = 0;
    rep(y, h)
    {
      rep(x, w)
      {
        if (field.at(y).at(x))
        {
          dfs(x, y);
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
}

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

using namespace std;

// Repetitions
#define FOR(i,a,n) for(ll i = ((ll) a); i < ((ll) n); ++i)
#define FORR(i, s, g) for(ll i = ((ll)s - 1); i >= ((ll) g); --i)
#define REP(i, n) FOR(i, 0, n)
#define REPR(i, n) FORR(i, n, 0)

// Container Utils
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define SORTALL(v) sort(ALL(v))
#define SZ(a) int((a).size())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())

// For Debug
#define DMP(x)  cerr << #x << " = " << (x) << endl
#define DBG(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl

// Aliases
#define PB push_back
#define MP make_pair
typedef long long ll;
typedef string str;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

// I/O Asynchronizing
struct aaa{
    aaa(){
        cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20);
    };
}aaaaaaa;

#define MAX_H 50
#define MAX_W 50

int h, w;
bool is_land[MAX_H][MAX_W] = { false };

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

inline bool is_in_range(const int y, const int x) {
    return 0 <= y && y < h && 0 <= x && x < w;
}

void dfs(const int y, const int x) {
    is_land[y][x] = false;
    REP(i, 8) {
        int nx = x + dx[i], ny = y + dy[i];
        if(is_in_range(ny, nx) && is_land[ny][nx]) dfs(ny, nx);
    }
}

int main() {
    while(true) {
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        REP(j, h) {
            REP(i, w) {
                int c;
                cin >> c;
                is_land[j][i] = (c == 1);
            }
        }
        int ans = 0;
        REP(j, h) {
            REP(i, w) {
                if(is_land[j][i]) {
                    dfs(j, i);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }

    return 0;
}