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++: // AOJ1160
#include <bits/stdc++.h>
#define endl "\n"
#define rep(i,n) for(int i=0;i<int(n);i++)
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ipair;
template <class T>ostream &operator<<(ostream &o,const vector<T>&v)
{o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}
namespace you {
    std::string to_string(char val) {
        return std::string(1, val);
        // return std::string{val};
        // you::to_string(char)
    }
}
int w,h;
vector<vector<int>> ma(60,vector<int>(60));
int ans;
void dfs(int y,int x){
    if(y<0||h<=y||x<0||w<=x) return;
    if(ma[y][x]==0) return;
    ma[y][x]=0; //陸を海にしちゃう
    for(int i=-1;i<=1;i++){
        for(int j=-1;j<=1;j++){
            dfs(y+i,x+j);
        }
    }
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(12);

    while(cin>>w>>h){
        ans=0;
        if(w==0 && h==0) break;
        rep(y,h){
            rep(x,w){
                cin>>ma[y][x];
            }
        }
        rep(y,h){
            rep(x,w){
                if(ma[y][x]==0) continue;
                dfs(y,x);
                ans++;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <vector>
#include <utility>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>
#include <cstring>
 
typedef long long ll;

#define rep(i,n) for(ll i = 0; i < n; i++)
 
using namespace std;
 
const ll INF = ll(1e18)+1;
const double PI=3.14159265358979323846;

int w,h,C[100][100], dx[8]={-1,1,0,0,-1,-1,1,1}, dy[8]={0,0,1,-1,-1,1,-1,1};
bool seen[100][100];

void dfs(int x, int y){
  seen[x][y] = true;
  rep(i,8){
    int movex = x+dx[i], movey = y+dy[i];
    if((movex<0 && w<=movex) || (movey<0 && h<=movey) || \
       seen[movex][movey] || C[movex][movey] == 0) continue;
    dfs(movex, movey);
  }
}

int main(){
  while(1){
    cin >> w >> h;
    if(!w && !h) break;
    rep(i,h) rep(j,w){
      cin >> C[i][j];
    }
    int ans = 0;
    rep(i,h) rep(j,w){
      if(seen[i][j] || C[i][j] == 0) continue;
      else{
	dfs(i,j);
	ans++;
      }
    }
    cout << ans << endl;
    memset(C, 0, sizeof(C));
    memset(seen, false, sizeof(seen));
  }
}

C++: #include<iostream>

using namespace std;
int w, h;
int c[50][50];
int dx[]={0, 0, 1, 1, 1, -1, -1, -1};
int dy[]={1, -1, 0, 1, -1, 0, 1, -1};
int used[50][50];

void rec(int i, int j){
   used[i][j]=1;
   for(int k=0; k<8; k++){
      int ni=i+dy[k];
      int nj=j+dx[k];
      if(!(0<=ni&&ni<h&&0<=nj&&nj<w)) continue;
      if(c[ni][nj]&&!used[ni][nj]) rec(ni, nj);
   }
}

int main(){

   while(1){
      cin>> w>> h;
      if(!(w||h)) break;
      for(int i=0; i<h; i++){
         for(int j=0; j<w; j++){
            cin>> c[i][j];
         }
      }

      fill((int*)used, (int*)used+50*50, 0);
      int cnt=0;
      for(int i=0; i<h; i++){
         for(int j=0; j<w; j++){
            if(c[i][j]&&!used[i][j]){
               cnt++;
               rec(i, j);
            }
         }
      }

      cout<< cnt<< endl;
   }

   return 0;
}

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

using std::cin;
using std::cout;
using std::endl;

int main(void) {
	int w,h;
	cin >> w;
	cin >> h;

	while (w!=0||h!=0) {
		int count = 0;
		std::stack<int> st_x;
		std::stack<int> st_y;
		int xn,yn;
		int c;
		bool all = false;
		std::vector< std::vector<bool> > island(h+2, std::vector<bool>(w+2));
		for (int i = 0; i < h+2; i++) {
			for (int j = 0; j < w+2; j++) {
				if (i==0||i==h+1||j==0||j==w+1) {
					island[i][j] = false;
				} else {
					cin >> c;
					if (c==1) {
						island[i][j] = true;
						st_x.push(j);
						st_y.push(i);
					} else {
						island[i][j] = false;
					}
				}
			}
		}

		while (!st_x.empty()) {
			xn = st_x.top();
			yn = st_y.top();
			st_x.pop();
			st_y.pop();
			if (island[xn][yn]) {
				std::stack<int> st_xn,st_yn;
				island[xn][yn] = false;
				count++;
				st_xn.push(xn);
				st_yn.push(yn);
				while (!st_xn.empty()) {
					xn = st_xn.top();
					yn = st_yn.top();
					st_xn.pop();
					st_yn.pop();
					if (island[xn+1][yn]) {
						st_xn.push(xn+1);
						st_yn.push(yn);
						island[xn+1][yn] = false;
					}
					if (island[xn-1][yn]) {
						st_xn.push(xn-1);
						st_yn.push(yn);
						island[xn-1][yn] = false;
					}
					if (island[xn+1][yn+1]) {
						st_xn.push(xn+1);
						st_yn.push(yn+1);
						island[xn+1][yn+1] = false;
					}
					if (island[xn+1][yn-1]) {
						st_xn.push(xn+1);
						st_yn.push(yn-1);
						island[xn+1][yn-1] = false;
					}
					if (island[xn-1][yn+1]) {
						st_xn.push(xn-1);
						st_yn.push(yn+1);
						island[xn-1][yn+1] = false;
					}
					if (island[xn-1][yn-1]) {
						st_xn.push(xn-1);
						st_yn.push(yn-1);
						island[xn-1][yn-1] = false;
					}
					if (island[xn][yn+1]) {
						st_xn.push(xn);
						st_yn.push(yn+1);
						island[xn][yn+1] = false;
					}
					if (island[xn][yn-1]) {
						st_xn.push(xn);
						st_yn.push(yn-1);
						island[xn][yn-1] = false;
					}
				}
			}
		}
		cout << count << endl;

		cin >> w;
		cin >> h;
	}
		return 0;
}

C++: 
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <ctime>
#include <string>
#include <map>
#include <stack>
#include <queue>

using namespace std;

int w, h;
const int MAX = 50;
int c[MAX][MAX];


void check(int x, int y){
	c[x][y] = 0;
    for(int dx = -1; dx <= 1; dx++){
        for(int dy = -1; dy <= 1; dy++){
            if(x + dx >= 0 && x + dx <= w  && y + dy >= 0 && y + dy <= h && c[x + dx][y + dy] == 1){
            	check(x + dx, y + dy);
            } 
        }
    }
	
}

int main(void){

	for(int i = 0; i < MAX ; i++){
		for(int j = 0; j < MAX; j++){
			c[i][j] = 0;
		}
	}

	cin >> w >> h;
	while(w != 0 && h != 0){
		int ans = 0;
		for(int i = 0; i < h ; i++){
			for(int j = 0; j < w; j++){
				cin >> c[i][j];
			}
		}

		for(int i = 0; i < h ; i++){
			for(int j = 0; j < w; j++){
				if(c[i][j] == 1){
					check(i, j);
					ans++;
				}
			}
		}
		cout << ans << endl;

		cin >> w >> h;
	}
	
    return 0;
}