Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		return []byte(f1())
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

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

func main() {
	defer stdout.Flush()

	for {
		ans, ok := solve()
		if !ok {
			break
		}
		println(ans)
	}

}

func solve() (int, bool) {
	w := readInt()
	h := readInt()
	if w == 0 && h == 0 {
		return 0, false
	}

	c := make([][]int, h+2)
	c[0] = make([]int, w+2)
	c[h+1] = make([]int, w+2)
	for i := 1; i-1 < h; i++ {
		c[i] = make([]int, w+2)
		for j := 1; j-1 < w; j++ {
			c[i][j] = readInt()
		}
	}

	var ans int
	for i := 1; i-1 < h; i++ {
		for j := 1; j-1 < w; j++ {
			if 0 < c[i][j] {
				ans++
				dfs(c, i, j)
			}
		}
	}
	return ans, true
}

func dfs(c [][]int, y, x int) {
	c[y][x] = 0
	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{-1, -1, -1, 0, 0, 1, 1, 1}
	for i := range dx {
		nx := x + dx[i]
		ny := y + dy[i]
		if 0 < c[ny][nx] {
			dfs(c, ny, nx)
		}
	}
}

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

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define MAX_W 50;
#define MAX_H 50;
ll N;
int W, H;
vector<ll> A;
vector<vector<int>> fields;
vector<vector<bool>> seen;

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

ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return b/gcd(a,b)*b;}

void dfs(int x, int y) {
  seen[y][x] = true;

  for (int i = 0; i < 8; i++) {
    if (x + dx[i] < 0 || x + dx[i] >= W || y + dy[i] < 0 || y + dy[i] >= H) continue;
    if (seen[y+dy[i]][x+dx[i]]) continue;
    if (fields[y + dy[i]][x + dx[i]]) dfs(x+dx[i], y+dy[i]);
  }
}

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

  // Start Loop
  while(true){
    cin >> W >> H;
    if (!(W || H)) break;
    fields.resize(H);
    seen.resize(H);
    rep(j, H) {
      fields[j].resize(W);
      seen[j].resize(W, false);
      rep(i, W) {
        cin >> fields[j][i]; 
      }
    }
    ll count = 0; 
    rep(j, H) {
      rep(i, W) {
        if (fields[j][i] && !seen[j][i]) {
          dfs(i, j);
          count++;
        }
      }
    }
    cout << count << endl;
    fields.clear();
    seen.clear();
  }
  // End Loop
  return 0;
}

C++: #include<iostream>
#include<vector>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;
void saiki(vector<vector<bool> >& in,int x,int y,int n,int m){
  in[x][y]=0;
  //cout<<x<<" "<<y<<endl;
  loop(i,-1,2){
    loop(j,-1,2){
      if(x+i>=0&&x+i<n&&y+j>=0&&y+j<m&&in[x+i][y+j])saiki(in,x+i,y+j,n,m);
    }
  }
}
int main(){
  int n,m;
  while(cin>>m>>n,n||m){
    int co=0;
    vector<vector<bool> >in(n);
    rep(i,n){
      rep(j,m){
	bool tmp;
	cin>>tmp;
	in[i].push_back(tmp);
      }
    }
    rep(i,n){
      rep(j,m){
	if(!in[i][j])continue;
	saiki(in,i,j,n,m);
	co++;
      }
    }
    cout<<co<<endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>

using namespace std;

void er(int c[52][52],int i,int j){
    c[i][j]--;
    if(c[i+1][j-1])
        er(c,i+1,j-1);
    else if(c[i][j+1])
        er(c,i,j+1);
    else if(c[i+1][j])
        er(c,i+1,j);
    else if(c[i+1][j+1])
        er(c,i+1,j+1);
}

int main(void){
    int w,h;
    int i,j;
    int a;
    int c[52][52];
    while(true){
        cin >> w >> h;
        if(w == 0){
            break;
        }
        for(i = 0;i < 52;i++){
            for(j = 0;j < 52;j++){
                c[i][j] = 0;
            }
        }
        for(i = 1;i <= w;i++){
            for(j = 1;j <= h;j++){
                cin >> c[i][j];
            }
        }
        a = 0;
        for(i = 1;i <= w;i++){
            for(j = 1;j <= h;j++){
                if(c[i][j]){
                    er(c,i,j);
                    a++;
                }
            }
        }
        cout << a << endl;
    }
}

C++: // aribon2-2_f
#include <iostream>
#include <vector>

using namespace std;

#define REP(i, n) for (int (i) = 0 ; (i) < (int)(n) ; ++(i))
#define REPN(i, m, n) for (int (i) = m ; (i) < (int)(n) ; ++(i))
#define COUT(x) cout << x << endl

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

int W, H;
vector<vector<int>> C;

void dfs(int h, int w){
    C[h][w] = 0;
    REP(i, 8){
        int nh = h + dx[i];
        int nw = w + dy[i];
        if (C[nh][nw] == 0) continue;
        dfs(nh, nw);
    }
}

int main() {

    while (true){
        cin >> W >> H;
        if (W == 0 && H == 0) return 0;

        C.assign(H+2, vector<int>(W+2, 0));
        REP(i, H) REP(j, W){
            cin >> C[i+1][j+1];
        }

        int ans = 0;
        REPN(i, 1, H+1) REPN(j, 1, W+1){
            if (C[i][j] == 0) continue;
            dfs(i, j);
            ans++;
        }

        COUT(ans);
    }
}

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

using namespace std;
using ll = long long;
using ld = long double;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define reps(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
	int w, h;
	int c[50][50] = {};

	void dfs(int i, int j) {
		c[i][j] = 0;

		for (int di = -1; di <= 1;di++) {
			for (int dj = -1; dj <= 1;dj++) {
				int ni = i + di;
				int nj = j + dj;

				if (0 <= ni&&ni < h && 0 <= nj&&nj < w && c[ni][nj] == 1) {
					dfs(ni, nj); 
				}
			}
		}
		return;
}

int main() {
	cin.sync_with_stdio(false);
	vector<int>ans;
	while (1) {
		cin >> w >> h;
		rep(i, h) {
			rep(j, w) {
				cin >> c[i][j];
			}
		}
		int res = 0;
		rep(i, h) {
			rep(j, w) {
				if (c[i][j] == 1) {
					dfs(i, j);
					res++;
				}
			}
		}
		ans.push_back(res);

		if (w == 0&&h == 0) {
			break;
		}
	}
	rep(i, ans.size()) {
		cout << ans[i] << endl;
	}

	return 0;
}