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 <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define P pair<int, int>
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
//#define int long long
const int MAX_HW = 55;

int H, W, v[MAX_HW][MAX_HW];
int my[] = {0, 0, 1, -1, 1, 1, -1, -1};
int mx[] = {1, -1, 0, 0, 1, -1, 1, -1};

void dfs(int y, int x){
  REP(i, 8){
    int ny = y + my[i], nx = x + mx[i];
    if(ny < 0 || nx < 0 || ny >= H || nx >= W || v[ny][nx] == 0) continue;
    v[ny][nx] = 0;
    dfs(ny, nx);
  }
}

int main() {
  while(cin >>W >>H && W){
    int ans = 0;
    REP(i, H) REP(j, W) cin >>v[i][j];
    REP(i, H){
      REP(j, W){
        if(v[i][j] == 1){
          ++ans;
          v[i][j] = 0;
          dfs(i, j);
        }
      }
    }
    cout <<ans <<endl;
  }
  return 0;
}

C++: #include <stdio.h>
int h, w;
void solve(int a[50][50],int x,int y){
	int i, j;
	a[y][x] = 0;
	for(i = y - 1;i <= y + 1;i++){
		for(j = x - 1;j <= x + 1;j++)
			if(0 <= i && i < h && 0 <= j && j < w && a[i][j]) solve(a,j,i);
	}
	return;
}

int main(void){
	int i, j, flag[50][50], count;
	scanf("%d%d",&w,&h);
	while(w != 0 || h != 0){
		for(i = 0;i < h;i++)
			for(j = 0;j < w;j++) scanf("%d",&flag[i][j]);
		count = 0;
		for(i = 0;i < h;i++){
			for(j = 0;j < w;j++){
				if(flag[i][j]){
					count++;
					solve(flag,j,i);
				}
			}
		}
		printf("%d\n",count);
		scanf("%d%d",&w,&h);
	}
	return 0;
}

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

int H, W, C[50][50];

void dfs(int a, int b) {
    C[a][b] = 0;
    for (int i = -1; i <= 1; ++i) {
        for (int j = -1; j <= 1; ++j) {
            int na = a + i, nb = b + j;
            if (na >= 0 && na < H && nb >= 0 && nb < W && C[na][nb] == 1) dfs(na, nb);
        }
    }
    return;
}

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 >> C[i][j];
        }

        int ans = 0;
        for (int i = 0; i < H; ++i) {
            for (int j = 0; j < W; ++j) {
                if (C[i][j]) {
                    dfs(i, j);
                    ++ans;
                }
            }
        }

        cout << ans << endl;
    }
    
    return 0;
}

C++: #include<cstdio>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
 
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1001001001
#define LLINF 1001001001001001001
#define mp make_pair
#define LLIandI pair<long long int , int>
#define ll long long

int main(){
    while(1){
    int W,H;
    scanf("%d %d\n",&W,&H);
    if(W==0)break;
    int c[100][100];//W,H<=50
    bool f[100][100];
    rep(i,H){
        rep(j,W){
            scanf("%d",&c[i][j]);
            f[i][j]=false;
        }
    }
    queue< pair<int,int> > q;
    int count=0;
    rep(i,H)rep(j,W){
        if(f[i][j])continue;
        if(c[i][j]==0)continue;
        count++;
        //printf("x=%d y=%d",i,j);
        q.push( mp(i,j) );
        while(!q.empty()){
            pair<int,int> p=q.front();
            q.pop();
            int py=p.first;int px=p.second;
            if(f[py][px])continue;
            if(c[py][px]==0)continue;
            f[py][px]=true;
            if(py>0)q.push( mp(py-1,px) );
            if(py<H-1)q.push( mp(py+1,px) );
            if(px>0)q.push( mp(py,px-1) );
            if(px<W-1)q.push( mp(py,px+1) );
            if(py>0&&px>0)q.push( mp(py-1,px-1));
            if(py>0&&px<W-1)q.push( mp(py-1,px+1));
            if(py<H-1&&px>0)q.push( mp(py+1,px-1));
            if(py<H-1&&px<W-1)q.push( mp(py+1,px+1));

        }
    }
    
    printf("%d\n",count);
    }
}

C++: #include <bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;

int dx8[8] = { -1,-1,-1, 0, 1, 1, 1, 0 };
int dy8[8] = { -1, 0, 1, 1, 1, 0,-1,-1 };

vector<vector<int>>as;
int w,h;

void check(int y, int x) {
    for (int i = 0; i < 8; ++i) {
        int nx = x + dx8[i];
        int ny = y + dy8[i];
        if (as[ny][nx]) {
            as[ny][nx] = 0;
            check(ny, nx);
        }
    }
}

int main() {
    while (1) {
        cin >> w >> h;
        if (!w)break;
        vector<vector<int>>as2(h+2);
        as = as2;
        for (int i = 0; i <= h+1; ++i) {
            as[i].emplace_back(0);
            if (i != 0 && i != h + 1) {
                for (int j = 0; j < w; ++j) {
                    int c; cin >> c;
                    as[i ].emplace_back(c);
                }
            }
            else {
                for (int j = 0; j < w; ++j) {
                    as[i].emplace_back(0);
                }
            }
            
            as[i].emplace_back(0);
        }
        int ans = 0;
        for (int i = 1; i <= h; ++i) {
            for (int j = 1; j <= w; ++j) {
                if (as[i][j]) {
                    ans++;
                    check(i, j);
                }
            }
        }
        cout << ans << endl;
    }
}

C++: #include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <stack>
#include <set>
#include <vector>

using namespace std;

#define all(c) ((c).begin()), ((c).end())
#define dump(c) cerr << "> " << #c << " = " << (c) << endl;
#define iter(c) __typeof((c).begin())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define REP(i, a, b) for (int i = a; i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define mp make_pair
#define fst first
#define snd second
#define pb push_back

typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef pair<int,int> pii;

const int INF = 1 << 29;
const double EPS = 1e-10;

int W, H;
int field[52][52];

void dfs(int x, int y) {
  field[y][x] = 0;

  REP(dx, -1, 2) REP(dy, -1, 2) {
    int tx = x + dx;
    int ty = y + dy;
    if (tx < 0 || tx >= W || ty < 0 || ty >= H) continue;
    if (field[ty][tx]) dfs(tx, ty);
  }
}

int main() {
  for(; cin >> W >> H, (W | H) != 0; ) {

    rep(i, H) rep(j, W) cin >> field[i][j];

    int ans = 0;
    rep(i, H) rep(j, W) if (field[i][j]) {
      dfs(j, i);
      ans++;
    }

    cout << ans << endl;
  }
  
  
  return 0;
}