Go: package main

import (
	"fmt"
)

func main() {
	var h, w int
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			return
		}
		matrix := make([][]int, h)
		Sean := make([][]bool, h)
		for i := 0; i < h; i++ {
			matrix[i] = make([]int, w)
			Sean[i] = make([]bool, w)
			for j := 0; j < w; j++ {
				fmt.Scan(&matrix[i][j])
			}
		}
		var count int
		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				if matrix[i][j] == 1 && Sean[i][j] == false {
					search(i, j, h, w, matrix, Sean)
					count++
				}
			}
		}
		fmt.Println(count)
	}
}
func search(x int, y int, h int, w int, matrix [][]int, Sean [][]bool) {
	if x < 0 || y < 0 || x >= h || y >= w || matrix[x][y] == 0 {
		return
	}
	if Sean[x][y] == true {
		return
	}

	Sean[x][y] = true

	search(x+1, y, h, w, matrix, Sean)
	search(x-1, y, h, w, matrix, Sean)
	search(x, y+1, h, w, matrix, Sean)
	search(x, y-1, h, w, matrix, Sean)
	search(x+1, y-1, h, w, matrix, Sean)
	search(x-1, y+1, h, w, matrix, Sean)
	search(x+1, y+1, h, w, matrix, Sean)
	search(x-1, y-1, h, w, matrix, Sean)
}

C++: #include <iostream>
#include <vector>
#include <string>

using namespace std;

