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<vector>
using namespace std;
int needx[]={-1,0,1,0,1,1,-1,-1};
int needy[]={0,1,0,-1,1,-1,1,-1};
//テ・ツウツカテ」ツつ津・ツ?ィテ」ツ?ヲfalseテ」ツ?ォテ・ツ、ツ嘉」ツ?暗」ツ?ヲtrueテ」ツつ津ィツソツ氾」ツ?凖ゥツ鳴「テヲツ閉ー//
bool land(int x,int y,vector<vector<bool> >& cango){
    if(cango[x][y]){
        cango[x][y]=false;
        for(int i=0;i<8;i++){
            int nextx=x+needx[i];
            int nexty=y+needy[i];
            if(cango[nextx][nexty])
                bool flag=land(nextx,nexty,cango);
            }
    return true;
     }else{
return false;
    }
}
int main(void){
    int input;
	int w,h;
	while(1){
cin>>w>>h;
if(h==0)break;
    vector<vector<bool> > cango(h+2,vector<bool>(w+2,false));
    for(int i=1;i<=h;i++){
        for(int j=1;j<=w;j++){
            cin >>input;
            if(input==1){
cango[i][j]=true;
            }
        }
    }
  
    int t=0;
for(int i=1;i<=h;i++){
    for(int j=1;j<=w;j++){
        if(land(i,j,cango)){
            t++;
        }
    }
}
cout <<t<<endl;
	}
return 0;
}

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

using namespace std;
using Point = pair<int,int>;

int map[52][52];

void BFS(const Point &p0){
  queue<Point> q;
  q.push(p0);
  map[p0.first][p0.second] = 0;
  while(!q.empty()){
    auto p = q.front();q.pop();
    int y = p.first , x = p.second;

    for(int i=-1;i<=1;++i){
      for(int j=-1;j<=1;++j){
        if(i==0&&j==0)continue;
        if(map[i+y][j+x]){
          q.push(Point(i+y,j+x));
          map[i+y][j+x] = 0;
        }
      }
    }

  }

}

int main(){
  int w,h;
  while(cin>>w>>h&&w>0&&h>0){

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


    int num = 0;
    for(int i=1;i<h+1;++i){
      for(int j=1;j<w+1;++j){
        if(map[i][j]){
          BFS(Point(i,j));
          num++;
        }
      }
    }
    cout << num << endl;
  }

  return 0;
}

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

#define rep(i,n) for(int i = 0; i < (n); i++)
#define rrep(i,n) for(int i = (n)-1; i >= 0; i--)
#define rep1(i,n) for(int i = 1; i <= (n); i++)
#define rrep1(i,n) for(int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

#define MAX_LEN 50
int board[MAX_LEN][MAX_LEN];
bool used[MAX_LEN][MAX_LEN];
int w,h;

bool isin(int x, int y){
  return (x>=0 && x<w && y>=0 && y<h);
}

void dfs(int x, int y){
  if( !isin(x, y) )return;
  if(used[y][x])return;
  if(board[y][x]==0)return;
  used[y][x]=true;

  dfs(x-1, y);
  dfs(x+1, y);
  dfs(x, y-1);
  dfs(x, y+1);

  dfs(x-1, y-1);
  dfs(x-1, y+1);
  dfs(x+1, y-1);
  dfs(x+1, y+1);
}

int main(){
  while(true){
    cin>>w>>h;
    if(w==0 && h==0)break;
    rep(i, MAX_LEN)rep(j, MAX_LEN){
      board[i][j]=0;
      used[i][j]=false;
    }

    rep(i, h)rep(j, w)cin>>board[i][j];

    int result=0;
    rep(i, h)rep(j, w){
      if(board[i][j]==1 && !used[i][j])result++;
      dfs(j, i);
    }

    // rep(i, h){
    //   rep(j, w)cout<<board[i][j];
    //   cout<<endl;
    // }

    cout<<result<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define debug(x) cerr << #x << ": " << x << endl;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

ll c[100][100];
bool b[100][100];
ll dx[8] = {0, 0, 1, 1, 1, -1, -1, -1}, dy[8] = {1, -1, 0, 1, -1, 0, -1, 1};
ll w, h;

void dfs(ll x, ll y) {
    b[x][y] = true;
    rep(i, 8) {
        ll nx = x + dx[i], ny = y + dy[i];
        if (0 <= nx && nx < h && 0 <= ny && ny < w && c[nx][ny] == 1) {
            if (b[nx][ny]) continue;
            dfs(nx, ny);
        }
    }
}

int main() {
    vector<ll> vec;
    while(cin >> w >> h) {
        if(w == 0 && h == 0) {
            for(auto v : vec) cout << v << endl;
            return 0;
        }

        rep(i, h) rep(j, w) cin >> c[i][j];
        rep(i, h) rep(j, w) b[i][j] = false;

        ll ans = 0;
        rep(i, h) rep(j, w) {
            if (c[i][j] == 1 && b[i][j] == false) {
                ans++;
                dfs(i, j);
            }
        }
        vec.push_back(ans);
    }
}

C++: //include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <queue>

using namespace std;

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

bool tab[60][60];
int dx[] = {-1,0,1,-1,1,-1,0,1};
int dy[] = {-1,-1,-1,0,0,1,1,1};
void dfs(int x, int y){
  if(!tab[y][x]) return;
  tab[y][x] = false;
  for(int i=0;i<8;++i)
	dfs(x+dx[i], y+dy[i]);
}

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

  while(true){
	int W, H; cin >> W >> H;
	if(W == 0) break;
	int ans = 0;
	fill((bool*)tab, (bool*)tab+60*60, false);
	REP(y,H) REP(x,W){
	  int c; cin >> c;
	  tab[y+1][x+1] = c == 1;
	}
	for(int y=1;y<=H;++y)
	  for(int x=1;x<=W;++x)
		if(tab[y][x]){
		  ++ans;
		  dfs(x,y);
		}
	cout << ans << endl;
  }
  
  return 0;
}