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++: // aribon2-2_f
#include <iostream>
#include <vector>

using namespace std;

#define REP(i, n) for (int (i) = 0 ; (i) < (int)(n) ; ++(i))
#define REPN(i, m, n) for (int (i) = m ; (i) < (int)(n) ; ++(i))
#define COUT(x) cout << x << endl

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

int W, H;
vector<vector<int>> C;

void dfs(int h, int w){
    C[h][w] = 0;
    REP(i, 8){
        int nh = h + dx[i];
        int nw = w + dy[i];
        if (C[nh][nw] == 0) continue;
        dfs(nh, nw);
    }
}

int main() {

    while (true){
        cin >> W >> H;
        if (W == 0 && H == 0) return 0;

        C.assign(H+2, vector<int>(W+2, 0));
        REP(i, H) REP(j, W){
            cin >> C[i+1][j+1];
        }

        int ans = 0;
        REPN(i, 1, H+1) REPN(j, 1, W+1){
            if (C[i][j] == 0) continue;
            dfs(i, j);
            ans++;
        }

        COUT(ans);
    }
}

C++: #include<bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define reps(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
	int w, h;
	int c[50][50] = {};

	void dfs(int i, int j) {
		c[i][j] = 0;

		for (int di = -1; di <= 1;di++) {
			for (int dj = -1; dj <= 1;dj++) {
				int ni = i + di;
				int nj = j + dj;

				if (0 <= ni&&ni < h && 0 <= nj&&nj < w && c[ni][nj] == 1) {
					dfs(ni, nj); 
				}
			}
		}
		return;
}

int main() {
	cin.sync_with_stdio(false);
	vector<int>ans;
	while (1) {
		cin >> w >> h;
		rep(i, h) {
			rep(j, w) {
				cin >> c[i][j];
			}
		}
		int res = 0;
		rep(i, h) {
			rep(j, w) {
				if (c[i][j] == 1) {
					dfs(i, j);
					res++;
				}
			}
		}
		ans.push_back(res);

		if (w == 0&&h == 0) {
			break;
		}
	}
	rep(i, ans.size()) {
		cout << ans[i] << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <iomanip>
#include <cassert>
#include <cmath>
using namespace std; typedef long long ll; ll an=0; const ll INF = 1e18; const ll MOD = 1e9+7;
typedef pair<int,int> P;
vector<int> v[50];

void dfs(int x, int y, int r, int c) {
  if (!v[x][y]) return;
  //cout<<"("<<x<<","<<y<<","<<r<<","<<c<<") ";
  v[x][y] = 0;
  for(int p=-1;p<=1;p++) {
    for(int q=-1;q<=1;q++) {
      int f=x+p, g=y+q;
      if (0 <= f && f < c && 0 <= g && g < r && v[f][g]) {
        dfs(f,g,r,c);
      }
    }
  }
}
int main(){
  while(true) {
    int r,c; cin>>r>>c; if (r==0) return 0;
    for(int i=0;i<c;i++) {v[i].clear(); v[i].resize(r);}
    int an=0;
    for(int i=0;i<c;i++) {
      for(int j=0;j<r;j++) {
        int x; cin>>x; v[i][j]=x;
      }
    }
    for(int i=0;i<c;i++) {
      for(int j=0;j<r;j++) {
        if (v[i][j]) {an++, dfs(i,j,r,c);}
      }
    }
    cout<<an<<endl;
  }
}

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

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

void dfs(vector< vector<int> > &plane, int x, int y){
  plane[x][y] = 0;
  for(int i=0; i<8; i++){
    if(0 <= x+dx[i] && x+dx[i] < h && 0 <= y+dy[i] && y+dy[i] < w)
      if(plane[x + dx[i]][y + dy[i]] == 1)
        dfs(plane, x + dx[i], y + dy[i]);
  }

  return;
}

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

    vector< vector<int> > plane(h, vector<int>(w));
    for(int i=0; i<h; i++)
      for(int j=0; j< w; j++)
        cin >> plane[i][j];

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

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;

#define rep(i, n) for (int i = 0; i < n; ++i)
#pragma region Debug
template <typename T>
void view(const std::vector<T> &v)
{
    for (const auto &e : v)
    {
        std::cout << e << " ";
    }
    std::cout << std::endl;
}
template <typename T>
void view(const std::vector<std::vector<T>> &vv)
{
    for (const auto &v : vv)
    {
        view(v);
    }
}
#pragma endregion

vector<vector<int>> m;

void dfs(int x, int y, int h, int w)
{
    m[y][x] = 0;

    for (int i = max(0, y - 1); i <= min(h - 1, y + 1); ++i)
    {
        for (int j = max(0, x - 1); j <= min(w - 1, x + 1); ++j)
        {
            if (m[i][j])
            {
                // cout << y << " " << x << "->" << i << " " << j << endl;
                dfs(j, i, h, w);
            }
        }
    }
}

int main()
{
    int w = 1, h = 1;
    while (true)
    {
        cin >> w >> h;
        if (w == 0 && h == 0)
        {
            break;
        }
        m = vector<vector<int>>(h, vector<int>(w));
        rep(i, h) rep(j, w) cin >> m[i][j];

        int ans = 0;
        rep(i, h) rep(j, w)
        {
            if (m[i][j])
            {
                ans++;
                dfs(j, i, h, w);
            }
        }

        cout << ans << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
#define LOOP(n) for (int _i = 0; _i < (n); _i++)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, r, n) for (int i = (r); i < (n); ++i)
#define ALL(obj) begin(obj), end(obj)
using namespace std;
using ll = long long;
using ull = unsigned long long;

const int INF = 2100100100;
const int MOD = 1e9 + 7;

// 多次元 vector 生成
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}

int N, M, K, Q, W, H, R, C;
int table[55][55];
bool used[55][55];

void dfs(int x, int y) {
    used[x][y] = true;
    vector<int> dx = {1, -1, 0, 0, 1, 1, -1, -1};
    vector<int> dy = {0, 0, 1, -1, 1, -1, 1, -1};
    REP(i, 8) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (0 <= nx && nx < H && 0 <= ny && ny < W && !used[nx][ny] && table[nx][ny] == 1) {
            dfs(nx, ny);
        }
    }
}

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

    vector<int> ans;
    while (true) {
        cin >> W >> H;
        if (W == 0 && H == 0) break;
        memset(used, 0, sizeof(used));
        REP(i, H) {
            REP(j, W) { cin >> table[i][j]; }
        }

        int cnt = 0;
        REP(i, H) {
            REP(j, W) {
                if (table[i][j] == 1 && !used[i][j]) {
                    dfs(i, j);
                    cnt++;
                }
            }
        }
        ans.push_back(cnt);
    }
    for (auto i : ans) {
        cout << i << endl;
    }

    return 0;
}