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 <bits/stdc++.h>

using namespace std;

#define rep(i, n) REP((i), 0, (n))
#define REP(i, a, n) for(int i=(a); i<(n); i++)

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

void dfs(int i, int j) {
    if(c[i][j] == 0) return;
    
    c[i][j] = 0;
    dfs(i-1, j-1);
    dfs(i-1, j);
    dfs(i-1, j+1);
    dfs(i  , j-1);
    dfs(i  , j+1);
    dfs(i+1, j-1);
    dfs(i+1, j);
    dfs(i+1, j+1);
}

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

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
const int dx[8] = {0,1,1,1,0,-1,-1,-1};
const int dy[8] = {1,1,0,-1,-1,-1,0,1};
int w,h,grid[51][51];
void s(int x, int y){
  grid[y][x] = 0;
  REP(r,8){
    if(x+dx[r]>=0&&x+dx[r]<w&&y+dy[r]>=0&&y+dy[r]<h&&grid[y+dy[r]][x+dx[r]]){
      s(x+dx[r],y+dy[r]);
    }
  }
} 


int main(void){
  while(cin>>w>>h){
    if(w==0) break;
    REP(y,h)
      REP(x,w)
        cin>>grid[y][x];
    int c = 0;
    REP(y,h)
      REP(x,w)
        if(grid[y][x]){
          c++;
          s(x,y);
        }
    cout<<c<<endl;
  }
  return 0;
}

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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x,n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,P> PP;

//-----------------------------------------------------------------------------

int h,w;
int fld[50][50];
int dx[]={1,-1,0,0,1,1,-1,-1},dy[]={0,0,1,-1,-1,1,-1,1};

void bfs(int sy,int sx) {
	queue<P> q;
	fld[sy][sx]=0;
	q.push(P(sx,sy));
	while(!q.empty()) {
		P p=q.front();q.pop();
		REP(i,8) {
			int nx=p.fi+dx[i],ny=p.se+dy[i];
			if(nx<0||nx>=w||ny<0||ny>=h||fld[ny][nx]==0) continue;
			fld[ny][nx]=0;
			q.push(P(nx,ny));
		}
	}
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	while(true) {
		cin>>w>>h;
		if(w==0) break;
		REP(i,h) REP(j,w) cin>>fld[i][j];
		int cnt=0;
		REP(i,h) {
			REP(j,w) {
				if(fld[i][j]==1) {
					cnt++;
					bfs(i,j);
				}
			}
		}
		cout<<cnt<<endl;
	}

	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
#ifndef FAST_IO
 #define IOS ios_base::sync_with_stdio(false);cin.tie(nullptr);
#endif

#define int long long
#define FILE_IO freopen("input.txt","r",stdin)/#define int long long
#define f first
#define s second
#define endl '\n'
#define all(c) c.begin(),c.end()
#define bp __builtin_popcount
#define debug(x) cerr << #x << " = " << x << endl;

const int MOD = 1e9 + 7;
const int INF = 1e18;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>void print(T t){for (auto &it : t) cout << it << " ";}
inline int powerexp(int a, int b, int m = MOD){ int res = 1;while(b){if(b&1)res = res * a % m;a = a * a % m,b >>= 1;}return res % m;}
inline int modinv(int a){ return powerexp(a,MOD-2,MOD);}
inline int lcm(int a, int b){ int g = __gcd(a,b);return a/g*b;}
inline int add(int x, int y){ x += y; if (x >= MOD) x -=  MOD; return x;}
inline int mul(int x, int y){ return (x * 1ll * y) % MOD;}
inline int sub(int x, int y){ x -= y; if (x<0) x += MOD; return x ;}

//-------------------< start here >-------------------------// 

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

void solve(){
    int w, h;
    while (cin >> w >> h, w && h){
		int g[h][w];
		bool visited[h][w];
		for (int i = 0; i < h; i++){
		  for (int j = 0; j < w; j++){
			  cin >> g[i][j]; 
			}	
		}
	  memset(visited, false, sizeof visited);
	  int ret = 0;
	  for (int i = 0; i < h; i++){
		for (int j = 0; j < w; j++){
			if (g[i][j] == 1 && !visited[i][j]){
			  ret++;
			  queue< pair<int, int> > q;
			  q.push({i, j});
			  while (!q.empty()){
				 auto p = q.front();
				 q.pop();
				 int ny = p.f, nx = p.s;
				 for (int k = 0; k < 8; k++){
					 int X = nx + dx[k];
					 int Y = ny + dy[k];
					 if (X >= 0 && Y >= 0 && X < w && Y < h){
					  if (g[Y][X] == 1 && !visited[Y][X]){
						   visited[Y][X] = true;
						   q.push({Y, X}); 
						 }	 
					  } 
				   }  
				}	
			 }
		  } 
		}
	 cout << ret << endl;	
	}
}

//-----------------------------
//comment from here for TOPCODER 
int32_t main(){
   IOS;
   solve();
   //find();
   #ifdef LOCAL_DEFINE
   cerr << "time elapsed " << (double)clock()/CLOCKS_PER_SEC << endl;
   #endif
}