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 <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <stack>
#include <climits>
#include <map>
#include <set>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const ll mod = 1000000007;
const int inf = 1e9;
const long long INF = 1LL << 60;

int ans;
int w,h;

const int w_max = 60,h_max = 60;
int dx[8] = {-1,-1,-1,0,0,1,1,1};
int dy[8] = {-1,0,1,-1,1,-1,0,1};
int v[h_max][w_max];
int f[h_max][w_max];


void dfs(int y,int x)
{
    if(v[y][x]){
        return ;
    }else{
        v[y][x] = true;
        for(int i = 0; i < 8; i++){
            int ny = y + dy[i];
            int nx = x + dx[i];
            if(ny >= 0 && ny < h && nx >= 0 && nx < w && f[ny][nx] != 0 ){
                dfs(ny,nx);
            }
        }
    }
}

int main()
{
    while(true){
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                cin >> f[i][j];
                v[i][j] = false;
            }
        }
        ans = 0;
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                if(f[i][j] == 1){
                    if(!v[i][j]){
                        ans++;
                        dfs(i,j);
                    }
                }
            }
        }
        cout << ans << endl;
    }
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <ctime>
#include <string>
#include <map>
#include <stack>
#include <queue>

using namespace std;

int w, h;
const int MAX = 50;
int c[MAX][MAX];


void check(int x, int y){
	c[x][y] = 0;
    for(int dx = -1; dx <= 1; dx++){
        for(int dy = -1; dy <= 1; dy++){
            if(x + dx >= 0 && x + dx <= h  && y + dy >= 0 && y + dy <= w && c[x + dx][y + dy] == 1){
            	check(x + dx, y + dy);
            } 
        }
    }
	
}

int main(void){

	for(int i = 0; i < MAX ; i++){
		for(int j = 0; j < MAX; j++){
			c[i][j] = 0;
		}
	}

	cin >> w >> h;
	while(w != 0 && h != 0){
		int ans = 0;
		for(int i = 0; i < h ; i++){
			for(int j = 0; j < w; j++){
				cin >> c[i][j];
			}
		}

		for(int i = 0; i < h ; i++){
			for(int j = 0; j < w; j++){
				if(c[i][j] == 1){
					check(i, j);
					ans++;
				}
			}
		}
		cout << ans << endl;

		cin >> w >> h;
	}
	
    return 0;
}

C++: #include<iostream>

using namespace std;

const int MAX = 50;
int data[MAX][MAX];
int w,h;

int x[8] = {1,0,-1,0,1,-1,-1,1};
int y[8] = {0,1,0,-1,1,-1,1,-1};

void dfs(int i, int j){
  data[i][j] = 0;
  for(int k = 0; k < 8; k++){
    int ni = i+y[k], nj = j+x[k];
    if(ni < 0 || ni>= h || nj < 0 || nj>= w) continue;
    if(data[ni][nj]) dfs(ni,nj);
  }
}

int main(){

  while(cin >> w >> h && w+h){
    int ans = 0;
    for(int i = 0; i < h; i++)
      for(int j = 0; j < w; j++) cin >> data[i][j];

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

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define between(a,x,b) ((a)<=(x)&&(x)<=(b))
#define ck(i, x) ((i) >= 0 && (i) < (x))
using namespace std;
int w, h;
int area[51][51];
int dx[] = {-1,0,1}, dy[] = {-1,0,1};
void dfs(int y, int x){
  area[y][x] = 0;
  //  printf("%d %d\n", y, x);
  rep(i, 3) rep(j, 3)
    if(ck(y+dy[i], h) && ck(x+dx[j], w) && area[y+dy[i]][x+dx[j]]) dfs(y+dy[i], x+dx[j]);

  return;
}
int main(){
  while(scanf("%d%d", &w, &h) && w && h){
    //    printf("in area\n");
    rep(i, h) rep(j, w) scanf("%d", &area[i][j]);
    int cnt = 0;
    //    puts("start solve");
    rep(i, h) rep(j, w) if(area[i][j]) dfs(i, j), cnt++;
    //    puts("end solve");
    printf("%d\n", cnt);
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define fi first
#define se second
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for(int i=(a);i>=(b);i--)
#define RREP(i, n) FOR(i, n, 0)
#define MFOR(i, m) for(auto i=(m).begin();i!=(m).end();i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())

typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;

const double eps = 1e-10;
const int MOD = 1000000007;

template<typename T>
void printv(vector<T> const& s) {
  REP(i, s.size()) {
    cout << s[i] << " ";
  }
  cout << endl;
}

int main () {
  cin.tie(0);
  cout << setprecision(10);

  
  while(1) {
    int w, h; cin >> w >> h;
    if(w == 0 && h == 0) break;
    vector<vector<bool>> c(h);
    int tmp = 0;
    REP(i, h) {
      c[i].resize(w);
      REP(j, w) {
        cin >> tmp;
        c[i][j] = (tmp == 1);
      }
    }

    int cnt = 0;
    vector<vector<bool>> sel(h);
    REP(i, h) {
      sel[i].resize(w, false);
    }

    REP(i, h) {
      REP(j, w) {
        if(!sel[i][j] && c[i][j]) {
          P st = make_pair(i, j);
          // cout << i << ":" << j << endl;
          vi dx = {-1, 0, 1}, dy = {-1, 0, 1};
          queue<P> q;
          q.push(st);
          sel[i][j] = true;
          while(!q.empty()) {
            P now = q.front();q.pop();
            REP(k, 3) {
              REP(l, 3) {
                int tmpx = now.fi + dx[k], tmpy = now.se + dy[l];
                if(0 <= tmpx && tmpx <= h-1 &&
                   0 <= tmpy && tmpy <= w-1 &&
                   !sel[tmpx][tmpy] && c[tmpx][tmpy] && (dx[k] != 0 || dy[l] != 0)) {
                  q.push(make_pair(tmpx, tmpy));
                  sel[tmpx][tmpy] = true;
                }
              }
            }
          }
          cnt++;
        }
      }
    }

    cout << cnt << endl;
  }

}

C++: #include<bits/stdc++.h>
using namespace std;
#define OUT(x) cout<<x<<endl
#define DEBUG(x) cout<<#x<<": "<<x
const int BA = 1000;
const int INFTY = (1<<21);
int c[51][51];
int w,h;
int del(int x,int y){
  if(x<0 || y<0 || x>=w || y>= h)return 0;
  if(c[x][y]){
    c[x][y] = 0;
    return del(x+1,y)+del(x,y+1)+del(x+1,y+1)+
      del(x-1,y-1)+del(x-1,y)+del(x,y-1)+del(x+1,y-1)+del(x-1,y+1);
    
  }
  else return 0;
  
}
int solve(int x,int y){
  if(x==0 && y == h)return 0;
  else if(x==w){
    return solve(0,y+1);
  }
  else if(c[x][y]==1){
    del(x,y);
    return solve(x+1,y)+1;
  }
  else return solve(x+1,y);
}
int main(){
  while(cin>>w>>h&&w&&h){
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin >> c[i][j];
      }
    }
    
    cout << solve(0,0) << endl;
  }
}