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<stack>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

class Node{
public:
	int x,y;
	int node;
	Node(int p,int q){
		x=p;
		y=q;
	}
};

int main(){
	int w,h;
	while(1){
		cin>>w>>h;
		if(w==0 && h==0)break;
		stack<Node> st;
		int dt[h][w];
		bool flg[h][w];
		rep(i,h){
			rep(j,w){
				cin>>dt[i][j];
				flg[i][j]=false;
			}
		}
		int ho[8]={1,1,0,-1,-1,-1,0,1};
		int ve[8]={0,1,1,1,0,-1,-1,-1};
		int ans=0;
		rep(i,h){
			rep(j,w){
				if(!flg[i][j] &&  dt[i][j]==1){
					st.push(Node(j,i));
					while(!st.empty()){
						Node now=st.top();
						flg[now.y][now.x]=true;
						st.pop();
						rep(k,8){
							int nx=now.x+ho[k];
							int ny=now.y+ve[k];
							if(nx>=0 && ny>=0 && nx<w && ny<h && flg[ny][nx]==false){
								if(dt[ny][nx]==dt[now.y][now.x]){
									Node next(nx,ny);
									st.push(next);
								}
							}
						}
					}
					ans++;
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

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


int H, W;
int f[100][100];

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

    for(int i = -1; i <= 1; ++i){
        for(int j = -1; j <= 1; ++j){
            if(y+i<0 || x+j < 0 || y+i>=H || x+j>=W) continue;
            if(f[y+i][x+j]) dfs(y+i, x+j);
        }
    }
}

int main(){
    while(cin >> W >> H){
        if(W==0||H==0) break;
        rep(i,H) rep(j,W) cin >> f[i][j];
        int cnt = 0;
        rep(i,H)rep(j,W){
            if(f[i][j])cnt++, dfs(i,j);
        }
        cout << cnt << endl;
    }
    
}

C++: #include <iostream>
using namespace std;
int w, h;
int c[64][64];
int d[] = {1, -1, 0};

void rec(int x, int y) {
  if(x < 0 || x >= w || y < 0 || y >= h) return;
  if(!c[y][x]) return;

  c[y][x] = 0;
  for(int i = 0;i < 3; i++) {
    for(int j = 0;j < 3; j++) {
      rec(x+d[i], y+d[j]);
    }
  }
  return;
}

int main(void) {
  while(cin >> w >> h, w+h) {
    for(int i = 0;i < h; i++)
      for(int j = 0;j < w; j++)
        cin >> 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++;
          rec(j, i);
        }
      }
    }

    cout << ans << endl;
  }
}

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

const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
int field[51][51];
int h, w;

void sol(int x, int y)
{
  field[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 (field[x + dx[i]][y + dy[i]] == 1) sol(x + dx[i], y + dy[i]);
  }
}

int main()
{

  while (cin >> w >> h, h || w){
    int res = 0;
    for (int i = 0; i < h; i++){
      for (int j = 0; j < w; j++){
        cin >> field[i][j];
      }
    }

    for (int i = 0; i < h; i++){
      for (int j = 0; j < w; j++){
        if (field[i][j] == 1){
          res++;
          sol(i, j);
        }
      }
    }
    cout << res << endl;
  }
}