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 <algorithm>
#include <vector>
#include <cfloat>
#include <string>
#include <cmath>
#include <set>
#include <cstdlib>
#include <map>
#include <ctime>
#include <iomanip>
#include <functional>
#include <deque>
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <stack>
#include <climits>
#include <sys/time.h>
#include <cctype>

using namespace std;

typedef long long ll;

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

    bool field[w][h];
    memset(field, false, sizeof(field));
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
	int t;
	cin >> t;
	if (t == 0) {
	  field[j][i] = false;
	}else {
	  field[j][i] = true;
	}
      }
    }

    int cnt = 0;
    int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
    int dy[] = {1, -1, 0, 0, 1, -1, -1, 1};
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
	if (field[j][i]) {
	  stack<int> st;
	  st.push(j);
	  st.push(i);
	  field[j][i] = false;
	  while (!st.empty()) {
	    int y = st.top(); st.pop();
	    int x = st.top(); st.pop();

	    for (int p = 0; p < 8; p++) {
	      int nx = x+dx[p];
	      int ny = y+dy[p];

	      if (nx < 0 || ny < 0 ||
		  nx >= w || ny >= h ||
		  !field[nx][ny]) {
		continue;
	      }
	      st.push(nx);
	      st.push(ny);
	      field[nx][ny] = false;
	    }
	  }
	  cnt++;
	}
      }
    }

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

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <cmath>
#include <string>
#include <sstream>
#include <iomanip>
#include <complex>
using namespace std;

#define ll long long
#define vvi vector< vector<int> >
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back 
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

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

int h,w;
bool inrange(int x,int y){
	if(x<0||y<0||x>h-1||y>w-1) return false;
	else return true;
}

bool search(int x,int y){
	bool rst = false;
	if(data[x][y] == 0) return rst;
	else {
		data[x][y] = 0;
		rst = true;
	}
	REP(i,8){
		int nx = x+dx[i];
		int ny = y+dy[i];
		if(!inrange(nx,ny)) continue;
		search(nx,ny);
	}
	return rst;
}

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

C++: #include <cstdio>
#define f(n)for(int i=0;i<n;i++)
#define s(w)scanf("%d",&w)
int a,n,m,d[99][99];
int u[16]={-1,-1,-1,0,0,1,1,1,-1,0,1,-1,1,-1,0,1};
void k(int x,int y){
	if(d[x][y]){f(8)k(x+u[i],y+u[i+8]);d[x][y]=0;}
}
int main(){
	a=0;
	s(n);s(m);
	f(9801)d[i/99][i%99]=0;
	f(n*m)s(d[i/n+1][i%n+1]);
	f(n*m)if(d[i/n+1][i%n+1])a++,k(i/n+1,i%n+1);
	printf("%d\n",a);
	main();
}

C++: #include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define EACH(i, c) for (auto &(i): c)
#define all(c) begin(c),end(c)
#define EXIST(s, e) ((s).find(e)!=(s).end())
#define SORT(c) sort(begin(c),end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

//#define LOCAL 0
//#ifdef LOCAL
//#define DEBUG(s) cout << (s) << endl
//#define dump(x)  cerr << #x << " = " << (x) << endl
//#define BR cout << endl;
//#else
//#define DEBUG(s) do{}while(0)
//#define dump(x) do{}while(0)
//#define BR
//#endif


//改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30)
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007
//ここから編集する
int W, H;

bool check(int w, int h) {
    return w >= 0 && h >= 0 && w < W && h < H;
}

void dfs(int w, int h, vector<vector<int> > &v) {
    if (check(w, h)) {
        if (v[h][w] != 1) return;
        v[h][w] = 2;
//        dfs(w+1,h,v);
//        dfs(w,h+1,v);
//        dfs(w-1,h,v);
//        dfs(w,h-1,v);
        for (int i = h - 1; i <= h + 1; i++) {
            for (int j = w - 1; j <= w + 1; j++) {
                dfs(j, i, v);
            }
        }
    } else {
        return;
    }
}

void solve(int w, int h) {
    W = w;
    H = h;
    vector<vector<int> > v(h, vector<int>(w, 0));
    rep(i, 0, h) {
        rep(j, 0, w) {
            cin >> v[i][j];
        }
    }
    int ans = 0;
    rep(i, 0, h) {
        rep(j, 0, w) {
            if (v[i][j] == 1) {
                ans++;
                dfs(j, i, v);
            }
        }
    }
    cout << ans << endl;
}

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

    while (true) {
        int w, h;
        cin >> w >> h;
        if (w == 0) break;
        solve(w, h);
    }


    return 0;
}

C++: #include <iostream>

using namespace std;

const int MAX_SIZE = 51;

int field[MAX_SIZE][MAX_SIZE];
int w, h;

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

bool ok(int y, int x) {
	return x >= 0 && x < w && y >= 0 && y < h;
}

void consume(int y, int x) {
	bool flag = false;
	for (int i = 0; i < 8; ++i) {
		int curx = x + dx[i],
			cury = y + dy[i];
		if (ok(cury, curx) && field[cury][curx] == 1) {
			field[cury][curx] = 0;
			consume(cury, curx);
		}
	}
}

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 >> field[i][j];

		int res = 0;
		for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) {
			if (field[i][j] == 1) {
				field[i][j] = 0;
				consume(i, j);
				++res;
			}
		}
		
		cout << res << endl;
	}
	return 0;
}