Go: ///
// File:  1160.go
// Author: ymiyamoto
//
// Created on Sun Mar  4 20:45:57 2018
//
package main

import (
	"fmt"
)

var w, h int
var graph [][]string

func dfs(visited [][]bool, x, y int) {
	visited[x][y] = true

	for i := -1; i <= 1; i++ {
		for j := -1; j <= 1; j++ {
			dx := x + i
			dy := y + j
			if 0 <= dx && dx < h && 0 <= dy && dy < w && graph[dx][dy] == "1" && !visited[dx][dy] {
				dfs(visited, dx, dy)
			}
		}
	}
}

func main() {
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			break
		}

		graph = make([][]string, h)
		for i := range graph {
			graph[i] = make([]string, w)
		}

		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				var s string
				fmt.Scan(&s)
				graph[i][j] = s
			}
		}

		visited := make([][]bool, h)
		for i := range visited {
			visited[i] = make([]bool, w)
		}

		count := 0
		for i := range graph {
			for j := range graph[i] {
				if graph[i][j] == "1" && !visited[i][j] {
					dfs(visited, i, j)
					count++
				}
			}
		}

		fmt.Println(count)
	}
}

C++: #include<bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for(int (i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int (i) = 1; (i) <= (n); (i++))
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define pb push_back
#define eb emplace_back
#define yn {puts("Yes");}else {puts("No");}
#define printd(x) printf("%.12f\n", ans);
template<class T> bool chmin(T& a, const T& b) {if (a > b) {a = b; return 1;} return 0;}
template<class T> bool chmax(T& a, const T& b) {if (a < b) {a = b; return 1;} return 0;}
using namespace std;
using ll = long long;
using P = pair<int, int>;

int w, h;
vector<int> ans;
vector<vector<bool>> visited(55, vector<bool>(55));
vector<vector<int>> grid(55, vector<int>(55));
int dx[] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};

void _init() {
	rep (i, 51) rep (j, 51) visited[i][j] = false;
}

int dfs(int sy, int sx) {
	visited[sy][sx] = true;
	rep (k, 8) {
		int x = sx + dx[k];
		int y = sy + dy[k];
		if (0 <= x && x < w && 0 <= y && y < h && grid[y][x] && !visited[y][x]) dfs(y, x);
	}

	return 1;
}

int main(void) {
	while (1) {
		cin >> w >> h;
		if (w == 0 || h == 0) break;

		_init();

		rep (i, h) rep (j, w) cin >> grid[i][j];

		int res = 0;
		rep (i, h) rep (j, w) if (!visited[i][j] && grid[i][j]) res += dfs(i, j);
		ans.pb(res);
	}

	rep (i, ans.size()) cout << ans[i] << endl;
	return 0;
}

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

int w, h;
int c[50][50];

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

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

int main(){
    while(scanf("%d%d", &w, &h) != EOF && (w||h)){
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                scanf("%d", &c[i][j]);
            }
        }
        int ans = 0;
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                if(c[i][j]){
                    ++ans;
                    dfs(i, j);
                }
            }

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

C++: /*
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp
*/

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const ll MOD = 1000000007;

const int di[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dj[8] = {-1, 0, 1, -1, 1, -1, 0, 1};

int w, h;
int c[50][50];

int solve(){
    int cnt = 0;
    bool visited[h][w];
    memset(visited, false, sizeof visited);
    queue<pair<int, int> > Q;

    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(!visited[i][j] && c[i][j] == 1){
                cnt++;
                // bfs
                visited[i][j] = true;
                Q.push(make_pair(i, j));
                while(!Q.empty()){
                    pair<int, int> p = Q.front(); Q.pop();
                    int si = p.first;
                    int sj = p.second;

                    for(int k=0;k<8;k++){
                        int ni = si + di[k];
                        int nj = sj + dj[k];
                        if(ni < 0 || ni >= h || nj < 0 || nj >= w) continue;
                        if(!visited[ni][nj] && c[ni][nj] == 1){
                            visited[ni][nj] = true;
                            Q.push(make_pair(ni, nj));
                        }
                    }
                }
            }
        }
    }
    return cnt;
}

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    while(1){
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++) cin >> c[i][j];
        }
        int ans = solve();
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<ll,pll> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-11;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
const int DX[8]={-1,0,1,0,-1,-1,1,1},DY[8]={0,-1,0,1,-1,1,-1,1};

const int NPrime=3000000;
bool prime[NPrime+1];
vi EnumeratePrime(){
	fill(prime+2,prime+NPrime+1,true);
	vi a;
	for(int i=2;i<=NPrime;i++) if(prime[i]){
		a.emplace_back(i);
		for(int j=2*i;j<=NPrime;j+=i) prime[j]=0;
	}
	return a;
}

int h,w;
vvi a;

void dfs(int i,int j){
	a[i][j]=0;
	for(int k=0;k<8;k++){
		int x=i+DX[k],y=j+DY[k];
		if(x>=0&&x<h&&y>=0&&y<w&&a[x][y]) dfs(x,y);
	}
}

int main(){
	while(1){
		cin>>w>>h;
		if(h==0) break;
		a=vvi(h,vi(w));
		for(int i=0;i<h;i++) for(int j=0;j<w;j++) cin>>a[i][j];
		int res=0;
		for(int i=0;i<h;i++) for(int j=0;j<w;j++) if(a[i][j]){
			dfs(i,j);
			res++;
		}
		cout<<res<<endl;
	}
}

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

int c[50][50];
void dfs(int i, int j, int w, int h) {
  c[i][j] = 0;
  rep(k, 3) {
    int di = i - 1 + k;
    if (di < 0 || h <= di) continue;
    rep(l, 3) {
      int dj = j - 1 + l;
      if (dj < 0 || w <= dj) continue;
      if (di == i && dj == j) continue;
      if (c[di][dj]) dfs(di, dj, w, h);
    }
  }
}

int main() {
  vector<int> ans;
  while (1) {
    int w, h;
    cin >> w >> h;
    if (w + h == 0) break;
    // vector<vector<int> > c(w, vector<int>(h));
    rep(i, h) rep(j, w) cin >> c[i][j];
    // cout << endl;
    // rep(i, h) {
    //   rep(j, w) printf("%d ", c[i][j]);
    //   cout << endl;
    // }
    int cnt = 0;
    rep(i, h) rep(j, w) {
      if (c[i][j] == 0) continue;
      cnt++;
      dfs(i, j, w, h);
      // rep(i, h) {
      //   rep(j, w) printf("%d ", c[i][j]);
      //   cout << endl;
      // }
    }
    ans.push_back(cnt);
  }
  for (int a : ans) cout << a << endl;
  return 0;
}