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 <string>
#include <vector>
#include <algorithm>
//#include <utility>
//#include <set>
//#include <map>
//#include <queue>
//#include <deque>
//#include <bitset>
//#include <math.h>
using namespace std ;
using ll = long long ;
//using ld = long double ;
using vll = vector<ll> ;
using vvll = vector<vll> ;
//using vc = vector<char> ;
//using vvc = vector<vc> ;
//using vb = vector<bool> ;
//using vvb = vector<vb> ;
//using pll = pair<ll,ll> ;
#define all(v) v.begin(),v.end()
//ll mod = 1000000007 ;
//long double pie = acos(-1) ;
//ll INF = 1000000000000 ;

//void yorn(bool a){if(a) cout << "Yes" << endl ; else cout << "No" << endl ;}
//void YorN(bool a){if(a) cout << "YES" << endl ; else cout << "NO" << endl ;}
//ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;}
//ll lcm(long long a,long long b){return a/gcd(a,b)*b ;}
//ll sa(long long a,long long b){if(a>b) return a-b ; return b-a ;}
//void fix_cout(){cout << fixed << setprecision(20) ;}
//void chmin(ll &a,ll b){if(a>b) a = b ;}

ll cnt = 1 ;

void DFS(vvll &g,ll h,ll w){
	if(g.at(h).at(w)==0) return ;
	g.at(h).at(w) = 0 ;
	for(int i=h-1;i<=h+1;i++) for(int j=w-1;j<=w+1;j++) DFS(g,i,j) ;
}

int main(){
	while(1){
		ll w,h ;
		cin >> w >> h ;
		if(w==0&&h==0) break ;
		vvll g(h+2,vll(w+2,0)) ;
		for(int i=1;i<=h;i++) for(int j=1;j<=w;j++) cin >> g.at(i).at(j) ;
		ll ans = 0 ;
		for(int i=1;i<=h;i++){
			for(int j=1;j<=w;j++){
				if(g.at(i).at(j)==1){
					ans++ ;
					DFS(g,i,j) ;
				}
			}
		}
		cout << ans << endl ;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

struct node{
  int x,y;
};

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

int main(){
  int w,h,i,j,k,ans=0;
  queue<node> que;
  while(1){
    cin >> w >> h;
    if(w+h == 0)break;
    int c[h][w];
    ans = 0;
    node now,next,to;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++)cin >> c[i][j];
    }
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
        if(c[i][j]){
          ++ans;
          now.x = j; now.y = i;
          que.push(now);
          while(!que.empty()){
            next = que.front();
            c[next.y][next.x] = 0;
            que.pop();
            for(k=0;k<8;k++){
              to.x = next.x + dx[k];
              to.y = next.y + dy[k];
              if(to.x<0 || to.x >=w || to.y<0 || to.y >=h)continue;
              if(c[to.y][to.x]){
                que.push(to);
              }
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;

int w,h;
int c[100][100];
int dy[] = {-1,-1,0,1,1,1,0,-1}, dx[] = {0,1,1,1,0,-1,-1,-1};

void rec(int y,int x){
  if(y<0 || x<0 || y>=h || x>=w)return;
  if(c[y][x]==0)return;
  c[y][x] = 0;
 
  for(int i=0;i<8;i++){
    rec(y+dy[i],x+dx[i]);
  }
}
 
int main(){
  while(cin >> w >> h,w){
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++)cin >> c[i][j];
    }

    int cnt = 0;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	if(c[i][j]){
	  rec(i,j); cnt++;
	}
      }
    }

    cout << cnt << endl;
  }
}

C++: #include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod=1000000007;
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;
int h,w,a[50][50];
int dy[8]={-1,-1,-1,0,0,1,1,1},dx[8]={-1,0,1,-1,1,-1,0,1};
ll uni[2500];
ll root(ll u){
    if(uni[u]<0)return u;
    return uni[u]=root(uni[u]);
}

void connect(int a,int b){
    a=root(a);
    b=root(b);
    if(a==b)return;
    if(uni[a]>uni[b]){
        a^=b;
        b^=a;
        a^=b;
    }
    uni[a]+=uni[b];
    uni[b]=a;
    return;
}
ll size(ll u){
    return -uni[root(u)];
}

int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    vector<int> v;
    while(1){
    	cin>>w>>h;
    	if(!(h||w))break;
    	for(int i=0;i<h;i++){
    		for(int j=0;j<w;j++){
    			cin>>a[i][j];
    			uni[j*h+i]=(a[i][j])?-1:0;
    		}
    	}
    	for(int i=0;i<h;i++){
    		for(int j=0;j<w;j++){
    			if(a[i][j]){
    				for(int k=0;k<8;k++){
    					int x=dx[k]+j;
    					int y=dy[k]+i;
    					if(x<0||x>=w||y<0||y>=h)continue;
    					if(a[y][x]){
    						connect(j*h+i,x*h+y);
    					}
    				}
    			}
    		}
    	}
    	int ans=0;
    	for(int i=0;i<h;i++){
    		for(int j=0;j<w;j++){
    			if(a[i][j]&&uni[j*h+i]<0)ans++;
    		}
    	}
    	v.pb(ans);
    }
    for(auto& i:v)cout << i<<endl;
    return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main(){
	int w,h;
	while(1){
		cin>>w>>h;if(w==0&&h==0)break;
	int map[55][55]={};
	int W=0;
	for(int i=0;i<h;i++)
		for(int j=0;j<w;j++)
		{int x;cin>>x;if(x==1){W++;map[i+1][j+1]=W;}}

	int MAP[55][55]={};int c=0;
	for(int i=0;i<h+1;i++)
		for(int j=0;j<w+1;j++)
		{if(map[i][j]>0){c=map[i][j];

	int G[10]={map[i-1][j-1],map[i-1][j],map[i-1][j+1],map[i][j-1],map[i][j],map[i][j+1],map[i+1][j-1],map[i+1][j],map[i+1][j+1]};
			if(map[i-1][j-1]>0&&c>map[i-1][j-1])c=map[i-1][j-1];
			if(map[i-1][j]>0&&c>map[i-1][j])c=map[i-1][j];
			if(map[i-1][j+1]>0&&c>map[i-1][j+1])c=map[i-1][j+1];
			if(map[i][j-1]>0&&c>map[i][j-1])c=map[i][j-1];
			if(map[i][j+1]>0&&c>map[i][j+1])c=map[i][j+1];
			if(map[i+1][j-1]>0&&c>map[i+1][j-1])c=map[i+1][j-1];
			if(map[i+1][j]>0&&c>map[i+1][j])c=map[i+1][j];
			if(map[i+1][j+1]>0&&c>map[i+1][j+1])c=map[i+1][j+1];
			for(int I=0;I<53;I++)
				for(int J=0;J<53;J++)
					for(int H=0;H<9;H++)
					{if(map[I][J]==G[H]&&map[I][J]>0)map[I][J]=c;}
	}
		}
	bool C[2502]={};
	for(int i=0;i<h+1;i++){//cout<<endl;
		for(int j=0;j<w+1;j++){//cout<<map[i][j]<<" ";
			if(map[i+1][j+1]>0)C[map[i+1][j+1]]=true;
		}
	}
	int R=0;
	for(int i=0;i<2501;i++)if(C[i]==true)R++;
			cout<<R<<endl;

	}
	return 0;
}