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

// region template
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

ll gcd(ll a, ll b) { return __gcd(a, b); }

ll euclid(ll a, ll b, ll &x, ll &y) {
    if (b) {
        ll d = euclid(b, a % b, y, x);
        return y -= a / b * x, d;
    }
    return x = 1, y = 0, a;
}

typedef unsigned long long ull;
typedef long double ld;
ull mod_mul(ull a, ull b, ull M) {
    ll ret = a * b - M * ull(ld(a) * ld(b) / ld(M));
    return ret + M * (ret < 0) - M * (ret >= (ll)M);
}
ull mod_pow(ull b, ull e, ull mod) {
    ull ans = 1;
    for (; e; b = mod_mul(b, b, mod), e /= 2)
        if (e & 1)
            ans = mod_mul(ans, b, mod);
    return ans;
}

const ll mod = 1000000007;
struct Mod {
    ll x;
    Mod(ll xx) : x(xx) {}
    Mod operator+(Mod b) { return Mod((x + b.x) % mod); }
    Mod operator-(Mod b) { return Mod((x - b.x) % mod); }
    Mod operator*(Mod b) { return Mod((x * b.x) % mod); }
    Mod operator/(Mod b) { return *this * invert(b); }
    Mod invert(Mod a) {
        ll x, y, g = euclid(a.x, mod, x, y);
        assert(g == 1);
        return Mod((x + mod) % mod);
    }
    Mod operator^(ll e) {
        if (!e)
            return Mod(1);
        Mod r = *this ^ (e / 2);
        r = r * r;
        return e & 1 ? *this * r : r;
    }
};
// endregion

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    cin.exceptions(cin.failbit);

    for (;;) {
        int n, m; cin >> n >> m;
        swap(n, m);
        if (!n && !m) break;

        vector<vi> mp(n, vi(m, 0));
        vector<vi> vis(n, vi(m, 0));
        rep(i, 0, n) rep(j, 0, m) cin >> mp[i][j];

        int ans = 0;
        rep(i, 0, n) rep(j, 0, m) if (!vis[i][j] && mp[i][j] == 1) {
            queue<pii> q;
            q.push({i, j});
            vis[i][j] = true;

            while (!q.empty()) {
                pii p = q.front(); q.pop();
                for (int i = -1; i <= 1; i++)
                    for (int j = -1; j <= 1; j++)
                        if (p.first + i >= 0 && p.first + i < n && p.second + j >= 0 && p.second + j < m && !vis[p.first + i][p.second + j] && mp[p.first + i][p.second + j]) {
                            vis[p.first + i][p.second + j] = 1;
                            q.push({p.first + i, p.second + j});
                        }
            }

            ans++;
        }
        cout << ans << endl;
    }
}

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

int n, w, h;
int field[50][50] = {{0}};

void solve(int x, int y){
    if (!(0 <= x < w && 0 <= y < h)) return;
    if (field[x][y] != 1) return;
    field[x][y] = -1;
    for (int x_ = -1; x_ <= 1; x_++){
        for (int y_ = -1; y_ <= 1; y_++){
            if (!(x_ == x && y_ == y)){
                solve(x+x_, y+y_);
            }
        }
    }
}

int main(){
    while (1){
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        n = 0;
        for (int i = 0; i < w; i++)
            for (int j = 0; j < h; j++)
                cin >> field[i][j];
        int sum = 0;
        for (int i = 0; i < w; i++){
            for (int j = 0; j < h; j++){
                if (field[i][j] == 1){
                    n++;
                    solve(i, j);
                }
            }
        }
        cout << n << endl;
    }
}

C++: #include <cstdio>
#define f(n)for(int i=0;i<n;i++)
#define s(w)scanf("%d",&w)
int a,n,m,d[99][99];
int u[16]={-1,-1,-1,0,0,1,1,1,-1,0,1,-1,1,-1,0,1};
void k(int x,int y){
	if(d[x+1][y+1]){f(8)k(x+u[i],y+u[i+8]);d[x+1][y+1]=0;}
}
int main(){
	a=0;
	s(n);s(m);
	f(10000)d[i]=0;
	f(n*m)s(d[i/n][i%n]);
	f(n*m)if(d[i/n][i%n])a++,k(i/n,i%n);
	printf("%d\n",a);
	main();
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int w,h,i,j;
int field[52][52];

void dfs(int x, int y){
    //埋め立て
    field[x][y]=0;
    
    if(field[x][y+1]==1) dfs(x,y+1);
    if(field[x][y-1]==1) dfs(x,y-1);
    
    if(field[x+1][y+1]==1) dfs(x+1,y+1);
    if(field[x+1][y-1]==1) dfs(x+1,y-1);
    if(field[x+1][y]==1) dfs(x+1,y);
    
    if(field[x-1][y+1]==1) dfs(x-1,y+1);
    if(field[x-1][y-1]==1) dfs(x-1,y-1);
    if(field[x-1][y]==1) dfs(x-1,y);
}


int main(){
    while(cin>>w>>h && w!=0 && h!=0){
        for(i=1; i<=h; i++){
            for(j=1; j<=w; j++){
                int a;
                scanf("%d",&a);
                field[i][j]=a;    
            }
        }
    
        int cnt=0;
    
        for(i=1; i<=h; i++){
            for(j=1; j<=w; j++){
                if(field[i][j]==1){
                    dfs(i,j);
                    cnt++;
                }    
            }
        }
        
        cout<<cnt<<endl;
    }
    return 0;
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp
// ????????¶????????£???????????????????????????????????°???????????£?????????
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int w, h, cnt;
int c[100][100], m[100][100];
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int main() {
    while(scanf("%d%d",&w,&h)) {
        if (w==0&&h==0) break;
        memset(m, -1, sizeof(m)); cnt = 0;
        for (int i=0; i<h; i++) for (int j=0; j<w; j++) scanf("%d",&c[i][j]);
        for (int i=0; i<h; i++) for (int j=0; j<w; j++) {
            if (m[i][j] == -1 && c[i][j] == 1) {
                cnt++;
                queue<P> q;
                q.push(P(i,j));
                m[i][j] = 0;
                while(!q.empty()) {
                    P p = q.front(); q.pop();
                    for (int k=0; k<8; k++) {
                        int ny = p.first + dy[k];
                        int nx = p.second + dx[k];
                        if (nx >= 0 && nx < w && ny >= 0 && ny < h && m[ny][nx] == -1 && c[ny][nx] == 1) {
                            m[ny][nx] = 0;
                            q.push(P(ny,nx));
                        }
                    }
                }
            }
        }
        printf("%d\n",cnt);
    }
}

C++: #include <iostream>
using namespace std;
const int MAX = 51;

int island[MAX][MAX],H,W;
int dx[8]={1,1,1,-1,-1,-1,0,0},dy[8]={0,1,-1,0,1,-1,-1,1};


void dfs(int x,int y){
  island[x][y] = 0;

  for(int i = 0 ; i < 8 ; i++){
    int nx = dx[i] + x;
    int ny = dy[i] + y;
    if(0 <= nx && nx < W && 0 <= ny && ny < H)
      if(island[nx][ny]) dfs(nx,ny);
  }
}

int main(){

  while(cin >> H >> W ,H + W){
    for(int i = 0 ; i < W ; i++)
      for(int j = 0 ; j < H ; j++)
	cin >> island[i][j];
    
    int ans = 0;
    for(int i = 0 ; i < W ; i++){
      for(int j = 0 ; j < H ; j++){
	if(island[i][j]){
	  dfs(i,j);
	  ans++;
	}
      }
    }
    cout << ans << endl;
  }

  return 0;
}