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 P = pair<int, int>;
using PL = pair<ll, ll>;
// Welcome to my source code!

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

void dfs(int cx, int cy) {
    for (int dx = -1; dx <= 1; dx++) {
        for (int dy = -1; dy <= 1; dy++) {
            int nx = cx + dx, ny = cy + dy;
            if (0 <= nx && nx < h && 0 <= ny && ny < w && c[nx][ny] != 0) {
                c[nx][ny] = 0;
                dfs(nx, ny);
            }
        }
    }
}

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];
        int ans = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (c[i][j] == 0) continue;
                dfs(i, j);
                ans++;
            }
        }
        cout << ans << endl;
    }
}

C++: #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<map>
#include<tuple>
#include<bitset>
#include<iomanip>
#define rep(index,num) for(int index=0;index<num;index++)
#define rep1(index,num) for(int index=1;index<=num;index++)
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define kaigyo cout<<endl
#define eps 1e-15
#define mp(a1,a2) make_pair(a1,a2)
typedef long long ll;
using namespace std;
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl=1e+18+1;
int INF=1e+9+1;
struct UnionFind{
  vector<int> par;
  vector<int> sizes;
  UnionFind(int n) : par(n), sizes(n, 1) {
    rep(i,n) par[i] = i;
  }
  int root(int x) {
    if (x == par[x]) return x;
    return par[x] = root(par[x]);
  }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return;
    if (sizes[x] < sizes[y]) swap(x, y);
    par[y] = x;
    sizes[x] += sizes[y];
  }
  bool same(int x, int y) {
    return root(x) == root(y);
  }
  int size(int x) {
    return sizes[root(x)];
  }
};
int main(){
	//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp
	while(1){
		int w,h;
		scan(w);scan(h);
		if(w==0&&h==0) break;
		int A[51][51];
		rep(i,h){
			rep(j,w){
				scan(A[i][j]);
			}
		}
		UnionFind uf(h*w);
		rep(i,h){
			rep(j,w-1){
				if(A[i][j]==1&&A[i][j+1]==1) uf.unite(i*w+j,i*w+j+1);
			}
		}
		rep(i,h-1){
			rep(j,w){
				if(A[i][j]==1&&A[i+1][j]==1) uf.unite(i*w+j,(i+1)*w+j);
			}
		}
		rep(i,h-1){
			rep(j,w-1){
				if(A[i][j]==1&&A[i+1][j+1]==1) uf.unite(i*w+j,(i+1)*w+j+1);
				if(A[i+1][j]==1&&A[i][j+1]==1) uf.unite((i+1)*w+j,i*w+j+1);
			}
		}
		int ans=0;
		rep(i,h){
			rep(j,w){
				if(A[i][j]==0) continue;
				if(uf.root(i*w+j)==i*w+j) ans++;
			}
		}
		prin(ans);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double pi=3.141592653589793;
typedef unsigned long long ull;
typedef long double ldouble;
const ll INF=1e18;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)

// AOJ How Many Islands? 1160
int h, w;
vector<vector<bool> > square(50, vector<bool>(50));

// 幅優先探索
void dfs(int x, int y){

    // 一度通過した部分は塀とする
    square.at(x).at(y) = 0;

    rep2(dx, -1, 2){
        rep2(dy, -1, 2){
            int nx = x + dx;
            int ny = y + dy;
            if(0 <= nx && nx < w && 0 <= ny && ny < h && square.at(nx).at(ny) == 1) {
                dfs(nx, ny);
            }
        }
    }

    return;
}

int main(){
    h = 1;
    w = 1;
    while(true){
        cin >> w >> h;
        if(h == 0 && w == 0) break;
        int answer = 0;
    
        rep(i, h){
            rep(j, w) {
                int a;
                cin >> a;
                square.at(j).at(i) = a;
            }
        }
        rep(i, h){
            rep(j, w){
                if(square.at(j).at(i) == 1) {
                    dfs(j, i);
                    //cout << "x:" << j << " y:" << i << endl;
                    answer++;
                    //rep(i, h) {rep(j, w) {cout << square.at(j).at(i);} cout << "\n";} cout << "------\n";
                }
            }
        }
        cout << answer << endl;
    }
    
    


}

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

// ?????¬???????????¬??????
#pragma region MACRO
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define PED cout << "\n"
#define rep(i,n) for(int i=0; i<(int)n; ++i)
#define REP(i,x,n) for(int i=x; i<(int)n; ++i)
#define repi(i,n) for(int i=0; i<=(int)n; ++i)
#define REPI(i,x,n) for(int i=x; i<=(int)n; ++i)
#define ILP while(true)
#define FOR(i,c) for(__typeof((c).begin())!=(c).begin(); i!=(c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#pragma endregion

// ???
#pragma region TYPE_DEF
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef pair<string, int> psi;
typedef pair<int, string> pis;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long double> vld;
typedef vector<long> vl;
typedef vector<long long> vll;
typedef vector<string> vs;
#pragma endregion

// Effective std
#pragma region ESTD
template<typename C, typename T> int count(C& c, T t) { return count(ALL(c), t); }
template<typename C, typename F> int count_if(C& c, F f) { return count_if(ALL(c), f); }
template<typename C, typename T> void erase(C& c, T t) { remove(ALL(c), t), c.end(); }
template<typename C> void remove(vector<C>& c, unsigned int index) { c.erase(c.begin()+index); }
template<typename C, typename T, typename U> void replace(C& c, T t, U u) { replace(ALL(c), t, u); }
template<typename C, typename F, typename U> void replace_if(C& c, F f, U u) { (ALL(c), f, u); }
template<typename C> void reverse(C& c) { reverse(ALL(c)); }
template<typename C> void sort(C& c) { sort(ALL(c)); }
template<typename C, typename Pred> void sort(C& c, Pred p) { sort(ALL(c), p); }
#pragma endregion

// ?????°
#pragma region CONST_VAL
constexpr int PI = (2*acos(0.0));
constexpr int EPS = (1e-9);
constexpr int MOD = (int)(1e9+7);
constexpr int INF = 100000000;
#pragma endregion

int w, h;
int a[51][51];
int dx[]{1,-1,0,0,1,-1,1,-1};
int dy[]{0,0,1,-1,1,-1,-1,1};

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

int main()
{
    while(cin>>w>>h, w) {
        rep(i,h) rep(j,w) a[i][j] = 0;

        rep(i,h) {
            rep(j,w) {
                cin >> a[i][j];
            }
        }

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