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<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int w,h;
char field[55][55];
int dx[]={1,-1,0,0,-1,-1,1,1};
int dy[]={0,0,1,-1,1,-1,1,-1};

int used[55][55];
void dfs(int y,int x){
  if(used[y][x]==1)return ;
  used[y][x]=1;
  for(int i=0;i<8;i++){
    int ny=y+dy[i];
    int nx=x+dx[i];
    if(0<=ny&&ny<h&&0<=nx&&nx<w&&field[ny][nx]=='1'){
      dfs(ny,nx);
    }
  }
  return ;
}
int main(){
  while(1){
    cin >>w>>h;
    if(w==0&&h==0) break;
    for(int i=0;i<55;i++){
      for(int j=0;j<55;j++){
        used[i][j]=0;
      }
    }
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        cin >>field[i][j];
      }
    }
    int cnt=0;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        if(used[i][j]==1)continue;
        if(field[i][j]=='1'){
          cnt++;
          dfs(i,j);
        }
      }
    }
    cout << cnt << endl;
  }
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

#define MAX 51

bool field[MAX][MAX];
int cnt;
int H, W;

void dfs(int x, int y){
  field[y][x] = 0;
  
  for(int dx = -1 ; dx <= 1 ; dx++){
    for(int dy = -1 ; dy <= 1 ; dy++){
      int nx = x + dx;
      int ny = y + dy;
      if(0 <= nx && nx < W && 0 <= ny && ny < H && field[ny][nx] == 1) dfs(nx, ny);
    }
  }
}

int main(){
  while(cin >> W >> H){
    cnt = 0;
    if(W == 0 && H == 0) break;
    for(int i = 0 ; i < H ; i++){
      for(int j = 0 ; j < W ; j++){
	cin >> field[i][j];
      }
    }

    for(int i = 0 ; i < H ; i++){
      for(int j = 0 ; j < W ; j++){
	if(field[i][j] == 1){
	  dfs(i, j);
	  cnt++;
	}
      }
    }
    cout << cnt << endl;
  }
  return 0;
}

C++: /* {{{ Shinobu kawaii */
/*
  ______   __        _                  __                 
.' ____ \ [  |      (_)                [  |                
| (___ \_| | |--.   __   _ .--.   .--.  | |.--.   __   _   
 _.____`.  | .-. | [  | [ `.-. |/ .'`\ \| '/'`\ \[  | | |  
| \____) | | | | |  | |  | | | || \__. ||  \__/ | | \_/ |, 
 \______.'[___]|__][___][___||__]'.__.'[__;.__.'  '.__.'_/ 
                                                           
*/
/* }}} */

// #define int long long

/* cpp template {{{ */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <functional>
using namespace std;

/* short */
#define pb push_back
#define mp make_pair
#define print(x) cout << x << '\n'
#define Fi first
#define Se second

/* REPmacro */
#define REPS(i, a, n) for (int i = (a); i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = 1; i <= (n); i++)
#define DEPS(i, a, n) for (int i = (a); i >= n; i--)
#define DEP(i, n) for (int i = (n); i >= 0; i--)

/* typedef */
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vector<int> > vii;
typedef pair<int, int> pii;

/* const */
const int INF = 999999999;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

/* func */
inline bool is_uruu(int y) {return (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0));}
inline bool inside(int y, int x, int H, int W) {return (y >= 0 && x >= 0 && y < H && x < W);}

/* }}} */

bool field[56][56];
int w, h;

void dfs(int y, int x)
{
  field[y][x] = false;

  REP(i, 8){
    int nx = x + dx[i], ny = y + dy[i];
    if (!inside(ny, nx, h, w)) continue;
    if (!field[ny][nx]) continue;
    dfs(ny, nx);
  }
}

signed main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  
  for (; cin >> w >> h, w || h; ){
    REP(i, h){
      REP(j, w){
        cin >> field[i][j];
      }
    }

    int ans = 0;
    REP(i, h){
      REP(j, w){
        if (field[i][j]){
          dfs(i, j);
          ans++;
        }
      }
    }
    print(ans);
  }

  return (0);
}

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

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;

int tiles[51][51];
int w,h;

void dfs(int x,int y){
    tiles[x][y]=0;

    for(int dx=-1;dx<=1;dx++){
      for(int dy=-1;dy<=1;dy++){
        int nx=x+dx,ny=y+dy;

        if(0<=nx && nx<w && 0<=ny && ny<h &&
          tiles[nx][ny] == 1) dfs(nx,ny);
        }
    }
}

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

  while(true){
    for(int i=0;i<51;i++){
      for(int j=0;j<51;j++){
        tiles[i][j]=0;
      }
    }
    int ans=0;
    cin >> w >> h;
    if(w==0) break;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        cin >> tiles[j][i];
      }
    }
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        if(tiles[j][i]==1){
           dfs(j,i);
           ans++;
         }
      }
    }
    cout << ans << endl;
  }
}