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>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, e) for (int i = (int)(s); i <= (int)(e); i++)
#define printYesNo(is_ok) puts(is_ok ? "Yes" : "No");
#define SORT(v) sort(v.begin(), v.end());
#define RSORT(v) sort(v.rbegin(), v.rend());
#define REVERSE(v) reverse(v.begin(), v.end());


int ans = 0;
vector<vector<int> > island_map;
void dfs(vector<vector<int> > field, pair<int, int> init_pos)
{
  int dirx[] = {1, 1, 1, 0, 0, -1, -1, -1};
  int diry[] = {1, 0, -1, 1, -1, 1, 0, -1};

  int h = field.size();
  int w = field[0].size();
  stack<pair<int, int> > pos_stack;
  pos_stack.push(init_pos);

  while (pos_stack.size())
  {
    pair<int, int> pos = pos_stack.top();
    pos_stack.pop();
    int pos_x = pos.first;
    int pos_y = pos.second;
    rep(i, 8)
    {
      int next_x = pos_x + dirx[i];
      int next_y = pos_y + diry[i];

      if (0 <= next_x && next_x < h && 0 <= next_y && next_y < w && island_map[next_x][next_y] == 0 && field[next_x][next_y] == 1)
      {
        island_map[next_x][next_y] = ans;
        pos_stack.push(make_pair(next_x, next_y));
      }
    }
  }
}

int main()
{
  while(true)
  {
    int w, h;
    cin >> w >> h;
    if(w == 0 && h == 0)
      return 0;

    ans = 0;    
    vector<vector<int> > field(h, vector<int>(w, 0));
    island_map = vector<vector<int> >(h, vector<int>(w, 0));
    rep(i, h) rep(j, w)
    {
      cin >> field[i][j];
    }

    rep(i, h) rep(j, w)
    {
      if(island_map[i][j] == 0 && field[i][j] == 1)
      {
        ans++;
        dfs(field, make_pair(i, j));
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;

#define rep(i,N) for(int i=0;i<(int)(N);++i)
#define rrep(i,N) for(int i=(int)(N)-1;i>=0;--i)
#define debug(x) cout<<#x<<"="<<x<<endl;
constexpr ll MOD=1000000007;
constexpr ll INF=1LL<<60;
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;}
template<typename T> void fail(T v){cout << v << endl;exit(0);}
//template end
int W,H;
bool inside(int h,int w){return 0<=h&&h<H&&0<=w&&w<W;}
int gh[]={1,-1,0,0,1,1,-1,-1},gw[]={0,0,1,-1,-1,1,-1,1},use[51][51],G[51][51];
void dfs(int h,int w){
    rep(k,8){
        int i=gh[k]+h,j=gw[k]+w;
        if(!inside(i,j)||use[i][j]||G[i][j]==0)continue;
        use[i][j]=1;
        dfs(i,j);
    }
}
void solve(){
    while((cin>>W>>H,W|H)){
        rep(i,51)rep(j,51)use[i][j]=0;
        rep(i,H)rep(j,W)cin>>G[i][j];
        int res=0;
        rep(i,H)rep(j,W)if(!use[i][j]&&G[i][j]){dfs(i,j);res++;}
        cout<<res<<endl;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    solve();
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std ;
int H , W ;
int board[550][550] ;

void dfs (int x , int y ){
  board[x][y] = 0 ;
  for(int i = -1 ; i < 2 ; i++ )
    for(int j = -1 ; j < 2 ; j++ ){
      int nx = x + i , ny = y + j ;
      if( nx >= 0 && ny >= 0 && nx < H && ny < W &&board[nx][ny] == 1) dfs(nx,ny);
    }
}

int main () {
  while(1){
    int ans = 0 ;
    cin >> W >> H ;
    if(H==0&&W==0) break ;
    for(int i = 0 ; i < H ; i++ ){
      for(int j = 0 ; j < W ; j++ ){
        cin >> board[i][j] ;
      }
    }
    for(int i = 0 ; i < H ; i++ ){
      for(int j = 0 ; j < W ; j++ ){
        if(board[i][j]==1){
          dfs(i,j) ;
          ans ++ ;
        }
      }
    }
    cout << ans << 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)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;
  }
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
int Map[52][52];
bool used[52][52];
const int dir[][2] = { {-1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 }, {0, -1 }, {-1, -1 } };
int w, h;

void dfs (int row, int col )
{
	used[row][col] = true;
	rep (k, 8 ){
		int nr = row + dir[k][0];
		int nc = col + dir[k][1];
		if (nr < 0 || nc < 0 || nr >= h || nc >= w || Map[nr][nc] == 0 || used[nr][nc] ) continue;
		dfs (nr, nc );
	} // end rep
}

int main()
{
	while (scanf ("%d %d", &w, &h ) && w && h ){
		memset (used, false, sizeof (used ) );
		rep (i, h ){
			rep (j, w ){
				scanf (" %d", Map[i]+j );
			} // end rep
		} // end rep
		int cnt = 0;
		rep (i, h ){
			rep (j, w ){
				if (Map[i][j] == 1 && !used[i][j] ){
					dfs (i, j );
					cnt++;
				} // end if
			} // end rep
		} // end rep
		printf ("%d\n", cnt );
	} // end loop
		
	return 0;
}