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 INF 1000000000
using namespace std;

vector<pair<int, int>> bfs(vector<pair<int, int>> not_yet, vector<vector<int>> maze)
{
  pair<int, int> check = not_yet.at(0);
  queue<pair<int, int>> a;
  a.push(check);
  maze.at(check.first).at(check.second) = 0;
  while (!a.empty())
  {
    pair<int, int> now = a.front();
    a.pop();
    not_yet.erase(find(not_yet.begin(), not_yet.end(), now));
    vector<int> move = {1, 0, -1};
    for (int i = 0; i < 3; ++i)
    {
      for (int j = 0; j < 3; ++j)
      {
        if (i == 1 && j == 1)
        {
          continue;
        }
        if (maze.at(now.first + move.at(i)).at(now.second + move.at(j)) == 1)
        {
          a.push({now.first + move.at(i), now.second + move.at(j)});
          maze.at(now.first + move.at(i)).at(now.second + move.at(j)) = 0;
        }
      }
    }
  }
  return not_yet;
}

int main()
{
  while (true)
  {
    int w, h;
    cin >> w >> h;
    if (w == h && w == 0)
    {
      break;
    }
    vector<vector<int>> maze(h + 2, vector<int>(w + 2, -1));
    vector<pair<int, int>> not_yet(0);// h w
    for (int i = 1; i < h + 1; ++i)
    {
      for (int j = 1; j < w + 1; ++j)
      {
        int point;
        cin >> point;
        maze.at(i).at(j) = point;
        if (point == 1)
        {
          not_yet.push_back({i, j});
        }
      }
    }

    int ans = 0;
    while (not_yet.size() != 0)
    {
      not_yet = bfs(not_yet, maze);
      ans += 1;
    }
    cout << ans << endl;
  }
}

C++: #include <stdio.h>
#include <iostream>

using namespace std;

int c[51][51];

void deep(int i,int n){
  if(c[i][n+1]==1){
    c[i][n+1]=2;
    deep(i,n+1);
  }
  if(c[i+1][n]==1){
    c[i+1][n]=2;
    deep(i+1,n);
  }
  if(c[i][n-1]==1){
    c[i][n-1]=2;
    deep(i,n-1);
  }
  if(c[i-1][n]==1){
    c[i-1][n]=2;
    deep(i-1,n);
  }
  if(c[i+1][n+1]==1){
    c[i+1][n+1]=2;
    deep(i+1,n+1);
  }
  if(c[i+1][n-1]==1){
    c[i+1][n-1]=2;
    deep(i+1,n-1);
  }
  if(c[i-1][n+1]==1){
    c[i-1][n+1]=2;
    deep(i-1,n+1);
  }
  if(c[i-1][n-1]==1){
    c[i-1][n-1]=2;
    deep(i-1,n-1);
  }
}

int main(){
  while(1){
    for(int i=0;i<51;i++){
      for(int n=0;n<51;n++){
          c[i][n]=0;
      }
    }
    int w,h;
    int num=0;

    scanf("%d%d",&w,&h);
    if(w==0&&h==0)break;

    for(int i=0;i<h;i++){
      for(int n=0;n<w;n++){
          scanf("%d",&c[i][n]);
      }
    }

    for(int i=0;i<h;i++){
      for(int n=0;n<w;n++){
          if(c[i][n]==1){
            num++;
            deep(i,n);
          }
      }
    }

    printf("%d\n",num);
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#include<string>
#include<vector>
using namespace std;
typedef pair<int, int>P;
typedef long long ll;
#define rep(i,n)for(ll i=0;i<n;++i)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
const ll MAX_N = 100010;
//max=({});
//条件式が真ならwhileの中身を回し続ける
//printf("%d\n", ans);

//pairの入力
//vector<pair<ll, ll>>work(n);
//rep(i, n) {
//	ll a, b;
//	cin >> a >> b;
//	work[i] = make_pair(a, b);
//for(auto p:mp)(mapの探索)
//printf("%.10f\n",なんちゃら)
//最大公約数
ll gcd(ll x, ll y) {
	return y ? gcd(y, x % y) : x;
}

ll lcm(ll x, ll y) {
	if (x == 0 || y == 0)return 0;
	return (x / gcd(x, y) * y);
}

//素因数分解
map<ll, ll>ins;
void facterize(ll n) {
	ll a = 2;
	while (n >= a * a) {
		if (n % a == 0) {
			ins[a]++;
			ins[a] = ins[a] % 1000000007;
			n /= a;
		}
		else {
			a++;
		}
	}
	ins[n]++;
}

int keta(int x) {
	int y = 0;
	while (x > 0) {
		y++;
		x /= 10;
	}
	return y;
}

ll dx[8] = { 0,0,-1,1 };
ll dy[8] = { -1,1,0,0 };
ll dp[1010][1010];
ll land[101][101];
bool flag[101][101];
ll h, w;

void dfs(ll y, ll x) {
	flag[y][x] = false;
	for (ll dx = -1;dx <= 1;++dx) {
		for (ll dy = -1;dy <= 1;++dy) {
			if (0 <= y + dy && y + dy < h && 0 <= x + dx && x + dx < w && flag[y + dy][x + dx]) {
				dfs(y + dy, x + dx);
			}
		}
	}
	return;
}

//long longしか使わない
int main() {
	while (cin >> w >> h,w||h) {
		for (ll i = 0;i < h;++i) {
			for (ll j = 0;j < w;++j) {
				cin >> land[i][j];
				if (land[i][j])flag[i][j] = true;
			}
		}
		ll ans = 0;
		for (ll i = 0;i < h;++i) {
			for (ll j = 0;j < w;++j) {
				if (flag[i][j]) {
					dfs(i, j);
					ans++;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <utility>
#include <cmath>
#include <random>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <algorithm>
using namespace std;

#define rep(i,n) for(int i = 0; i<n; ++i)
#define REP(i,n) for(int i = 1; i<=n; ++i)
#define all(x) begin(x),end(x)
typedef long long ll;
typedef pair<int,int> ip;
typedef pair<ll,ll> lp;
const int inf = 1001001000;
const ll INF = 1LL<<60;
const int MOD = (int)1e9 + 7;

int d8x[8] = {0,1,1,1,0,-1,-1,-1};
int d8y[8] = {1,1,0,-1,-1,-1,0,1};

int m[55][55];
int w,h;

void dfs(int i, int j){
	m[i][j] = 0;
	rep(c,8){
		int dy = i+d8y[c], dx = j+d8x[c];
		if(1<=dx && dx<=w && 1<=dy && dy<=h && m[dy][dx] == 1){
			dfs(dy,dx);
		}
	}
	return ;
}

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