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 <stdio.h>
#include <iostream>

using namespace std;

int c[51][51];

void deep(int i,int n){
  if(c[i][n+1]==1){
    c[i][n+1]=2;
    deep(i,n+1);
  }
  if(c[i+1][n]==1){
    c[i+1][n]=2;
    deep(i+1,n);
  }
  if(c[i][n-1]==1){
    c[i][n-1]=2;
    deep(i,n-1);
  }
  if(c[i-1][n]==1){
    c[i-1][n]=2;
    deep(i-1,n);
  }
  if(c[i+1][n+1]==1){
    c[i+1][n+1]=2;
    deep(i+1,n+1);
  }
  if(c[i+1][n-1]==1){
    c[i+1][n-1]=2;
    deep(i+1,n-1);
  }
  if(c[i-1][n+1]==1){
    c[i-1][n+1]=2;
    deep(i-1,n+1);
  }
  if(c[i-1][n-1]==1){
    c[i-1][n-1]=2;
    deep(i-1,n-1);
  }
}

int main(){
  while(1){
    for(int i=0;i<51;i++){
      for(int n=0;n<51;n++){
          c[i][n]=0;
      }
    }
    int w,h;
    int num=0;

    scanf("%d%d",&w,&h);
    if(w==0&&h==0)break;

    for(int i=0;i<h;i++){
      for(int n=0;n<w;n++){
          scanf("%d",&c[i][n]);
      }
    }

    for(int i=0;i<h;i++){
      for(int n=0;n<w;n++){
          if(c[i][n]==1){
            num++;
            deep(i,n);
          }
      }
    }

    printf("%d\n",num);
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#include<string>
#include<vector>
using namespace std;
typedef pair<int, int>P;
typedef long long ll;
#define rep(i,n)for(ll i=0;i<n;++i)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
const ll MAX_N = 100010;
//max=({});
//条件式が真ならwhileの中身を回し続ける
//printf("%d\n", ans);

//pairの入力
//vector<pair<ll, ll>>work(n);
//rep(i, n) {
//	ll a, b;
//	cin >> a >> b;
//	work[i] = make_pair(a, b);
//for(auto p:mp)(mapの探索)
//printf("%.10f\n",なんちゃら)
//最大公約数
ll gcd(ll x, ll y) {
	return y ? gcd(y, x % y) : x;
}

ll lcm(ll x, ll y) {
	if (x == 0 || y == 0)return 0;
	return (x / gcd(x, y) * y);
}

//素因数分解
map<ll, ll>ins;
void facterize(ll n) {
	ll a = 2;
	while (n >= a * a) {
		if (n % a == 0) {
			ins[a]++;
			ins[a] = ins[a] % 1000000007;
			n /= a;
		}
		else {
			a++;
		}
	}
	ins[n]++;
}

int keta(int x) {
	int y = 0;
	while (x > 0) {
		y++;
		x /= 10;
	}
	return y;
}

ll dx[8] = { 0,0,-1,1 };
ll dy[8] = { -1,1,0,0 };
ll dp[1010][1010];
ll land[101][101];
bool flag[101][101];
ll h, w;

void dfs(ll y, ll x) {
	flag[y][x] = false;
	for (ll dx = -1;dx <= 1;++dx) {
		for (ll dy = -1;dy <= 1;++dy) {
			if (0 <= y + dy && y + dy < h && 0 <= x + dx && x + dx < w && flag[y + dy][x + dx]) {
				dfs(y + dy, x + dx);
			}
		}
	}
	return;
}

//long longしか使わない
int main() {
	while (cin >> w >> h,w||h) {
		for (ll i = 0;i < h;++i) {
			for (ll j = 0;j < w;++j) {
				cin >> land[i][j];
				if (land[i][j])flag[i][j] = true;
			}
		}
		ll ans = 0;
		for (ll i = 0;i < h;++i) {
			for (ll j = 0;j < w;++j) {
				if (flag[i][j]) {
					dfs(i, j);
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <utility>
#include <cmath>
#include <random>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <algorithm>
using namespace std;

#define rep(i,n) for(int i = 0; i<n; ++i)
#define REP(i,n) for(int i = 1; i<=n; ++i)
#define all(x) begin(x),end(x)
typedef long long ll;
typedef pair<int,int> ip;
typedef pair<ll,ll> lp;
const int inf = 1001001000;
const ll INF = 1LL<<60;
const int MOD = (int)1e9 + 7;

int d8x[8] = {0,1,1,1,0,-1,-1,-1};
int d8y[8] = {1,1,0,-1,-1,-1,0,1};

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

void dfs(int i, int j){
	m[i][j] = 0;
	rep(c,8){
		int dy = i+d8y[c], dx = j+d8x[c];
		if(1<=dx && dx<=w && 1<=dy && dy<=h && m[dy][dx] == 1){
			dfs(dy,dx);
		}
	}
	return ;
}

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