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 <bits/stdc++.h>
using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int,int,int> tpl;

#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

#define en "\n"

const double EPS = 1e-9;
const double PI  = acos(-1.0);
const int INT_INF = 2147483647;
const long long LL_INF = 1LL<<60;
const long long MOD = 1000000007;

#define CLR(a) memset((a), 0 ,sizeof(a))

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

int h,w;
int c[55][55];
bool done[55][55];
vector<pair<int,int>> v{P(-1,-1),P(-1,0),P(-1,1),P(0,-1),P(0,1),P(1,-1),P(1,0),P(1,1)};

void dfs(int x, int y){
    if(done[x][y]) return;
    done[x][y] = true;
    
    for(P p : v){
        int i = p.first, j = p.second;
        if(0<=x+i && x+i<h && 0<=y+j && y+j<w && c[x+i][y+j]) dfs(x+i,y+j);
    }
}

int main(void){

    cin >> w >> h;
    while(h!=0 || w!=0){
        CLR(c); CLR(done);
        REP(i,h)REP(j,w) cin >> c[i][j];

        int ans = 0;
        REP(i,h)REP(j,w){
            if(done[i][j] || c[i][j]==0) continue;
            dfs(i,j);
            ans++;
        }
        cout << ans << en;
        cin >> w >> h;
    }
    return 0;
}

C++: #include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define INF 1000000000
#define mod 1000000007
int H,W;
int hw[50][50];
void dfs(int x,int y){
	hw[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<H && 0<=ny && ny<W && hw[nx][ny]==1) dfs(nx,ny);
		}
	}
	return;
}

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

	st:
	int ans=0;
	cin>>W>>H;
	if(H==0 && W==0) return 0;
	REP(i,H){
		REP(j,W){
			cin>>hw[i][j];
		}
	}
	REP(i,H){
		REP(j,W){
			if(hw[i][j]==1){
				ans++;
				//cout<<i<<" "<<j<<endl;
				dfs(i,j);
			}
		}
	}
	
	cout<<ans<<endl;
	goto st;
		
	return 0;
}

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

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
#define rrep(i, m, n) for(int (i)=(m); (i)<(n);  (i)++)
#define erep(i, m, n) for(int (i)=(m); (i)<=(n); (i)++)
#define  rep(i, n)    for(int (i)=0; (i)<(n);  (i)++)
#define rrev(i, m, n) for(int (i)=(n)-1; (i)>=(m); (i)--)
#define erev(i, m, n) for(int (i)=(n); (i)>=(m); (i)--)
#define  rev(i, n)    for(int (i)=(n)-1; (i)>=0; (i)--)
#define vrep(i, c)    for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++)
#define  ALL(v)       (v).begin(), (v).end()
#define pb            push_back
template<class T, class S> inline pair<T, S> mp(T x, S y){ return make_pair(x, y); }
template<class T, class S> inline bool minup(T& m, S x){ return m>(T)x ? (m=(T)x, true) : false; }
template<class T, class S> inline bool maxup(T& m, S x){ return m<(T)x ? (m=(T)x, true) : false; }

static const int    INF = 1000000000;
static const ll     MOD = 1000000007LL;
static const double EPS = 1E-12;

const int MAX_W = 50;
const int MAX_H = 50;
int w, h;
int c[MAX_W][MAX_H+1];

inline bool check(int hx, int hy){ return 0 <= hx && hx < w && 0 <= hy && hy < h; }

int main()
{
  while(cin >> h >> w, w){
    int res = 0;
    rep(i, w) rep(j, h) cin >> c[i][j];

    rep(i, w) rep(j, h) if(c[i][j]){
      stack<pii> st;
      st.push(mp(i, j));
      c[i][j] = 0;
      res += 1;
      while(!st.empty()){
        pii here = st.top();  st.pop();
        int hx = here.first;
        int hy = here.second;
        erep(dx, -1, 1) erep(dy, -1, 1) if(dx || dy){
          int nx = hx + dx;
          int ny = hy + dy;
          if(!check(nx, ny)) continue;
          if(!c[nx][ny]) continue;
          st.push(mp(nx, ny));
          c[nx][ny] = 0;
        }
      }
    }

    cout << res << endl;
  }

  return 0;
}

C++: #include <iostream>

int h, w;
bool island[51][51];

bool func(int y, int x){
	if(!island[y][x]) return false;
	island[y][x] = false;

	for(int i = -1; i <= 1; ++i){
		for(int j = -1; j <= 1; ++j){
			if(i == 0 && j == 0) continue;
			if(y + i < 0 || y + i >= h || x + j < 0 || x + j >= w) continue;
			if(!island[y + i][x + j]) continue;
			func(y + i, x + j);
		}
	}

	return true;
}

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

		int cnt = 0;
		for(int i = 0; i < h; ++i){
			for(int j = 0; j < w; ++j){
				cnt += func(i, j);
			}
		}

		std::cout << cnt << std::endl;
	}

	return 0;
}

C++: #define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

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

void dfs(int i, int j, vector< vector<int> >& a) {
	a[i][j] = false;
	for (int di = -1; di <= 1; di++)
		for (int dj = -1; dj <= 1; dj++)
			if (a[i + di][j + dj]) dfs(i + di, j + dj, a);
}

int main() {
	for (;;) {
		int w, h; cin >> w >> h;
		if (w == 0 && h == 0) break;
		vector< vector<int> > a(h + 2, vector<int>(w + 2));
		for (int i = 1; i <= h; i++)
			for (int j = 1; j <= w; j++)
				cin >> a[i][j];
		int cnt = 0;
		for (int i = 1; i <= h; i++)
			for (int j = 1; j <= w; j++)
				if (a[i][j]) {
					cnt++;
					dfs(i, j, a);
				}
		cout << cnt << endl;
	}
}