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 pq priority_queue
using namespace std; typedef pair<int, int> P; typedef pair<int, P> P2;
typedef pair<int, P2> P3; typedef long long ll; typedef long double ld;
constexpr long long gcd(long long a, long long b){return b ? gcd(b, a % b) : a;}
constexpr long long lcm(long long a, long long b){return a / gcd(a, b) * b;}
constexpr int INF = 1e9, MOD = INF + 7, around[] = {0, 1, 1, -1, -1, 0, -1, 1, 0, 0};
constexpr int mod_pow(long long x, long long n, const int mod){long long ret=1;while(n){if(n&1)(ret*=x)%=mod;(x*=x)%=mod;n>>=1;}return ret;}
template<int n> struct Prime{bool arr[n+1];constexpr bool operator[](int k){return arr[k];}constexpr Prime():arr(){for(int i=2;i<n;i++){arr[i]=true;for(int j=2;j*j<=i;j++){if(!(i%j))arr[i]=false;}}}};
template<int n> struct Factorial{long long arr[n+1],ary[n+1];constexpr Factorial():arr(),ary(){arr[0]=1;ary[0]=1;for(int i=0;i<n;i++){arr[i+1]=arr[i]*(i+1)%MOD;ary[i+1]=mod_pow(arr[i+1],MOD-2,MOD);}}};
constexpr Factorial<10> fact; constexpr Prime<10> prime;
constexpr int comb(int a, int b){long long pos = fact.arr[a], pot = fact.ary[a - b], por = fact.ary[b];return pos * pot % MOD * por % MOD;}
template<int n> struct Bernoulli{long long arr[n+1];constexpr Bernoulli():arr(){arr[0]=1;for(int i=1;i<=n;i++){long long sum=0;for(int j=0;j<i;j++){(sum+=comb(i+1,j)*arr[j]%MOD)%=MOD;}arr[i]=(MOD-mod_pow(i+1,MOD-2,MOD))%MOD*sum%MOD;}}};
constexpr int vx[] = {1, 0, -1, 0}, vy[] = {0, 1, 0, -1};
constexpr int sqrtN = 512, logN = 32;
constexpr ld PI = abs(acos(-1));
constexpr ll LINF=1e18;

int w, h, a[55][55];

int main(){
	while(cin >> w >> h,w){
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin >> a[i][j];
			}
		}
		
		int c = 0;
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				if(a[i][j] != 1) continue; c++;
				
				queue<P> qu; qu.push({i, j}); a[i][j] = 2;
				while(!qu.empty()){
					auto p = qu.front(); qu.pop();
					
					for(int k = 0; k < 8;){
						int nx = p.first + around[k], ny = p.second + around[++k];
						if(nx<0||ny<0||nx>=h||ny>=w||a[nx][ny] != 1) continue;
						a[nx][ny] = 2;
						qu.push({nx, ny});
					}
				}
			}
		}
		
		cout << c << endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> pp;
const int INF = 1e9;
const int MOD = 1000000007;
int w = 51,h = 51;
vector<vector<int>> maze(h,vector<int>(w,0));
vector<int> dx = {0,0,1,-1,1,1,-1,-1};
vector<int> dy = {1,-1,0,0,1,-1,1,-1};

void dfs(int y,int x){
        maze.at(y).at(x) = 2;
        rep(i,8){
                int new_y,new_x;
                new_y = y + dy[i];
                new_x = x + dx[i];
                if(0 <=new_y && new_y < h && 0<= new_x && new_x < w){
                        if(maze.at(new_y).at(new_x) == 1){
                                dfs(new_y,new_x);
                        }
                }
        }
}

int main(){
        while(1){
                cin >> w >> h;
                if(w == 0 && h == 0) break;
            rep(i,h)rep(j,w) cin >> maze.at(i).at(j);
        
            int cnt = 0;
            rep(i,h)rep(j,w){
                        if(maze.at(i).at(j) == 1){
                                dfs(i,j);
                        cnt ++;
                }
            }
        
                cout << cnt << endl;
        }
        return 0;
}

C++: #include<iostream>

int field[52][52];

void clear();

bool walk(int x, int y);

int main(){

	int w,h,ans;
	
	while(true){

		std::cin >> w >> h;
		if(w==0 && h==0)
			break;

		clear();
		for(int i=1; i<=h; i++)
			for(int j=1; j<=w; j++)
				std::cin >> field[i][j];

		ans = 0;
		for(int i=1; i<=h; i++)
			for(int j=1; j<=w; j++)
				if(walk(j, i))
					ans++;

		std::cout << ans << std::endl;

	}

	return 0;
}

void clear(){
	for(int i=0; i<52; i++)
		for(int j=0; j<52; j++)
			field[i][j] = 0;
}

bool walk(int x, int y){
	if(field[y][x] == 0)
		return false;

	field[y][x] = 0;
	
	for(int i=-1; i<=1; i++)
		for(int j=-1; j<=1; j++)
			walk(x+i, y+j);

	return true;
}

C++: #include<bits/stdc++.h>
#include<vector>
using namespace std;
vector< vector<int> > c(50, vector<int> (50));
void dfs(int x, int y, int w, int h){
  c[y][x] = 0;

  for(int i = -1 ; i <= 1 ; i++){
    for(int j = -1 ; j <= 1 ; j++){
      int nx = x + i, ny = y + j;

      if(0 <= nx && nx < w && 0 <= ny && ny < h && c[ny][nx] == 1)
        dfs(nx, ny, w, h);
    }
  }
}

int main(void){

while(true){
  int w, h;   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 = 0;
  for(int i = 0 ; i < h ; i++){
    for(int j = 0 ; j < w ; j++){
      if(c[i][j] == 1){
        dfs(j, i, w, h);
        ans++;
      }
    }
  }

  cout << ans << endl;
}

  return 0;
}

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

int field[50][50];
int w, h;
vector<int> di_x = {1,0,-1,0,1,-1,1,-1};
vector<int> di_y = {0,1,0,-1,1,1,-1,-1};

void dfs(int x, int y) {
   field[x][y] = 0;
   rep(i,8) {
      int n_x = x + di_x[i];
      int n_y = y + di_y[i];
      if (n_x < 0 || h <= n_x || n_y < 0 || w <= n_y) continue;
      else if (field[n_x][n_y] == 0) continue;
      else dfs(n_x,n_y);
   }
}

int main () {
   while (true) {
      cin >> w >> h;
      if (w == 0 && h == 0) break;
      int ct = 0;
      rep(i,h)rep(j,w) cin >> field[i][j];
      rep(i,h)rep(j,w) {
         if (field[i][j] == 1) {
            dfs(i,j);
            ct++;
         }
      }
      cout << ct << endl;
      rep(i,h)rep(j,w) field[i][j] = 0;
   }
}