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 <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <cctype>
#include <complex>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <cassert>

using namespace std;

void dfs(int x, int y, int h, int w, vector<vector<int> > &f){
	
	f[x][y] = 0;
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			int x2 = x+i;
			int y2 = y+j;
			if((0<=x2 && x2<=h-1) && (0<=y2 && y2<=w-1)){
				if(f[x2][y2]) dfs(x2, y2, h, w, f);
			}
		}
	}
	
	return;
}

bool solve(){
	int w, h;
	cin>> w>> h;
	if(w==0 && h==0) return false;
	
	vector<vector<int> > field(h, vector<int>(w,0));
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			int c;
			cin>> c;
			field[i][j] += c;
		}
	}
	
	int ans = 0;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			if(field[i][j]){
				dfs(i, j, h, w, field);
				ans++;
			}
		}
	}
	
	cout<< ans<< endl;
	return true;
}

int main(){
	cout.setf(ios::fixed);
	cout.precision(10);
	while(solve());

	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<iomanip>
#include<algorithm>
#include<queue>
#include<stack>
#include<list>
#include<map>
#include<deque>
#include<math.h>
using namespace std;
#define ll long long
ll mp[100][100];
ll visited[100][100];
void dfs(ll w,ll h,ll i,ll j){
    ll a,b;
    visited[i][j]=1;
    for(a=i-1;a<=i+1;a++){
        for(b=j-1;b<=j+1;b++){
            if(a>=0&&a<h&&b>=0&&b<w&&mp[a][b]==1&&visited[a][b]==0)dfs(w,h,a,b);
        }
    }
    return ;
}

int main(){
    while(true){
        ll w,h;
        cin >> w >> h;
        if(w==0&&h==0) return 0;
        ll i,j;
        for(i=0;i<h;i++){
            for(j=0;j<w;j++){
                cin >> mp[i][j];
                visited[i][j]=0;
            }
        }
        ll ans =0;
        for(i=0;i<h;i++){
            for(j=0;j<w;j++){
                if(mp[i][j]==1&&visited[i][j]==0){
                    ans++;
                    dfs(w,h,i,j);
                }
            }
        }
        cout << ans <<endl;
    }
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()

int w,h;
int dx[] = {0,1,1,1,0,-1,-1,-1};
int dy[] = {-1,-1,0,1,1,1,0,-1};
int ba[50][50];

void go(int x, int y) {
  if (ba[x][y]==0) return;
  ba[x][y]=0;
  REP(i,8) {
    int xx=x+dx[i];
    int yy=y+dy[i];
    if (xx<0||w<=xx||yy<0||h<=yy) continue;
    if (ba[xx][yy]==1)
      go(xx,yy);
  }
}




int main() {
  while(cin>>w>>h,w+h) {
    REP(y,h)
      REP(x,w)
      cin >> ba[x][y];
    int ans = 0;
    REP(y,h) {
      REP(x,w) {
        if (ba[x][y]) {
          go(x,y);
          ans++;
        }
      }
    }
    cout << ans << endl;
  }

}

C++: #include <bits/stdc++.h>
using namespace std;
using vi=vector<int>;
using vvi=vector<vi>;
using vs=vector<string>;
using msi=map<string,int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
vvi f;
int ans=0;
void search(int x,int y){
    vi v={1,0,-1,0,1,1,-1,-1,1};
    rep(i,8){
        if(f[y+v[i]][x+v[i+1]]==1){
            f[y+v[i]][x+v[i+1]]=0;
            search(x+v[i+1],y+v[i]);
        }
    }
}

int main(void){
    int w,h;
    while(cin>>w>>h,w){
        ans=0;
        f=vvi(h+2,vi(w+2,0));
        rep(i,h)rep(j,w){
            cin>>f[i+1][j+1];
        }
        rep(i,h)rep(j,w){
            if(f[i+1][j+1]==1){
                search(j+1,i+1);
                ans++;
            }
        }
        cout<<ans<<endl;
    }
}

C++: #include<iostream>
using namespace std;
int w,h,i,j;
int map[52][52];

void rec(int i,int j){
 
  if(map[i][j]==0)return;
  map[i][j]=0;
  rec(i+1,j);
  rec(i-1,j);
  rec(i,j-1);
  rec(i,j+1);
  rec(i-1,j-1);
  rec(i-1,j+1);
  rec(i+1,j-1);
  rec(i+1,j+1);
}
int main(){
  
  
  while(1){
    cin >> w >> h;
    if(w==0 && h==0)break;
    
    for(i=1;i<=h;i++){
      for(j=1;j<=w;j++){
	cin >> map[i][j];
	
      }
    }
    int count=0;
    for(int k=1;k<=h;k++){
      for(int l=1;l<=w;l++){
	if(map[k][l]==1){
	  count++;
	  rec(k,l);
	}
      }
    }
    cout << count << endl;
  }
  
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define pb push_back
const int maxn = 10000;
const int INF32 = 1'050'000'000;
const long long INF64 = 4'000'000'000'000'000'000;
const int MOD7 = 1'000'000'007;
const int MOD9 = 1'000'000'009;

ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }

int h, w;
using Graph = vector<vector<int>>;
Graph G;
vector<pair<int,int>> vis;
int cnt;

void dfs(int x, int y)
{
	G[x][y] = cnt;
	for(int dx = -1; dx <= 1; dx++){
		for(int dy = -1; dy <= 1; dy++){
			int nx = x + dx;
			int ny = y + dy;
			if(nx<0 || nx>=h || ny<0 || ny>=w || 
				G[nx][ny]!=1)
				continue;
			dfs(nx,ny);
		}
	}
}

int main()
{
	int time = 0;
	while(1){
		cin >> w >> h;
		if(h==0&&w==0) break;
		G.clear();
		G.resize(h);
		rep(i,h) G[i].resize(w);
		rep(i,h)rep(j,w){//input
			cin >> G[i][j];
		}
		cnt = 1;
		rep(i,h)rep(j,w){
			if(G[i][j]==1){ 
				cnt++;
				dfs(i,j);
			}
		}
		// rep(i,h)rep(j,w){
		// 	if(j!=w-1) printf("%d ", G[i][j]);
		// 	else printf("%d\n", G[i][j]);
		// }
		time++;
		cout << cnt-1 << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
typedef pair<int,int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int w,h;
int nx,ny;
void dfs(vector<vector<int>> &G,int a,int b){
     if(0>nx|| nx>=h || 0>ny|| ny>=w) return ;
     if( G.at(nx).at(ny)==0) return ; 
     G.at(a).at(b)=0;
    for(int k=-1;k<=1;k++){
        for(int s=-1;s<=1;s++){
            nx=a+k;
            ny=b+s;
            dfs(G,nx,ny);
        }
    }
    return ;
}
 
 
 
int main(){
    cin>>w>>h;
    vector<vector<int>>G(h,vector<int>(w));
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>G.at(i).at(j);
        }
    }
    int count=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(G.at(i).at(j)==1){
                count++;
                dfs(G,i,j);
            }
        }
    }
cout<<count<<endl;
}