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 <iostream>
#include <queue>

using namespace std;

struct  pos {
	int x, y;
	pos(int _x, int _y) { x = _x; y = _y;}
};

int a[50][50];

void ans(int p,int q,int h,int w);

int main(){
	int H,W,count;
	while(1){
		count=0;
		cin >> H;
		cin >> W;
		if(H==0&&W==0){
			break;
		}
		for(int i=0;i<W;i++){
			for(int j=0;j<H;j++){
				cin >> a[i][j];
			}
		}
		
		for(int i=0;i<W;i++){
			for(int j=0;j<H;j++){
				if(a[i][j]==1){
					a[i][j]=0;
					ans(i,j,H,W);
					count++;
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}

void ans(int p, int q, int h, int w)
{
	queue<pos> Q;
	Q.push(pos(p, q));
	while (Q.size()) {
		pos b= Q.front();
		Q.pop();
		for (int i=-1;i<=1;i++) {
			for (int j=-1;j<=1;j++) {
				if(0<=b.x+i&&b.x+i<w&&0<=b.y+j&&b.y+j<h){
					if(a[b.x+i][b.y+j]==1){
						a[b.x+i][b.y+j]=0;
						Q.push(pos(b.x+i,b.y+j));
					}
				}
			// mawariwomiru
			}
		}
	}
}


/*
void ans(int p,int q,int h,int w){
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			if(0<=p+i&&p+i<h&&0<=q+j&&q+j<w){
				if(a[p+i][q+j]==1){
					a[p+i][q+j]=0;
					Q.push(pos(p+i,q+j));
				}
			}
		}
	}
	if(Q.size()){
		pos b=Q.front();
		Q.pop();
		ans(b.x,b.y,h,w);
	}
}*/

C++: #include <iostream>
#include <vector>
using namespace std;
const int di[8] = {-1, 0, 1, 0, -1, -1, 1, 1};
const int dj[8] = {0, 1, 0, -1, -1, 1, 1, -1};
bool ongrid(int h, int w, vector< vector<int> >& g)
{
    if (h >= 0 && h < g.size() && w >= 0 && w < g[0].size()) return true;
    return false;
}

bool dfs(int h, int w, vector< vector<int> >& g, vector< vector<bool> >& seen)
{
    seen[h][w] = true;
    if (g[h][w] == 0) return false;
    for (int i = 0; i < 8; i++)
    {
        if (ongrid(h+di[i], w+dj[i], g) && seen[h+di[i]][w+dj[i]] == false)
            dfs(h+di[i], w+dj[i], g, seen);
    }
    return true;
}

int main()
{
    while (true)
    {
        int w, h; cin >> w >> h;
        if (w == 0 && h == 0) break;
        vector< vector<int> > g(h, vector<int>(w));
        for (int i = 0; i < h; i++)
            for (int j = 0; j < w; j++)
                { int c; cin >> c; g[i][j] = c;}
        vector< vector<bool> > seen(h, vector<bool>(w, false));
        int cnt = 0;
        for (int i = 0; i < h; i++)
            for (int j = 0; j < w; j++)
                if (seen[i][j] == false)
                    if (dfs(i, j, g, seen) == true) cnt++;
        cout << cnt << endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;


int main(void){

	int w, h;

	while (cin >> w >> h, w, h){
		int c[100][100] = { 0 };
		vector<bool> island;
		island.push_back(false);

		for (int j = 1; j <= h; j++){
			for (int i = 1; i <= w; i++){
				cin >> c[j][i];
			}
		}

		int count = 1;
		for (int j = 1; j <= h; j++){
			for (int i = 1; i <= w; i++){
				if (c[j][i] != 0){

					if (c[j][i - 1] != 0) c[j][i] = c[j][i - 1];
					else if (c[j - 1][i + 1] != 0) c[j][i] = c[j - 1][i + 1];
					else if (c[j - 1][i] != 0) c[j][i] = c[j - 1][i];
					else if (c[j - 1][i - 1] != 0) c[j][i] = c[j - 1][i - 1];
					else{
						c[j][i] = count;
						count++;
						island.push_back(true);
					}

					island[c[j][i - 1]] = island[c[j - 1][i + 1]] = island[c[j - 1][i]] = island[c[j - 1][i - 1]] = false;
					island[c[j][i]] = true;

				}
			}
		}

		int result = 0;
		vector<bool>::iterator it;
		for (it = island.begin(); it != island.end(); ++it) 	if (*it == true) result++;
		
		cout << result << endl;

	}

	return 0;
}

/*
for(i=0;i<N;i++){
cin >> X[i];
}


for(i=0;i<N;i++){
for(j=0;j<N;j++){
cin >> X[i][j];
}
}
*/

C++: #include<stdio.h>

#define R(q,n) for(int q=0; q<n; q++)
size_t m[50][50],W,H,i,j,a,c,d;

void f(int x, int y)
{
	m[x][y]=0;
	R(b,9)
	{
		c = x+b/3-1;
		d = y+b%3-1;

		if(c<W&&d<H&&m[c][d])
			f(c,d);
	}
}

main()
{
	while(scanf("%d%d",&W,&H),W&H)
	{
		a=0;
		R(i,H) R(j,W)
			scanf("%d", &m[j][i]);

		R(i,H) R(j,W)
		if(m[j][i]){
				f(j,i);
				a++;
		}
		printf("%d\n", a);
	}
}

C++: #include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define rrep(i, a, b) for (int i = a; i >= (b); i--)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T>
inline bool chmin(T& a, T b) {
   if (a > b) {
      a = b;
      return true;
   }
   return false;
}
template <class T>
inline bool chmax(T& a, T b) {
   if (a < b) {
      a = b;
      return true;
   }
   return false;
}
const int INF = 2e9;
const int MOD = 1e9 + 7;

int main() {
   while (true) {
      int w, h;
      cin >> w >> h;
      if (w == 0 && h == 0) break;
      vector<vector<int>> c(h, vector<int>(w)), a(h, vector<int>(w));

      rep(i, 0, h) rep(j, 0, w) cin >> c[i][j];
      int ans = 0;
      function<void(int, int)> dfs = [&](int u, int v) {
         if (a[v][u] != 0) return;
         c[v][u] = ans;
         a[v][u] = 1;
         rep(i, -1, 2) rep(j, -1, 2) {
            int x_i = u + i;
            int y_j = v + j;
            if (x_i >= 0 && x_i < w && y_j >= 0 && y_j < h && c[y_j][x_i] == 1)
               dfs(x_i, y_j);
         }
         return;
      };
      rep(i, 0, h) {
         rep(j, 0, w) {
            if (a[i][j] == 0 && c[i][j] == 1) {
               ans++;
               dfs(j, i);
            }
         }
      }
      cout << ans << endl;
   }
   return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <utility>

using namespace std;

inline bool inRange(int a, int x, int b) {
    return a <= x && x < b;
}

void fill(vector<vector<int> > &v, int r, int c) {
    queue<pair<int,int> > q;
    q.push(make_pair(r, c));
    v[r][c] = 0;
    while(!q.empty()) {
        const pair<int,int> cur = q.front();
        q.pop();
        for(int dr = -1; dr <= 1; ++dr) {
            for(int dc = -1; dc <= 1; ++dc) {
                int nr = cur.first + dr;
                int nc = cur.second + dc;
                if(inRange(0, nr, v.size()) && 
                   inRange(0, nc, v[0].size()) &&
                   v[nr][nc])
                {
                    v[nr][nc] = 0;
                    q.push(make_pair(nr, nc))
                }
            }
        }
    }
}

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

    while(true) {
        int C, R;
        cin >> C >> R;
        if(!C && !R) break;

        vector<vector<int> > v(R, vector<int>(C));
        for(int i = 0; i < R; ++i) 
            for(int j = 0; j < C; ++j)
                cin >> v[i][j];

        int cnt = 0;
        for(int r = 0; r < R; ++r) {
            for(int c = 0; c < C; ++c) {
                if(v[r][c]) {
                    ++cnt;
                    fill(v, r, c);
                }
            }
        }

        cout << cnt << endl;
    }

    return 0;
}