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;
using ll = long long;
using ull = unsigned long long;
#define REP(i,a) for(int i=0;i<(a);i++)
#define MOD 1000000007

int main(){
  while(1){
    int h,w;
    cin>>w>>h;
    if(h==0&&w==0) break;
    int a[h][w];
    bool used[h][w];
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        cin>>a[i][j];
        used[i][j]=0;
      }
    }
    int ans=0;
    queue<pair<int,int>> q;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        if(a[i][j]&&!used[i][j]){
          //cout<<i<<" "<<j<<endl;
          ans++;
          q.push(make_pair(i,j));
          while(!q.empty()){
            pair<int,int> p=q.front();
            q.pop();
            int x=p.first, y=p.second;
            if(used[x][y]) continue;
            used[x][y]=1;
            for(int i=-1;i<2;i++){
              for(int j=-1;j<2;j++){
                if(x+i==-1||x+i==h) continue;
                if(y+j==-1||y+j==w) continue;
                if(a[x+i][y+j]&&!used[x+i][y+j]){
                  q.push(make_pair(x+i,y+j));
                }
              }
            }
          }
        }
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
???
using ll=long long;
#define int ll
???
#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)
???
int read(){
	int i;
	scanf("%lld",&i);
	return i;
}
???
int land[100][100]={0};
int dy[]={-1,-1,-1,0,0,1,1,1};
int dx[]={-1,0,1,-1,1,-1,0,1};
int cnt=0;
???
void isLand(int i,int j){
    if(land[i][j]==1){
    	land[i][j] = 0;
		REP(k,8){
			int newy=i+dy[k];
			int newx=j+dx[k];
			isLand(newy,newx);
		}
    }
}
???
signed main(){
	int w,h;
	while( w=read(),h=read(),w||h){
		cnt=0;
		
		REP(i,100){
			REP(j,100){
				land[i][j]=0;
			}
		}
		FOR(i,1,w+1){
			FOR(j,1,h+1){
				land[i][j]=read();
			}
		}
		
		FOR(i,1,w+1){
			FOR(j,1,h+1){
				if(land[i][j]==1){
					cnt++;
					isLand(i,j);
				}
			}
		}
		
		cout << cnt << endl;
	}
	
}

C++: #include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
int H,W;
bool fld[50][50];
int main(){
	queue<pair<int,int> > p;
	while(cin>>W>>H,W,H){
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				cin>>fld[j][i];
			}
		}
		int cnt=0;
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				if(fld[j][i]){
					cnt++;
					fld[j][i]=0;
					p.push(make_pair(j,i));
					while(!p.empty()){
						int x=p.front().first,y=p.front().second;
						fld[x][y]=0;
						p.pop();
						for(int dx=-1;dx<=1;dx++)for(int dy=-1;dy<=1;dy++){
							int nx=x+dx,ny=y+dy;
							if(nx>=0 && nx<W && ny>=0 && ny<H && fld[nx][ny]){
								p.push(make_pair(nx,ny));
							}
						}
					}
				}
			}
		}
		cout<<cnt<<endl;
	}
}

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

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

#define MAX 1000
#define EPS (1e-10)
using namespace std;

typedef unsigned int uint;
typedef long long ll;

int w,h;
int  b[64][64];

void clustering(int x, int y, char num){
  if(b[y][x] == 0) return;

  b[y][x] = 0;

  REP(i,3) REP(j,3){
    int xx = x + j - 1;
    int yy = y + i - 1;
    if(xx >= 0 && xx <= w-1 && yy >= 0 && yy <= h-1)
      clustering(xx, yy, num);
  }
}

int main(){
  while(scanf("%d%d",&w,&h), w+h){
    int cl = 0;
    REP(i,h) REP(j,w) scanf("%d", &b[i][j]);
    REP(i,h) REP(j,w){
      if(b[i][j] == 1){
        clustering(j,i,b[i][j]);
        cl++;
      }
    }
    printf("%d\n",cl);
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include<iterator>
#include<complex>
#include<stack>
using namespace std;
typedef  long long int lint;
typedef pair<int, int> P;
typedef pair<lint, lint> LLP;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define sort(v) sort((v).begin(), (v).end())
#define reverse(v) reverse((v).begin(), (v).end())
/*
vector<char>al(26);
al = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
vector<char>AL(26);
AL = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
*/

int main() {
	int W, H;
	vector<int>dx(8);
	vector<int>dy(8);
	dx = { 1,1,0,-1,-1,-1,0,1 };
	dy = { 0,1,1,1, 0,-1,-1,-1};
	while (cin >> W >> H, W != 0) {
		vector<vector<char>>field(H, vector<char>(W));
		rep(i, H) {
			rep(j, W) {
				cin >> field[i][j];
			}
		}
		int cnt = 0;
		rep(sy, H) {
			rep(sx, W) {
				if (field[sy][sx] == '1') {
					cnt++;
					queue<P>que;
					que.push({ sx,sy });
					field[sy][sx] = '0';
					while (!que.empty()) {
						int x = que.front().first;
						int y = que.front().second;
						que.pop();
						rep(i, 8) {
							int xx = x + dx[i];
							int yy = y + dy[i];
							if (xx >= 0 && xx < W && yy >= 0 && yy < H && field[yy][xx] == '1') {
								que.push({ xx,yy });
								field[yy][xx] = '0';
							}
						}
					}
				}
			}
		}
		cout << cnt << endl;
	}
}