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++: //http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp
//How Many Islands?
#include <bits/stdc++.h>
#if LOCAL
#include "dump.hpp"
#else
#define dump(...)
#endif
using namespace std;
using ll=long long;
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
template<class T>bool chmax(T &a, const T &b) {if (a<b) { a=b; return 1; } return 0;}
template<class T>bool chmin(T &a, const T &b) {if (b<a) { a=b; return 1; } return 0;}

//2重配列上でdfs
bool solve2(){
    ll w,h; cin>>w>>h;
    if(w==0&&h==0)return false;//終わり
    vector<vector<ll>> chw(h,vector<ll>(w));
    FOR(y,0,h){
        FOR(x,0,w){
            ll c; cin>>c;
            chw.at(y).at(x)=c;
        }
    }

    auto available_node = [&](ll x,ll y)->bool{
        if(x<0||x>=w)return false;
        if(y<0||y>=h)return false;
        return chw.at(y).at(x);
    };

    function<void(ll,ll)> dfs = [&](ll x,ll y){
        if(not available_node(x,y)) return;
        chw.at(y).at(x)=0;//訪問済、flag落す
        pair<ll,ll> ofstbl[] = {
            {0,1},{0,-1},{1,0},{-1,0},
            {1,1},{1,-1},{-1,1},{-1,-1},        
        };
        for(const auto&p:ofstbl){
            dfs(x+p.first,y+p.second);
        } 
    };
    ll ans=0;
    FOR(y,0,h){
        FOR(x,0,w){
            if(available_node(x,y)){
                ++ans;
                dfs(x,y);
            }
        }
    }
    cout<<ans<<endl;

    return true;
}
int main(){
    while(solve2()){
    }
    return 0;
}

//template graph使ってるけど冗長
template< typename T >
struct edge {
	int src, to;
	T cost;
	edge(int _to, T _cost) : src(-1), to(_to), cost(_cost) {}
	edge(int _src, int _to, T _cost) : src(_src), to(_to), cost(_cost) {}
	edge &operator=(const int &x) {
		to = x;
		return *this;
	}
	operator int() const { return to; }
};

template< typename T>
struct node {
	vector<edge<T>> edges;
    bool flag=false;;
};

template< typename T >
using Edges = vector< edge< T > >;
template< typename T >
using WeightedGraph = vector< node< T > >;
using UnWeightedGraph = vector< vector< int > >;
template< typename T >
using Matrix = vector< vector< T > >;

bool solve(){
    ll w,h; cin>>w>>h;
    if(w==0&&h==0)return false;//終わり
    vector<vector<ll>> chw(h,vector<ll>(w));
    ll node_num=0;
    FOR(y,0,h){
        FOR(x,0,w){
            ll c; cin>>c;
            if(c==1)++node_num;
            chw.at(y).at(x)=c;
        }
    }

    auto pos2id = [&](ll x,ll y)->ll{
        return y*w+x;
    };
    auto available_node = [&](ll x,ll y)->bool{
        if(x<0||x>=w)return false;
        if(y<0||y>=h)return false;
        return chw.at(y).at(x);
    };
    ll node_max=w*h;
    WeightedGraph<ll> graph(node_max);
    pair<ll,ll> ofstbl[] = {
        {0,1},{0,-1},{1,0},{-1,0},
        {1,1},{1,-1},{-1,1},{-1,-1},        
    };
    FOR(y,0,h){
        FOR(x,0,w){
            if (available_node(x,y)) {
                ll xy=pos2id(x,y);
                auto&& node=graph.at(xy);
                node.flag=true;
                for(const auto& o:ofstbl){
                    ll nx =x+o.first;
                    ll ny =y+o.second;
                    if(available_node(nx,ny)){
                        ll nxy=pos2id(nx,ny);
                        node.edges.emplace_back(nxy,1);
                    }
                }
            }
        }
    }

    function<void(ll)> dfs = [&](ll id){
        auto& node=graph.at(id);
        if (node.flag==false)return; //訪問済orない
        node.flag=false;
        for(const auto& e :graph.at(id).edges){
            dfs(e);
        }
    };

    ll ans=0;
    FOR(i,0,node_max){
        if(graph.at(i).flag){
            ++ans;
            dfs(i);
        }
    }
    cout<<ans<<endl;

    return true;
}

int main_() {
#if LOCAL&01
    //std::ifstream in("./test/sample-1.in");
    std::ifstream in("./input.txt");
    std::cin.rdbuf(in.rdbuf());
#else
    cin.tie(0);
    ios::sync_with_stdio(false);
#endif
    while(solve()){
    }
    return 0;
}

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

int c[52][52];
int dfs(int x,int y){
	if( c[y][x] == 0 ) return 0;
	c[y][x] = 0;
	for(int i = -1 ; i <= 1 ; i++)
		for(int j = -1 ; j <= 1 ; j++)
			dfs(x+i,y+j);

}
int main(){
	int w,h;
	while(cin >> w >> h and w){
		memset(c,0,sizeof(c));
		for(int i = 0 ; i < h ; i++){
			for(int j = 0 ; j < w ; j++){
				cin >> c[i+1][j+1];
			}
		}
		int ans = 0;
		for(int i = 1 ; i <= h ; i++){
			for(int j = 1 ; j <= w ; j++){
				if( c[i][j] ){
					ans++;
					dfs(j,i);
				}
			}
		}
		cout << ans << endl;
	}

}

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

typedef vector<vector<int> > VII;
typedef pair<int, int> PII;

const int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};

void bfs (int i, int j, VII &table) {
    queue<PII> que;
    que.push(make_pair(i, j));
    table[i][j] = 0;
    while (1) {
        if (que.empty() == 1) {
            break; 
        }
        int now_y = que.front().first;
        int now_x = que.front().second;
        que.pop();

        for (int k = 0; k < 8; k++) {
            if (table[now_y + dy[k]][now_x + dx[k]] == 1) {
                que.push(make_pair(now_y + dy[k], now_x + dx[k]));
                    table[now_y + dy[k]][now_x + dx[k]] = 0;
            }
        }
    }
    return;
}

int main(void) {
    int w;
    int h;
    while (cin >> w >> h) {
        if (w == 0 && h == 0) {
            break;
        }
        VII table(h + 2, vector<int>(w + 2, 0));
        for (int i = 1; i < h + 1; i++) {
            for (int j = 1; j < w + 1; j++) {
                cin >> table[i][j];
            }
        }
        int number = 0;
        for (int i = 1; i < h + 1; i++) {
            for (int j = 1; j < w + 1; j++) {
                if (table[i][j] == 1) {
                    bfs(i, j, table);
                    number++;
                }
            }
        }
        cout << number << endl;
    }
    return 0;
}