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>

#define _GLIBCXX_DEBUG
#define all(v) (v).begin(),(v).end()

using namespace std;

using ll=long long;
using pii=pair<int, int>;
using vi=vector<int>;

const ll INF=1LL<<60;
const int MOD=1e9+7;

vector<vector<char>> ch;
vector<vector<bool>> visit;
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;

void dfs(int x, int y){
    visit[x][y]=true;
    for(int i=0; i<8; i++){
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(nx<0 || nx>=h || ny<0 || ny>=w) continue;
        if(visit[nx][ny] != 0 || ch[nx][ny] == '0') continue;
        dfs(nx, ny);
    }
}

int main(){
    while(cin >> w >> h){
        if(w==0 && h==0) break;
        ch.assign(h, vector<char>(w));
        visit.assign(h, vector<bool>(w,0));
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                cin >> ch[i][j];
            }
        }
        int res=0;
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                if(ch[i][j] == '1' && visit[i][j] == 0){
                    dfs(i, j);
                    res++;
                }
            }
        }
        cout << res << endl;
    }
    return 0;
}

C++: #include <iostream>
using namespace std;
int mas[50][50];
int dx[8] = {0,1,0,-1,1,-1,1,-1};
int dy[8] = {1,0,-1,0,-1,1,1,-1};
int w,h;
void dfs(int,int);

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

void dfs(int x,int y){
    mas[y][x] = 0;
    for(int i=0;i<8;i++){
        int nx = x + dx[i];
        int ny = y + dy[i];
        if(nx >= 0 && nx < w && ny >= 0 && ny < h && mas[ny][nx] == 1){
            dfs(nx,ny);
        }
    }
}

C++: #include<cstring>
#define foreach(t,p) for(t::iterator it=p.begin();it!=p.end();p++)
#define REP(i,n) for(int i=0;i<n;i++)
using namespace std;
#define MAX_W 50

bool island[MAX_W+3][MAX_W+3];

int solve(int x,int y)
{
	if(!island[x][y]) {return 0;}

	island[x][y]= false;
	solve(x-1,y-1);
	solve(x-1,y);
	solve(x-1,y+1);
	solve(x,y-1);
	solve(x,y+1);
	solve(x+1,y+1);
	solve(x+1,y);
	solve(x+1,y-1);
//	cout << x << " " << y << endl;
	return 1;
}


int main()
{
	int w,h;
	while(cin >> w >> h &&w && h)
	{
		REP(i,MAX_W+2)REP(j,MAX_W+2) island[i][j]=false;
		REP(i,h)REP(j,w){cin >> island[i+1][j+1];}
		int kosu = 0;
		REP(i,h)REP(j,w){kosu += solve(i+1,j+1);}
		cout << kosu << endl;
		
	}
	return 0;
}

C++: //include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <complex>
#include <math.h>

using namespace std;

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

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

const long double EPS = 1e-12;
const int INF = 10000;

char mp[52][52];

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

bool bfs(int s,int t){
	if(flag[s][t])return false;
	int sum=0;
	queue<PII>pi;
	pi.push(MP(s,t));
	flag[s][t]=true;
	while(!pi.empty()){
		PII p=pi.front();pi.pop();
		sum++;
		REP(i,8){
			int y=p.first+dy[i],x=p.second+dx[i];
			if(mp[y][x]=='1'&&!flag[y][x]){
				flag[y][x]=true;
				pi.push(MP(y,x));
			}
		}
	}
	return true;
}
int main(){
	int W,H;
	while(cin>>W>>H){
		if(W==0&&H==0)break;
		REP(i,52)REP(j,52){
			mp[i][j]='0';
		}
		CLR(flag);
		int y,x;
		REP(i,H)REP(j,W){
			cin>>mp[i+1][j+1];
		}
		int sum=0;
		REP(i,H)REP(j,W){
			if(mp[i+1][j+1]=='1'&&bfs(i+1,j+1))sum++;
		}
		//cout<<"!"<<y<<" "<<x<<endl;
		cout<<sum<<endl;
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
int mp[51][51];
void init(){
  for(int i = 0; 51 > i; i++){
    for(int j = 0; 51 > j; j++){
      mp[i][j] == 0;
    }
  }
}
int tansa(int h,int w, int nh, int nw){
  if(mp[nh][nw] == 1)mp[nh][nw] = 0;
  if(nh != 0 && nw != 0 && mp[nh-1][nw-1] == 1)tansa(h,w,nh-1,nw-1);
  if(           nw != 0 && mp[nh  ][nw-1] == 1)tansa(h,w,nh  ,nw-1);
  if(nh!=h-1 && nw != 0 && mp[nh+1][nw-1] == 1)tansa(h,w,nh+1,nw-1);
  if(nh != 0 &&            mp[nh-1][nw  ] == 1)tansa(h,w,nh-1,nw  );
  if(nh!=h-1 &&            mp[nh+1][nw  ] == 1)tansa(h,w,nh+1,nw  );
  if(nh != 0 && nw!=w-1 && mp[nh-1][nw+1] == 1)tansa(h,w,nh-1,nw+1);
  if(           nw!=w-1 && mp[nh  ][nw+1] == 1)tansa(h,w,nh  ,nw+1);
  if(nh!=h-1 && nw!=w-1 && mp[nh+1][nw+1] == 1)tansa(h,w,nh+1,nw+1);
  return 0;
}
int main(){
  while(true){
    int w,h;cin>>w>>h;
    if(w == 0)break;
    for(int i = 0; h > i; i++){
      for(int j = 0; w > j; j++){
        cin >> mp[i][j];
      }
    }
    int ret = 0;
    for(int i = 0; h > i; i++){
      for(int j = 0; w > j; j++){
        if(mp[i][j] == 1){
          tansa(h,w,i,j);
          ret++;
        }
      }
    }
    cout << ret << endl;
    init();
  }
}