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 rep(i,a,b) for(int i=int(a);i<int(b);++i)
using namespace std;
typedef long long ll;
int INF = (1LL << 30) - 1;
int MOD = 1e9+7;
int H,W;
void dfs(int h,int w,vector<vector<int> > &table){
    //cout << h << " " << w << endl;
    table[h][w] = 0;
    int dx[] = {-1,-1,-1,0 ,0,1 ,1,1};
    int dy[] = {-1,0 ,1 ,-1,1,-1,0,1};
    rep(i,0,8){
        int nx = w + dx[i];
        int ny = h + dy[i];
        if (nx >= W || nx < 0 || ny >= H || ny < 0) continue;
        if(table[ny][nx])dfs(ny,nx,table);
    }
}
main(){
    cin >> W >> H;
    vector< vector<int> > table;
    while(W){
        table = vector< vector<int> > (H,vector<int>(W) );
        rep(i,0,H)rep(j,0,W)cin >> table[i][j];
        int cnt = 0;
        rep(i,0,H)rep(j,0,W){
            if(table[i][j] == 1){
                //cout << i << " " << j << endl;
                dfs(i,j,table);
                cnt++;
            }
        }
        cout << cnt << endl;
        cin >> H >> W;
    }
}

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

int w, h;

void dfs(vector<vector<short>>& masu, int x, int y) {
    for (int i = -1; i <= 1; i++) {
        for (int j = -1; j <= 1; j++) {
            if (0 <= x+i && x+i < w && 0 <= y+j && y+j < h) {
                if (masu[y+j][x+i]) {
//                    cout << "#: x=" << x+i << ", y="  << y+j << endl;
                    masu[y+j][x+i] = 0;
                    dfs(masu, x+i, y+j);
                }
            }
        }
    }
}

int main(void) {
    while (cin >> w >> h, w) {
        vector<vector<short>> masu(h, vector<short>(w));
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                cin >> masu[i][j];
            }
        }

        int ans = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (masu[i][j]) {
                    dfs(masu, j, i);
                    ans++;
                }
            }
        }

        cout << ans << endl;
    }

    return 0;
}

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

const vector<int> di = {-1, -1, -1, 0, 0, 1, 1, 1};
const vector<int> dj = {-1, 0, 1, -1, 1, -1, 0, 1};

int w, h;
vector<vector<int>> field;

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

  rep(dir, 8) {
    int ni = i + di[dir];
    int nj = j + dj[dir];

    if (ni < 0 || nj < 0 || ni >= h || nj >= w) continue;
    if (field[ni][nj] == 0) continue;
    dfs(ni, nj);
  }
}

int main() {
  vector<int> ans;

  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0) break;

    field.assign(h, vector<int>(w));
    rep(i, h) rep(j, w) { cin >> field[i][j]; }

    int count = 0;

    rep(i, h) rep(j, w) {
      if (field[i][j] == 0) continue;
      dfs(i, j);
      count++;
    }

    ans.push_back(count);
  }

  for (auto &&i : ans) {
    printf("%d\n", i);
  }
}

C++: #include <iostream>
#include <queue>
using namespace std;
struct Point{int x,y;};
int main(){
  int w,h,p[60][60]={},v[60][60]={},i,j,k,o;
  queue <Point> q;
  Point n,b;
  cin >> w >> h;
  while(w!=0){
    for(j=0;j<h;j++){
      for(i=0;i<w;i++){
	cin >> p[i][j];
	v[i][j]=0;
      }
    }
    o=0;
    for(j=0;j<h;j++){
      
      for(i=0;i<w;i++){
	if(p[i][j]==1){
	  o++;
	  n.x=i;n.y=j;
	  q.push(n);
	  p[i][j]=0;
	  while(!q.empty()){
	    n=q.front();
	    q.pop();
	    if(n.x+1<w&&p[n.x+1][n.y]==1) {
	      b.x=n.x+1;b.y=n.y;
	      p[n.x+1][n.y]=0;
	      q.push(b);
	    }
	    if(n.y+1<h&&p[n.x][n.y+1]==1) {
	      b.x=n.x;b.y=n.y+1;
	      p[n.x][n.y+1]=0;
	      q.push(b);
	    }
	    if(n.x-1>=0&&p[n.x-1][n.y]==1) {
	      b.x=n.x-1;b.y=n.y;
	      p[n.x-1][n.y]=0;
	      q.push(b);
	    }
	    if(n.y-1>=0&&p[n.x][n.y-1]==1) {
	      b.x=n.x;b.y=n.y-1;
	      p[n.x][n.y-1]=0;
	      q.push(b);
	    }
	    if(n.x+1<w&&n.y+1<h&&p[n.x+1][n.y+1]==1) {
	      b.x=n.x+1;b.y=n.y+1;
	      p[n.x+1][n.y+1]=0;
	      q.push(b);
	    }
	    if(n.x-1>=0&&n.y-1>=0&&p[n.x-1][n.y-1]==1) {
	      b.x=n.x-1;b.y=n.y-1;
	      p[n.x-1][n.y-1]=0;
	      q.push(b);
	    }
	    if(n.x+1<w&&n.y-1>=0&&p[n.x+1][n.y-1]==1) {
	      b.x=n.x+1;b.y=n.y-1;
	      p[n.x+1][n.y-1]=0;
	      q.push(b);
	    }
	    if(n.x-1>=0&&n.y+1<h&&p[n.x-1][n.y+1]==1) {
	      b.x=n.x-1;b.y=n.y+1;
	      p[n.x-1][n.y+1]=0;
	      q.push(b);
	    }
	  }
	}
      }
    }
    cout << o << endl;
    cin >> w >> h;
  }
  return 0;
}