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 <deque>
#include <cstdio>
using namespace std;

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

int mesh[50][50];
int n, m;

struct Point
{
	int x;
	int y;
};

int main()
{
	deque<Point> stack;
	while (true)
	{
		scanf("%d %d", &n, &m);
		if (n == 0 && m == 0) break;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				int a;
				scanf("%d", &a);
				mesh[i][j] = a;
			}
		}
		int count = 0;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if (mesh[i][j] == 1)
				{
					Point pt = {j, i};
					stack.push_front(pt);
					mesh[i][j] = 0;
					while (!stack.empty())
					{
						Point point = stack.front();
						stack.pop_front();
						for (int k = 0; k < 8; k++)
						{
							if (mesh[point.y + dy[k]][point.x + dx[k]] == 1)
							{
								Point pt = {point.x + dx[k], point.y+dy[k]};
								stack.push_front(pt);
								mesh[point.y + dy[k]][point.x + dx[k]] = 0;
							}
						}
					}
					count++;
				}
			}
		}
		printf("%d\n", count);
	}
}

C++: #include<iostream>
#include<sstream>
#include<fstream>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<functional>
#include<utility>
#include<bitset>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cstdio>

using namespace std;

#define REP(i,n) for(int i=0;i<int(n);i++)
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
typedef long long ll;
typedef pair<int,int> P;

int L[55][55];
int w,h;
bool inside(int i,int j){
	return (1<=i && i<=h && 1<=j && j<=w);
}

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

void dfs(int i,int j){
	L[i][j]=0;
    REP(k,8){
    int x=i+dx[k],y=j+dy[k];
	if(inside(x,y)){
		if(L[x][y]==1) dfs(x,y);
	}
    }
}

int main(){
	  int i,j;

	  while(1){
		  cin >> w >> h;
		  if(w==0 && h==0) break;
		  for(i=1;i<=h;i++) for(j=1;j<=w;j++) cin >> L[i][j];

		  int ans=0;
		  for(i=1;i<=h;i++) for(j=1;j<=w;j++){
			  if(L[i][j]==1){
				  ans++;
				  dfs(i,j);
			  }
		  }

		  cout << ans << endl;

	  }

	  return 0;
}

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

const int N = 55;
int C[N][N];
bool seen[N][N];

int h, w;

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

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

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    while(cin >> w >> h){
        if(h == 0 && w == 0){
            break;
        }

        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                cin >> C[i][j];
                seen[i][j] = false;
            }
        }

        int ans = 0;
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                if(C[i][j] == 0 || seen[i][j]) continue;
                ans++;
                dfs(i, j);
            }
        }

        cout << ans << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <sstream>

#define ll long long int
#define rep(i,x,y) for(int i=x;i<y;i++)
#define rel(i,x,y) for(int i=x-1;i>=y;i--)
#define all(x) x.begin(),x.end()
#define NMAX 50

using namespace std;
bool MAP[NMAX][NMAX];
bool alr[NMAX][NMAX];

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

void DFS(int y,int x){
	alr[y][x] = true;
	rep(i,0,9){
		if(0<=y+dy[i/3] && y+dy[i/3]<h && 0<=x+dx[i%3] && x+dx[i%3]<w && MAP[y+dy[i/3]][x+dx[i%3]] && !alr[y+dy[i/3]][x+dx[i%3]]){
			DFS(y+dy[i/3],x+dx[i%3]);
		}
	}
}

int main(){
	while(true){
	int count=0;
	cin >> w >> h;
	if(w == 0 && h == 0){ break; }
	rep(i,0,h){
		rep(j,0,w){
			alr[i][j] = false;
			cin >> MAP[i][j];
		}
	}
	rep(i,0,h){
		rep(j,0,w){
			if(MAP[i][j] && !alr[i][j]){
				DFS(i,j);
				count++;
			}
		}
	}
	cout << count << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,n1,n2) for(int i=n1;i<n2;i++)
#define speed_up    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

typedef long long int ll;
typedef pair<ll,ll> Pi;
const int INF=(ll)(1LL<<31)-1;
const ll INFl=(ll)9223372036854775807;
const int MAX=10000;
const ll MOD=(ll)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a, b)*b;}
//int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

int w,h;
int ls[55][55];
int alr[55][55];

void dfs(int a,int b){
	alr[a][b]=1;
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			if(i!=0||j!=0){
				if(a+i>=0&&a+i<h&&b+j>=0&&b+j<w){
					if(alr[a+i][b+j]==0&&ls[a+i][b+j]==1){
						dfs(a+i,b+j);
					}
				}
			}
		}
	}
	return;
}

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