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 <bits/stdc++.h>

#define FOR(i, a, b) for(long long int i=(a);i<=(b);i++)
#define RFOR(i, a, b) for(long long int i=(a);i>=(b);i--)
#define MOD 1000000007
#define INF 1000000000 //2000000000
#define LLINF 1000000000000000000 //9000000000000000000
#define PI 3.14159265358979

using namespace std;
typedef long long int ll;
typedef pair< long long int, long long  int> P;

int c[50][50] = {};

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

int main(void) {
	
	while (1) {
		int h, w;
		int count = 0;

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

		FOR(i, 0, h - 1) {
			FOR(t, 0, w - 1) {
				cin >> c[i][t];
			}
		}
		FOR(i, 0, h - 1) {
			FOR(t, 0, w - 1) {
				if (c[i][t] == 0) {
					continue;
				}
				
				dfs(t, i, h, w);
				count++;
			}
		}
		cout << count << endl;
	}

}

C++: /************************************
 * ???????????¢?´¢??§??¶?????°?????°??????????????°?????? *
 ************************************/

#include <iostream>
#include <queue>
#include <map>

#define LAND '1'  //??? char???
#define SEA  '0'   //??? char???

using namespace std;

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

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

bool input(vector< vector<char> > &m);
void delete_island(vector< vector< char > > &m, int x, int y);
void output(vector< vector< char> > m);


int main(){
  int w, h;
    
  while(cin >> w >> h, w && h){
    vector< vector< char > > m(h+2, vector<char>(w+2));
    input(m);
    
    int island = 0;
    for(int y = 0; y < m.size(); y++){
      for(int x = 0; x < m[y].size(); x++){
	if(m[y][x] == LAND){
	  delete_island(m,x,y);
	  island++;
	}
      }
    }
    cout << island << endl;
  }
}


bool input(vector< vector<char> > &m){
  for(int y = 0; y < m.size(); y++){
    for(int x = 0; x < m[y].size(); x++){
      if(y == 0 || x == 0 ||
	 y == m.size()-1 || x == m[y].size()-1){
	m[y][x] = SEA;
      }else{
	if(!(cin >> m[y][x])) return false;
      }
    }
  }
  return true;
}


 void delete_island(vector< vector< char > > &m, int x, int y){
   queue< pair< int, int > > q;
   q.push(make_pair(x,y));

   while(!(q.empty())){
     pair<int, int> cur = q.front();
     q.pop();
     if(m[cur.second][cur.first] == LAND){
       m[cur.second][cur.first] = SEA;
       for(int i = 0; i < sizeof(dx)/sizeof(int); i++){
	 q.push(make_pair(cur.first+dx[i],cur.second+dy[i]));
       }
     }
   }
}

void output(vector< vector< char> > m){
  for(int y = 0; y < m.size()-1; y++){
    for(int x = 0; x < m[y].size()-1; x++){
      cout << m[y][x] << " ";
    }
    cout << endl;
  }
  cout << endl;
}

C++: #include<bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
ll H, W;
ll field[110][110];
ll dx[8] = { 0, 1, 0,-1, 1, 1,-1,-1 };
ll dy[8] = { 1, 0,-1, 0, 1,-1, 1,-1 };
void dfs(ll y, ll x) {
	field[y][x] = 0;
	for (ll i = 0; i < 8; i++) {
		ll ny = y + dy[i];
		ll nx = x + dx[i];
		if (nx >= 0 && nx < H && ny >= 0 && ny < W) {
			if (field[ny][nx] == 1)
				dfs(ny, nx);
		}
	}
}
int main() {
	while (cin >> H >> W) {
		if (H == 0 && W == 0) break;
		ll cnt = 0;
		for (ll i = 0; i < W; i++) {
			for (ll j = 0; j < H; j++) {
				cin >> field[i][j];
			}
		}
		for (ll y = 0; y < W; y++) {
			for (ll x = 0; x < H; x++) {
				if (field[y][x] == 1) {
					dfs(y, x);
					cnt++;
				}
			}
		}
		cout << cnt << endl;
	}
}

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

int M[52][52];

void fill(int y, int x){
	M[y][x] = 0;
	if(M[y-1][x]) fill(y-1,x);
	if(M[y+1][x]) fill(y+1,x);
	if(M[y][x-1]) fill(y,x-1);
	if(M[y][x+1]) fill(y,x+1);
	if(M[y-1][x-1]) fill(y-1,x-1);
	if(M[y+1][x-1]) fill(y+1,x-1);
	if(M[y-1][x+1]) fill(y-1,x+1);
	if(M[y+1][x+1]) fill(y+1,x+1);
}

main(){
	int w,h,c,i,j;
	while(cin>>w>>h,w){
		memset(M, 0, sizeof(M));
		c=0;
		for(i=1;i<=h;i++){
		for(j=1;j<=w;j++){
			cin >> M[i][j];
		}
		}
		for(i=1;i<=h;i++){
		for(j=1;j<=w;j++){
			if(!M[i][j]) continue;
			fill(i, j);
			c ++;
		}
		}
		
		cout << c << endl;
	}
}

C++: #include <bits/stdc++.h>
#define r(i,n) for(int i = 0; i<n; i++)
typedef long long ll;
using namespace std;
typedef pair<int,int> P;

int w,h,sum=0,umi[52][52];

int kesi(int a,int b){
  umi[a][b]=0;
  for(int dx=-1;dx<=1;dx++){
    for(int dy=-1;dy<=1;dy++){
      if(umi[a+dx][b+dy]==1){
        kesi(a+dx,b+dy);
      }
    }
  }
  return 0;
}

int main(){
  while(cin >> w >> h&&w&&h){
    r(i,h){
      r(j,w){
        cin >> umi[i][j];
      }
    }
    r(i,h){
      r(j,w){
        if(umi[i][j]==1){
          sum++;
          kesi(i,j);
        }
      }
    }
    cout<<sum<<endl;
    sum=0;
  }
}