void search(int w, int h, const vector< vector<int> >& area_map, vector< vector<int> >& visited, int W, int H) {
    visited[h][w] = 1;
    int dw[] = {1, 1, 0, -1, -1, -1, 0, 1}, dh[] = {0, 1, 1, 1, 0, -1, -1, -1};
    for(int i = 0; i < 8; i++) {
        int nw = w + dw[i], nh = h + dh[i];
        if(0 <= nw && nw < W && 0 <= nh && nh < H && visited[nh][nw] != 1 && area_map[nh][nw] == 1) {
            search(nw, nh, area_map, visited, W, H);
        }
    }
}

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

        vector< vector<int> > area_map, visited;
        for(int i = 0; i < h; i++) {
            vector<int> temp;
            for(int j = 0; j < w; j++) {
                int e; cin >> e;
                temp.push_back(e);
            }
            area_map.push_back(temp);
            visited.push_back(vector<int>(w));
        }
        int ans = 0;
        for(int i = 0; i < h; i++) {
            for(int j = 0; j < w; j++) {
                if(area_map[i][j] == 1 && visited[i][j] != 1) {
                    search(j, i, area_map, visited, w, h);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
}

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

int h,w;

int v[50][50];

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

void calc(int y,int x){
	if(v[y][x]==0) return;
	v[y][x]=0;
	for(int r=0;r<8;r++){
		int nx=x+dx[r],ny=y+dy[r];
		if(nx<0 || ny<0 || nx>=w || ny>=h)
			continue;
		calc(ny,nx);
	}
}


void solve()
{
	memset(v,0,sizeof(v));
	for(int y=0;y<h;y++){
		for(int x=0;x<w;x++){
			int t;cin>>t;v[y][x]=t;
		}
	}
	int res=0;

	for(int y=0;y<h;y++)
		for(int x=0;x<w;x++){
			if(v[y][x]==0) continue;
			res++;
			calc(y,x);
		}
		cout<<res<<endl;
}



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

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <stack>
#include <numeric>
#include <algorithm>
#include <stdio.h>
#include <bitset>
#include <complex>
#include <iomanip>
using namespace std;
//using ll = long long;
#define MM 1000000000
#define MOD MM+7
#define pi pair<int32,int32>
#define pl pair<int64,int64>
#define chmax(a,b) (a<b ? a=b:0)
#define chmin(a,b) (a>b ? a=b:0)
const long double PI = acos(-1);
const long long INF = 1e15;
int dx[8] = {-1,0,1,0,-1,-1,1,1};
int dy[8] = {0,-1,0,1,-1,1,1,-1};
template<typename T>
T GCD(T u,T v){
    return v ? GCD(v,u%v) : u;
}
int c[50][50];
int visited[50][50];
void init(int h,int w){
    for(int i = 0; i < 50; i++){
        for(int j = 0; j < 50; j++){
            visited[i][j] = false;
            c[i][j] = 0;
        }
    }
}
void dfs(int h,int w,int H,int W){
    visited[h][w] = true;
    for(int i = 0; i < 8; i++){
        int ny = h+dy[i];
        int nx = w+dx[i];
        if (0 <= ny && ny < H && 0 <= nx && nx < W && visited[ny][nx] == false && c[ny][nx] == 1){
            dfs(ny,nx,H,W);
        }
    }
    return;
}
int main(){
    vector<int> ans(0);
    for(;;){
        int H,W; cin >> W >> H;
        if(H == 0 && W == 0) break;
        init(H,W);
        for(int h = 0; h < H; h++){
            for(int w = 0; w < W; w++){
                cin >> c[h][w];
            }
        }
        int cnt = 0;
        for(int h = 0; h < H; h++){
            for(int w = 0; w < W; w++){
                if (c[h][w] == 1 && visited[h][w] == false){
                    dfs(h,w,H,W);
                    cnt++;
                }
            }
        }
        /* 
        for(int i = 0; i < H; i++){
            for(int j = 0; j < W; j++){
                cout << visited[i][j] << " ";
            }
            cout << endl;
        }
        */
        ans.push_back(cnt);
    }
    for(int i = 0; i < ans.size(); i++){
        cout << ans[i] << endl;
    }
}

C++: #include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
#include<stdio.h>
#include<vector>
using namespace std;
#define int long long
#define double long double
#define rep(s,i,n) for(int i=s;i<n;i++)
#define c(n) cout<<n<<endl;
#define ic(n) int n;cin>>n;
#define sc(s) string s;cin>>s;
#define mod 1000000007
#define inf 1000000000000000007
#define f first
#define s second
#define mini(c,a,b) *min_element(c+a,c+b)
#define maxi(c,a,b) *max_element(c+a,c+b)
#define P pair<int,int>
//printf("%.12Lf\n",);
int keta(int x){
	rep(0,i,30){
		if(x<10){
			return i+1;
		}
		x=x/10;
	}
}
int gcd(int x,int y){
	int aa=x,bb=y;
	rep(0,i,1000){
		aa=aa%bb;
		if(aa==0){
			return bb;
		}	
		bb=bb%aa;
		if(bb==0){
			return aa;
		}
	}
}	
int lcm(int x,int y){
	int aa=x,bb=y;
	rep(0,i,1000){
		aa=aa%bb;
		if(aa==0){
			return x/bb*y;
		}	
		bb=bb%aa;
		if(bb==0){
			return x/aa*y;
		}
	}
}
bool p(int x){
	if(x==1)return false;
	rep(2,i,sqrt(x)+1){
		if(x%i==0&&x!=i){
			return false;
		}
	}	
	return true;
}
int max(int a,int b){
	if(a>=b)return a;
	else return b;
}
int min(int a,int b){
	if(a>=b)return b;
	else return a;
}	
int n2[41];		
int nis[41]; 
int nia[41];
int mody[41];
int nn;
int com(int n,int y){
	int ni=1;
	for(int i=0;i<41;i++){
		n2[i]=ni;
		ni*=2;
	}
	int bunsi=1,bunbo=1;
	rep(0,i,y)bunsi=(bunsi*(n-i))%mod;
	rep(0,i,y)bunbo=(bunbo*(i+1))%mod;
	mody[0]=bunbo;
	rep(1,i,41){
		bunbo=(bunbo*bunbo)%mod;
		mody[i]=bunbo;
	}
	rep(0,i,41)nis[i]=0;
	nn=mod-2;
	for(int i=40;i>=0;i-=1){
		if(nn>n2[i]){
			nis[i]++;
			nn-=n2[i];
		}
	}
	nis[0]++;
	rep(0,i,41){
		if(nis[i]==1){
			bunsi=(bunsi*mody[i])%mod;
		}
	}
	return bunsi;
}
int yakuwa(int n){
	int sum=0;
	rep(1,i,sqrt(n+1)){
		if(n%i==0)sum+=i+n/i;
		if(i*i==n)sum-=i;
	}
	return sum;
}	
bool integer(double n){
	if(n==long(n))return true;
	else return false;	
}
int pow(int b,int a){	
	int aa=1;
	rep(0,i,a)aa*=b;	
	return aa;
}
int sankaku(int a){
	return a*(a+1)/2;
} 
bool search(int x){
	int n;
	int a[n];
	int l=0,r=n;
	while(r-l>=1){
		int i=(l+r)/2;
		if(a[i]==x)return true;
		else if(a[i]<x)l=i+1;
		else r=i;
	}
	return false;
}
int h,w;
char c[55][55];
void dfs(int x,int y){
	c[x][y]='0';
	for(int i=-1;i<=1;i++)for(int j=-1;j<=1;j++){
		int nx=x+i;
		int ny=y+j;
		if(0<=nx&&nx<w&&0<=ny&&ny<h&&c[nx][ny]=='1')dfs(nx,ny);
	}
	return ;
}	
signed main(){
	bool a=true;
	while(a){
		int ans=0;
		cin>>h>>w;
		if(h==0&&w==0)return 0;
		rep(0,i,w)rep(0,j,h)cin>>c[i][j];
		rep(0,i,w)rep(0,j,h){
			if(c[i][j]=='1'){
				dfs(i,j);
				ans++;
			}
		}
		c(ans)
	}	
}