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;

int visited[50][50];
const int offset[8][2] = {
    {1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}
};
int grid[50][50];

void dfs(int i, int j, int h, int w){
    for (int k = 0; k < 8; k++) {
        int ii = i + offset[k][0], jj = j + offset[k][1];
        if (ii < 0 || ii >= h || jj < 0 || jj >= w) {
            continue;
        }
        if (!visited[ii][jj] && grid[ii][jj]) {
            visited[ii][jj] = 1;
            dfs(ii, jj, h, w);
        }
    }
}


int main(int argc, char *argv[])
{
    
    cin.tie(0);
    ios::sync_with_stdio(false);

    int w, h;

    
    while (cin >> w >> h, w != 0 && h != 0) {
        memset(visited, 0, 50*50*sizeof(int));
        memset(grid, -1, 50*50*sizeof(int));
        
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                cin >> grid[i][j];
            }
        }
        int cnt = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (visited[i][j] || !grid[i][j]) {
                    continue;
                }
                dfs(i, j, h, w);
                cnt++;
            }
        }
        cout << cnt << endl;
    }
    
    return 0;
}

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

int w,h;
int c[50][50];
vector<vector<int> > visited;
int dx[]={1,1,0,-1,-1,-1,0,1};
int dy[]={0,-1,-1,-1,0,1,1,1};

bool dfs(int x,int y){
    if(visited[x][y]) return false;
    visited[x][y]=1;

    for(int d=0;d<8;d++){
        int i=x+dx[d];
        int j=y+dy[d];
        if(0<=i&&i<h&&0<=j&&j<w){
            if(c[i][j]){
                dfs(i,j);
            }  
        }
    }
    return true;
}

int main(){
    visited.assign(50,vector<int>(50,0));
    cin>>w>>h;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>c[i][j];
        }
    }
    int cnt=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(c[i][j]){
                cnt+=dfs(i,j);
            }
        }
    }

    cout<<cnt<<endl;
    return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
int dx[8]={1,1,1 ,0, 0,-1,-1,-1};
int dy[8]={0,1,-1,1,-1, 1, 0,-1};
int c[55][55];
int w,h;
int res;
void paint(int i,int j,int k){
  if(i<0||j<0)return;
  else if(i>=h||j>=w) return;
  else if(c[i][j]!=1) return;
  c[i][j]=k;
  for(int l=0;l<8;l++) paint(i+dx[l],j+dy[l],k);
  return;
}
void count(int i,int j){
  res++;
  paint(i,j,res);
  return;
}
int main(void){
  int i,j;
  while(scanf("%d %d",&w,&h),w|h){
    res=1;
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
        scanf("%d",&c[i][j]);
      }
    }
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
        if(c[i][j]==1){
          count(i,j);
        }
      }
    }
    printf("---\n");
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
        printf("%d",c[i][j]);
      }
      printf("\n");
    }
    printf(":%d\n",res-1);
  }
  return 0;
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160

#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;

int w, h;
bool F[60][60];

struct point {
  int x, y;
};

void init() {
  fill(&F[0][0], &F[0][0]+60*60, 0);
  for (int i=1; i<=h; i++) {
    for (int j=1; j<=w; j++) {
      int temp;
      scanf("%d", &temp);
      F[i][j] = (bool) temp;
    }
  }
}

int solve() {
  int ans = 0;
  for (int i=1; i<=h; i++) {
    for (int j=1; j<=w; j++) {
      if (F[i][j]) {
	stack<point> S;
	point p = { i, j };
	S.push(p);
	while (! S.empty()) {
	  p = S.top();
	  S.pop();
	  F[p.x][p.y] = false;
	  for (int i=-1; i<=1; i++) {
	    for (int j=-1; j<=1; j++) {
	      point q = { p.x + i , p.y + j };
	      if (F[q.x][q.y]) {
		S.push(q);
	      }
	    }
	  }
	}
	ans++;
      }
    }
  }
  return ans;
}

int main() {
  while(scanf("%d%d", &w, &h) != EOF && w && h) {
    init();
    printf("%d\n", solve());
  }
}

C++: #include <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0,1,-1,1,-1};
const int dx[] = {0,-1,0,1,1,-1,-1,1};

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

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

ll w,h;
vector<vector<ll>> v;
void dfs(int x, int y){
  v[x][y] = 0;
  rep(i,8){
    int nx = x+dx[i],ny = y+dy[i];
    if(value(nx,ny,w,h) && v[nx][ny]){
      dfs(nx,ny);
    }
  }
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  while(cin >> w >> h && w && h){
    v = vector<vector<ll>>(w,vector<ll>(h,0));
    rep(i,h){
      rep(j,w){
        cin >> v[j][i];
      }
    }
    ll ans = 0;
    rep(i,h){
      rep(j,w){
        if(v[j][i]){
          ++ans;
          dfs(j,i);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}