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++: // #define USE_CPP11
// #define USE_PRINTF

#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <utility>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <numeric>
#include <complex>
#include <memory>
#include <sstream>

#ifdef USE_CPP11
#include <unordered_map>
#include <unordered_set>
#include <functional>
#endif

#define PRINT(obj) cout << obj << endl
#define REP(i, n) for(int (i) = 0; (i) < (n); i++)

typedef long long ll;

using namespace std;

namespace Scanner {
#ifdef USE_PRINTF
  int    nextInt()    { int    d; scanf("%d" , &d); return d; }
  float  nextFloat()  { float  f; scanf("%f" , &f); return f; }
  double nextDouble() { double f; scanf("%lf", &f); return f; }  
  string nextString() { string s; scanf("%s" , &s); return s; }
  ll     nextll()     { ll     l; scanf("%ll", &l); return l; }
#else
  int    nextInt()    { int    d; cin >> d; return d; }
  float  nextFloat()  { float  f; cin >> f; return f; }
  double nextDouble() { double f; cin >> f; return f; }  
  string nextString() { string s; cin >> s; return s; }
  ll     nextll()     { ll     l; cin >> l; return l; }
#endif

  string getLine() { 
    string input;
    getline(cin, input);
    return input;
  }
};

namespace myLibrary {
  vector<string> split(const string& str, char delim) {
    vector<string> ret;
    size_t current = 0, found;
    while ((found = str.find_first_of(delim, current)) != string::npos) {
      ret.push_back(string(str, current, found - current));
      current = found + 1;
    }
    ret.push_back(string(str, current, str.size() - current));
    return ret;
  }
  
#ifdef USE_CPP11
  ///
  /// higher-order functional
  /// This is like map in haskell, python, etc.
  /// Sample usage:
  ///   vector<int> a = {1, 2, 3, 4, 5};
  ///   vector<int> b = myLibrary::_Map<int, int>(a, [](int n){
  ///     return n * n;
  ///   });
  /// Result:
  ///   b = 1, 4, 9, 16, 25
  ///
  template<typename T_IN, typename T_OUT, class Functor>
  vector<T_OUT> _Map(vector<T_IN> src, Functor func) {
    vector<T_OUT> ret;
    for (int i = 0; i < src.size(); i++) {
      ret.push_back(func(src[i]));
    }
    return ret;
  }
#endif

};

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

void rec(int x, int y) {
  if (x < 0 || x > 50) return;
  if (y < 0 || y > 50) return;
  if (!field[x][y]) return;
  
  field[x][y] = 0;
  int dx[8] = { 0, 1, 1, 1, 0, -1, -1, -1 };
  int dy[8] = { -1, -1, 0, 1, 1, 1, 0, -1 };
  
  REP(i, 8) {
    rec(x + dx[i], y + dy[i]);
  }
}

int main() {
  while (true) {
    w = Scanner::nextInt();
    h = Scanner::nextInt();
    if (w + h == 0) break;
    memset(field, 0, sizeof(field));
    
    REP(i, h) {
      REP(j, w) {
        int c = Scanner::nextInt();
        field[i][j] = c;   
      }
    }
       
    int count = 0;   
    REP(i, h) {
      REP(j, w) {
        if (field[i][j]) {
          count++;
          rec(i, j);
        }
      }
    }
    PRINT(count);
  }
 
  
  return 0;
}

C++: #include <iostream>
using namespace std;
int main(){
	int w,h,ct,num2;
	while(cin>>w>>h){
		ct=0;
		if(w==0&&h==0) break;
		int c[w+2][h+2];
		for(int i=0;i<w+2;i++){
			for(int j=0;j<h+2;j++){
				c[i][j]=0;
			}
		}
		for(int i=0;i<w;i++){
			for(int j=0;j<h;j++){
				cin>>c[i+1][j+1];
			}
		}
		for(int i=0;i<w+2;i++){
			for(int j=0;j<h+2;j++){
				if(c[i][j]==1){
					c[i][j]=2;
					do{
						num2=0;
						for(int ii=1;ii<w+1;ii++){
							for(int jj=1;jj<h+1;jj++){
								if(c[ii][jj]==2&&c[ii-1][jj-1]==1){
									c[ii-1][jj-1]=2;
									num2++;
								}
								else if(c[ii][jj]==2&&c[ii-1][jj]==1){
									c[ii-1][jj]=2;
									num2++;
								}
								else if(c[ii][jj]==2&&c[ii-1][jj+1]==1){
									c[ii-1][jj+1]=2;
									num2++;
								}
								else if(c[ii][jj]==2&&c[ii][jj-1]==1){
									c[ii][jj-1]=2;
									num2++;
								}
								else if(c[ii][jj]==2&&c[ii][jj+1]==1){
									c[ii][jj+1]=2;
									num2++;
								}
								else if(c[ii][jj]==2&&c[ii+1][jj-1]==1){
									c[ii+1][jj-1]=2;
									num2++;
								}
								else if(c[ii][jj]==2&&c[ii+1][jj]==1){
									c[ii+1][jj]=2;
									num2++;
								}
								else if(c[ii][jj]==2&&c[ii+1][jj+1]==1){
									c[ii+1][jj+1]=2;
									num2++;
								}
							}
						}
					} while(num2>0);
					ct++;
					for(int ii=0;ii<w+2;ii++){
						for(int jj=0;jj<h+2;jj++){
							if(c[ii][jj]==2){
								c[ii][jj]=0;
							}
						}
					}
				}
			}
		}
		cout<<ct<<endl;		
	}
    return 0;
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160

#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;

int w, h;
bool F[60][60];

struct point {
  int x, y;
};

void init() {
  fill(&F[0][0], &F[0][0]+60*60, 0);
  for (int i=1; i<=h; i++) {
    for (int j=1; j<=w; j++) {
      int temp;
      scanf("%d", &temp);
      F[i][j] = (bool) temp;
    }
  }
}

int solve() {
  int ans = 0;
  for (int i=1; i<=h; i++) {
    for (int j=1; j<=w; j++) {
      if (F[i][j]) {
	stack<point> S;
	point p = { i, j };
	S.push(p);
	while (! S.empty()) {
	  p = S.top();
	  S.pop();
	  F[p.x][p.y] = false;
	  for (int i=-1; i<=1; i++) {
	    for (int j=-1; j<=1; j++) {
	      point q = { p.x + i , p.y + j };
	      if (F[q.x][q.y]) {
		S.push(q);
	      }
	    }
	  }
	}
	ans++;
      }
    }
  }
  return ans;
}

int main() {
  while(scanf("%d%d", &w, &h) != EOF && w && h) {
    init();
    printf("%d\n", solve());
  }
}

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

int state[100][100];
bool reached[100][100];
int w,h;

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

void dfs(int x, int y) {

	if(x==0 || w<x || y==0 || h<y || state[x][y] == 0)
		return;
	if(reached[x][y] == true)
		return;

	reached[x][y]=true;

	for(int i=0; i<8; i++) {
		dfs(x+dx[i],y+dy[i]);
	}
}

int main() {

	while(cin >> w >> h,w){
		memset(state, 0, sizeof(state));
		memset(reached, false, sizeof(reached));
		for(int i=1; i<=h; i++) {
			for(int j=1; j<=w; j++) {
				cin >> state[i][j];
			}
		}
		int count=0;
		for(int i=1; i<=w; i++) {
			for(int j=1; j<=h; j++) {
				if(reached[i][j]==true || state[i][j] == 0)
					continue;
				else{
					dfs(i,j);
					count++;
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}