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>
#define ALL(a)  (a).begin(),(a).end()
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template <typename T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const long long INF = 1LL << 60;
#define MOD 1000000007

int w, h;
vvi c(60, vi(60));
vector<vector<bool>> flag(60, vector<bool>(60));

void dfs(int i, int j){
  flag.at(i).at(j) = false;

  vector<int> dx = {0, 0, 1, -1, 1, -1, -1, 1};
  vector<int> dy = {1, -1, 0, 0, 1, 1, -1, -1};
  for(int k = 0; k < 8; k++){
    int y = i + dy.at(k);
    int x = j + dx.at(k);
    if(x < 0 || x >= w || y < 0 || y >= h) continue;
    if(c.at(y).at(x) && flag.at(y).at(x)) dfs(y, x);
  }
}

int main(){
while(true){
  cin >> w >> h;
  if(w == 0 && h == 0) break;

  for(int i = 0; i < h; i++){
    for(int j = 0; j < w; j++){
      cin >> c.at(i).at(j);
      flag.at(i).at(j) = true;
    }
  }
  int result = 0;
  for(int i = 0; i < h; i++){
    for(int j = 0; j < w; j++){
      if(c.at(i).at(j) && flag.at(i).at(j)){
        dfs(i, j);
        result++;
      }
    }
  }
  cout << result << endl;
}
}

C++: #include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>

using namespace std;

typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
int dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dx[8] = {-1, 0, 1, -1, 0, -1, 0, 1};

void dfs(VVI &table, int i, int j) {
    table[i][j] = 0;
    for (int loop = 0; loop < 8; loop++) {
        int new_i = i + dy[loop];
        int new_j = j + dx[loop];
        if (table[new_i][new_j] == 1) {
            dfs(table, new_i, new_j);
        }
    }
    return;
}

int main(void) {
    while(true) {
        int w;
        int h;
        cin >> w >> h;
        if (h == 0) {
            break;
        }
        VVI table(h + 2, VI(w + 2, 0));
        for (int i = 1; i <= h; i++) {
            for (int j = 1; j <= w; j++) {
                cin >> table[i][j];
            }
        }
        int number = 0;
        for (int i = 1; i <= h; i++) {
            for (int j = 1; j <= w; j++) {
                if (table[i][j] == 1) {
                    dfs(table, i, j);
                    number++;
                }
            }
        }
        cout << number << endl;
    }
    return 0;
}

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

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

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

void dfs(int x, int y)
{
  if (x < 0 || y < 0 || w == x || h == y) return;
  if (c[x][y] == 0) return;
  c[x][y] = 0;
  for (int i = 0; i < 8; i++){
    dfs(x + dx[i], y + dy[i]);
  }
}

int main()
{
  while (cin >> w >> h, w){
    for (int i = 0; i < h; i++){
      for (int j = 0; j < w; j++){
        cin >> c[j][i];
      }
    }

    int cnt = 0;
    for (int i = 0; i < h; i++){
      for (int j = 0; j < w; j++){
        if (c[j][i] == 1){
          cnt++;
          dfs(j, i);
        }
      }
    }
    cout << cnt << endl;
  }
}

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

int w,h;
int m[50][50];

void solve(int x,int y){
	if(m[y][x] == 1){
		m[y][x] = 0;
		for(int i=-1;i<2;i++){
			for(int j=-1;j<2;j++){
				if(i==0&&j==0){
					continue;
				}
				if(y+i >= 0 && y+i < h && x+i >= 0 && x+i <= w){
					solve(x+j,y+i);
				}
			}
		}
	}
}

int main(){
	while(true){
		int count=0;

		cin >> w >> h;
		if(w==0&&h==0){
			break;
		}

		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin >> m[i][j];
			}
		}

		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(m[i][j] == 1){
					solve(j,i);
					count++;
				}
			}
		}

		cout << count << endl;
	}
}

C++: #include <bits/stdc++.h>
#include <math.h>
#include <vector>
#include <queue>
#include <iomanip>
#include <string>
#include <algorithm>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rep_1 (i,j,n) for(int i=j;i<n;i++)
typedef long long ll;
typedef long l;
const ll l_INF = 1010101010101010;
const int INF = 1010101010;
const int dx[] = {-1,0,1,0,-1,-1,1,1};
const int dy[] = {0,-1,0,1,1,-1,1,-1};
bool seen[100][100];
int w,h;
bool c[100][100];

int main(){
  while(true){
    cin>>w>>h;
    if(w==0) return 0;
    int cnt=0;
    rep(i,h)rep(j,w) seen[j][i]=false;
    rep(i,h)rep(j,w) cin>>c[j][i];
    int num=0;
    /*rep(i,h){
      rep(j,w) cout<<c[j][i];
      cout<<endl;
    }*/
    rep(i,h){
      rep(j,w){
        stack<int> q;
        if(c[j][i]==0||seen[j][i]) continue;
        q.push(i*w+j);
        while(!q.empty()){
          int x=q.top()%w,y=q.top()/w;
          q.pop();
          if(c[x][y]==0||seen[x][y]) continue;
          //cout<<x<<' '<<y<<endl;
          rep(d,8){
            if(y+dy[d]<0||y+dy[d]>=h||x+dx[d]<0||x+dx[d]>=w) continue;
            if(c[x+dx[d]][y+dy[d]]==0||seen[x+dx[d]][y+dy[d]]) continue;
            q.push((x+dx[d])+(y+dy[d])*w);
          }
          seen[x][y]=true;
        }
        num++;
        //cout<<i<<' '<<j<<endl;
      }
    }
    cout<<num<<endl;
  }
}