Go: package main

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

var sc = bufio.NewScanner(os.Stdin)
var route [][]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func dfs(h, w int) {
	if route[h][w] == 0 {
		return
	}

	route[h][w] = 0
	for _, x := range []int{-1, 0, 1} {
		for _, y := range []int{-1, 0, 1} {
			if route[h+x][w+y] == 0 {
				continue
			}
			dfs(h+x, w+y)
		}
	}
}

func main() {
	writer := bufio.NewWriter(os.Stdout)
	for {
		w, h := scanInt(), scanInt()
		if w == 0 && h == 0 {
			break
		}

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

		total := 0
		for i := 1; i <= h; i++ {
			for j := 1; j <= w; j++ {
				if route[i][j] == 1 {
					total++
					dfs(i, j)
				}
			}
		}
		fmt.Fprintln(writer, total)
	}
	writer.Flush()
}

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
using namespace std;

#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define int long long int

template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int W, H;
int board[100][100];
bool used[100][100];

void fillboard(int x, int y) {
    if(x < 0 || x >= H || y < 0 || y >= W) return;
    if(used[x][y]) return;
    if(board[x][y] == 0) return;
    // printf("x = %lld, y = %lld\n", x, y);
    used[x][y] = true;
    rep(i,0,8) fillboard(x + dx[i], y + dy[i]);
}

signed main() {
    while(cin >> W >> H, W || H) {
        memset(used, false, sizeof(used));
        rep(i,0,H) rep(j,0,W) cin >> board[i][j];

        int ans = 0;
        rep(i,0,H) rep(j,0,W) {
            if(used[i][j] || board[i][j] == 0) continue;
            ans++;
            fillboard(i, j);
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include<math.h>
#include<cmath>
#include<bitset>
#include<stdio.h>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
#include<iostream>
#include<utility>
using namespace std;
int c[55][55],seen[55][55];
int dfs(int x,int y){
  int i,j;
  seen[x][y]=1;
  for(i=x-1;i<=x+1;i++){
    for(j=y-1;j<=y+1;j++){
      if(i==x && j==y){
        continue;
      }
      if(seen[i][j]==0 && c[i][j]==1){
        dfs(i,j);
      }
    }
  }
};
int main(){
  int w,h,i,j,z;
  while(1){
    z=0;
    scanf("%d %d\n",&h,&w);
    if(w==0 && h==0){
      break;
    }
    for(i=0;i<=w+1;i++){
      for(j=0;j<=h+1;j++){
        c[i][j]=0;
        seen[i][j]=0;
      }
    }
    for(i=1;i<=w;i++){
      for(j=1;j<=h;j++){
        scanf("%d",&c[i][j]);
      }
      scanf("\n");
    }
    for(i=1;i<=w;i++){
      for(j=1;j<=h;j++){
        if(seen[i][j]==0 && c[i][j]==1){
          dfs(i,j);
          z++;
        }
      }
    }
    printf("%d\n",z);
  }
  return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <functional>
#include <vector>
#include <queue>
#include <string>
#include <complex>

#define REP(i,n) for (int i = 0; i < (n); i++)
#define FOR(i,s,n) for (int i = (s); i < (n); i++)
#define ALLOF(c) c.begin(), c.end()
#define RANGEOF(c,i,n) c.begin() + (i), c.begin() + (i) + (n)
#define EPS 1e-9
#define INF 1000000000
using namespace std;

template <class T> T read() { T t; cin >> t; return t; }
template <class T> vector<T> read(int n) { vector<T> v; REP(i,n) { v.push_back(read<T>()); } return v; }
template <class T> vector<vector<T>> read(int n, int m)
{
	vector<vector<T>> v;
	REP(i,n) { v.push_back(read<T>(m)); }
	return v;
}

template <class T> void write(const T &t) { cout << t << endl; }
template <class T> void write(const T &t, const T &t2) { cout << t << ' ' << t2 << endl; }
template <class T> void write(const vector<T> &v)
{
	ostringstream ss; 
	for (auto x : v) ss << x << ' ';
	auto s = ss.str();
	cout << s.substr(0, s.length() - 1) << endl;
}

struct _Reader { template <class T> _Reader operator ,(T &rhs) { cin >> rhs; return *this; } };
#define READ(t,...) t __VA_ARGS__; _Reader(), __VA_ARGS__

void solve();
int main() { solve(); return 0; }

/// template end ///



void solve()
{
	while (true)
	{
		READ(int, H, W);
		if (!(W|H)) { break; }
		auto field = read<char>(W, H);
		for (auto &f : field) { f.insert(f.begin(), '0'); f.push_back('0'); }
		field.insert(field.begin(), vector<char>(H+2, '0'));
		field.push_back(vector<char>(H+2, '0'));
		W += 2; H += 2;

		int count = 0;
		REP(sx,W) REP(sy,H) if (field[sx][sy] == '1')
		{
			typedef complex<int> P;
			queue<P> q;
			q.push(P(sx, sy));
			while (!q.empty())
			{
				P cur = q.front(); q.pop();
				if (field[cur.real()][cur.imag()] == '0') { continue; }
				field[cur.real()][cur.imag()] = '0';
				P d[] = { P(-1,-1), P(0,-1), P(1,-1), P(-1, 0), P(1, 0), P(-1,1), P(0,1), P(1,1) };
				REP(i, _countof(d))
				{
					P next = cur + d[i];
					if (field[next.real()][next.imag()] == '1') q.push(next);
				}
			}
			count++;
		}
		write(count);
	}
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<utility>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<complex>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long

using namespace std;
int main(){
  int w,h;
  int vx[8]={1,0,-1,0,1,1,-1,-1};
  int vy[8]={0,1,0,-1,1,-1,1,-1};
  while(cin>>w>>h,w+h){
    ll ans=0;
    int Map[h][w]={};
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	cin>>Map[i][j];
    bool Map2[h][w]={};
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(Map2[i][j])continue;
	if(Map[i][j]==0)Map2[i][j]=true;
	else{
	  Map2[i][j]=true;
	  queue<pair<int,int> >que;
	  que.push(mp(i,j));
	  while(!que.empty()){
	    pair<int,int>now=que.front();
	    que.pop();
	    for(int i=0;i<8;i++){
	      int a=now.F+vx[i],b=now.S+vy[i];
	      if(!(a>=0&&a<h&&b>=0&&b<w))continue;
	      if((Map[a][b]==1)&&(!Map2[a][b])){
		Map2[a][b]=true;
		que.push(mp(a,b));
	      }else{
		Map2[a][b]=true;
	      }
	    }
	  }
	  ans++;
	}
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}