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 FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) FOR(i,0,n)
#define repr(i,n) for(int i=(n)-1;0<=i;--i)
#define each(e,v) for(auto&& e:(v))
#define all(v) begin(v),end(v)
#define dump(x) cerr<<#x<<": "<<(x)<<endl
#define INF 100000000
#define IINF 9999999999999;
using vint = vector<int>;
using ll = long long;
using vll = vector<ll>;
template <class T> void chmin(T& a, const T& b) { a = min(a, b); }
template <class T> void chmax(T& a, const T& b) { a = max(a, b); }

bool earth[51][51];

void erase_island(int h, int w) {
    earth[h][w] = false;
    //cout << "# " << h << " " << w << endl;
    if(earth[h - 1][w - 1]) erase_island(h - 1, w - 1);
    if(earth[h - 1][w]) erase_island(h - 1, w);
    if(earth[h - 1][w + 1]) erase_island(h - 1, w + 1);
    if(earth[h][w + 1]) erase_island(h, w + 1);
    if(earth[h + 1][w + 1]) erase_island(h + 1, w + 1);
    if(earth[h + 1][w]) erase_island(h + 1, w);
    if(earth[h + 1][w - 1]) erase_island(h + 1, w - 1);
    if(earth[h][w - 1]) erase_island(h, w - 1);
}

int main() {
    // ofstream outputfile("output.txt");
    int w, h;
    while(cin >> w >> h, w + h) {
        if(w == 0 && h == 0) break;
        int temp;
        rep(i, h) {
            rep(j, w) {
                cin >> temp;
                if(temp == 0) earth[i][j] = false;
                else earth[i][j] = true;
            }
        }

        int ans = 0;
        rep(i, h) {
            rep(j, w) {
                if(earth[i][j]) {
                    erase_island(i, j);
                    ++ans;
                }
            }
        }

        cout << ans << endl;
    }

    return 0;
}

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

int W, H; 
vector<vector<int>> C(55, vector<int>(55));

void dfs(int h, int w){
  C[h][w] = 0;

  for(int dx=-1; dx<=1; dx++){
    for(int dy=-1; dy<=1; dy++){
      if(0 <= h+dx && h+dx < H && 0 <= w+dy && w+dy < W){
        if(C[h+dx][w+dy] == 1){
          dfs(h+dx, w+dy);
        }
      }
    }
  }
}

int main(){ 
  while(true){
    cin >> W >> H;
    if(W == 0 && H == 0) break;

    for(int h = 0; h<H; h++){
      for(int w =  0; w<W; w++){
        cin >> C[h][w];
      }
    }

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

    cout << ans << endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <cmath>
using namespace std;
#define MOD 1000000007
#define INF 1<<30
#define LINF (ll)1<<62
#define MAX 510000
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(ll i=(a); i<(b); i++)
#define all(x) (x).begin(),(x).end()
#define uni(q) unique(all(q)),q.end()
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> P;
typedef vector<pair<ll, ll>> vpl;

ll dy[8] = {0,1,0,-1,1,-1,1,-1};
ll dx[8] = {1,0,-1,0,1,-1,-1,1};
ll cnt;
ll visit[55][55];
ll c[55][55];
ll h,w;

bool in(ll y, ll x){
    return 0 <= y && y < h && 0 <= x && x < w;
}

void dfs(ll y, ll x){
    if(c[y][x]==0) return;
    if(visit[y][x]>0) return;
    visit[y][x] = cnt;
    rep(i,8){
        ll ny = y+dy[i];
        ll nx = x+dx[i];
        if(!in(ny, nx)) continue;
        else{
            dfs(ny,nx);
        }
    }
}

int main(){
    while(1){
        cin >> w >> h;
        if(w==0 && h==0) break;
        cnt = 0;
        rep(i,h) rep(j,w) cin >> c[i][j];
        rep(i,h) rep(j,w) visit[i][j] = 0;
        rep(i,h){
            rep(j,w){
                if(c[i][j]==0) continue;
                else{
                    if(visit[i][j] > 0) continue;
                    else{
                        cnt++;
                        dfs(i,j);
                    }
                }
            }
        }
        cout << cnt << endl;
    }
}

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

using namespace std;
#define INF 1000007
#define MOD 1000000007
#define int long long
#define maxn 101
#define maxh 1000
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)
const int dx[4] = {0, 1, -1, 0}, dy[4] = {1, 0, 0, -1};
typedef std::pair<int, int> P;

vector<bool> prime(maxn + 1, true); //素数テーブル
void prime_table(void) {
    prime[0] = false;
    prime[1] = false;
    for (int i = 2; i * i <= maxn; i++) {
        if (!prime[i]) {
            continue;
        }
        for (int j = i + i; j <= maxn; j += j + i) {
            prime[j] = false;
        }
    }
}

int gcd(int a, int b) {
    return b ? gcd(b, a % b) : a;
}

int lcm(int a, int b) { return a / gcd(a, b) * b; }

bool IsPrime(int num) //素数判定
{
    if (num < 2)
        return false;
    else if (num == 2)
        return true;
    else if (num % 2 == 0)
        return false;

    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2) {
        if (num % i == 0) {
            return false;
        }
    }

    return true;
}

vector<int> divisor(int n) //約数列挙
{
    vector<int> ret;
    for (int i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i - 1);
            if (i * i != n) {
                ret.push_back((n / i) - 1);
            }
        }
    }
    sort(ret.begin(), ret.end());
    return (ret);
}

int a, b;

void f(int x) {
    while (x) {
        a++;
        b = x;
        x /= 10;
    }
}

int w, h;
int c[50][50];

void dfs(int x, int y) {
    c[y][x] = 0;
    for (int i = -1; i <= 1; i++) {
        for (int j = -1; j <= 1; j++) {
            int nx = x + i;
            int ny = y + j;
            if (nx >= 0 && nx < w && ny >= 0 && ny < h && c[ny][nx] == 1) {
                dfs(nx, ny);
            }
        }
    }
}

signed main() {
    while (true) {
        cin >> w >> h;
        if (w == 0 && h == 0)break;
        rep(i, h) {
            rep(j, w) {
                cin >> c[i][j];
            }
        }
        int ans = 0;
        rep(i, w) {
            rep(j, h) {
                if (c[j][i] == 1) {
                    dfs(i, j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
}