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"
#define Rep(i,n) for(int i=0;i<(int)(n);i++)
#define For(i,n1,n2) for(int i=(int)(n1);i<(int)(n2);i++)
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define RREP(i,n) for(ll i=((ll)(n)-1);i>=0;i--)
#define FOR(i,n1,n2) for(ll i=(ll)(n1);i<(ll)(n2);i++)
#define RFOR(i,n1,n2) for(ll i=((ll)(n1)-1);i>=(ll)(n2);i--)
#define all(a)  (a).begin(),(a).end()
#define IOS std::cin.tie(0),std::ios::sync_with_stdio(false)

namespace idaten {

	using namespace std;
	using ll = int_fast64_t;
	using ull = uint_fast64_t;
	using P = std::pair<int64_t, int64_t>;
	template<typename T1, typename T2> inline bool chmin(T1& a, T2 b) { if (a > b) { a = b; return 1; }return 0; }
	template<typename T1, typename T2> inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return 1; }return 0; }
	template<class Type>struct is_vector : std::false_type {};
	template<class ValueType, class Alloc>struct is_vector<std::vector<ValueType, Alloc>> : std::true_type {};
	template <typename T> inline std::ostream& operator << (std::ostream& out, const std::vector<T>& v) {
		if (v.empty())return out;
		constexpr bool is_vector_v = is_vector<T>::value;
		if (is_vector_v)for (auto itr = v.begin(); itr != v.end();)out << (*itr), out << ((++itr != v.end()) ? "\n" : "");
		else for (auto itr = v.begin(); itr != v.end();)out << (*itr), out << ((++itr != v.end()) ? " " : "");
		return out;
	}
	inline void put() {}
	template<class T> inline void put(const T& first) { std::cout << first << "\n"; }
	template<class T, class... N> inline void put(const T& first, const N& ... rest) { std::cout << first << " "; put(rest...); }
	inline void putn() {}
	template<class T, class... N> inline void putn(const T& first, const N& ... rest) { std::cout << first << "\n"; putn(rest...); }

	void dfs(int now, vector<vector<int>>& path, vector<int>& visit, int& time) {
		visit[now] = time;
		REP(i, path[now].size()) {
			int t = path[now][i];
			if (visit[t] < 0) {
				dfs(t, path, visit, time);
			}
		}
	}

	void solve() {
		IOS;
		int h, w;
		cin >> w >> h;
		while (h + w > 0) {
			vector<vector<int>> c(h, vector<int>(w));
			vector<vector<int>> path(h * w);
			REP(i, h) {
				REP(j, w) {
					cin >> c[i][j];
				}
			}
			int dy[] = { 0,1,1,1,0,-1,-1,-1 };
			int dx[] = { 1,1,0,-1,-1,-1,0,1 };
			REP(i, h) {
				REP(j, w) {
					if (c[i][j] == 1) {
						REP(k, 8) {
							int ny = i + dy[k];
							int nx = j + dx[k];
							if (0 <= ny && ny < h && 0 <= nx && nx < w) {
								if (c[ny][nx] == 1) {
									int next = ny * w + nx;
									int now = i * w + j;
									path[next].push_back(now);
									path[now].push_back(next);
								}
							}
						}
					}
				}
			}
			vector<int> visit(h * w, -1);
			int cnt = 0;
			REP(i, h) {
				REP(j, w) {
					int t = i * w + j;
					if (c[i][j]==1&&visit[t] < 0) {
						dfs(t, path, visit, cnt);
						cnt++;
					}
				}
			}
			put(cnt);
			cin >> w >> h;
		}
	}
} // namespace idaten

int main() {
	idaten::solve();
	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int ans;
int w, h;
//?§???????
vector<int> vx = { -1, 0, 1, -1, 1, -1, 0, 1 };
vector<int> vy = { 1, 1, 1, 0, 0, -1, -1, -1 };
vector<vector<int>> area;
int islands = 0;

//????±????????????¨????????¢?´¢
void dfs(int i,int j){
	for (int n = 0; n < vx.size(); n++){
		int x = j + vx[n];
		int y = i + vy[n];
		if (x >= 0 && x < w &&
			y >= 0 && y < h &&
			area[y][x] == 1){
			area[y][x] = 2;
			dfs(y,x);
		}
	}
}

int main(){
	while (1){
		cin >> w >> h;
		if (w == 0 && h == 0) break;
		//?????????
		islands = 0;
		area.clear();
		area = vector<vector<int>>(h, vector<int>(w));
		//??\???
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				cin >> area[i][j];
			}
		}
		//??¨??¢?´¢
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				if (area[i][j] == 1){
					islands++;
					area[i][j] = 2;
					dfs(i, j);
				}
			}
		}
		cout << islands << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
#define rep(i, b) for (int i = 0; i < int(b); i++)

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

int const dx[] = { 1, 1, 0, -1, -1, -1, 0, 1 };
int const dy[] = { 0, 1, 1, 1, 0, -1, -1, -1 };
inline bool in(int x, int y){
    return 0 <= x && x < w && 0 <= y && y < h;
}

void fill(int x, int y){
    g[y][x] = 0;
    rep(i, 8){
        int xx = x + dx[i];
        int yy = y + dy[i];
        if (in(xx, yy) && g[yy][xx]) fill(xx, yy);
    }
}

int main(){
    while (cin >> w >> h, w | h){
        g.assign(h, vector<int>(w));
        rep(i, h)rep(j, w) cin >> g[i][j];
        int c = 0;
        rep(i, h)rep(j, w){
            if (g[i][j]){
                c++;
                fill(j, i);
            }
        }
        cout << c << endl;
    }
}

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

#define inRange(x,a,b) (a <= x && x < b)
int di[8] = {0,0,1,-1,1,1,-1,-1};
int dj[8] = {1,-1,0,0,1,-1,1,-1};

int main(){
    int w, h;
    while(cin >> w >> h, w+h){
        char mat[h][w];
        for(int i = 0; i < h*w; i++)    cin >> mat[i/w][i%w];
        int ans = 0;
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                if(mat[i][j] == '0')    continue;
                ans++;
                mat[i][j] = '0';
                queue<pair<int,int>> q;
                q.push({i,j});
                while(!q.empty()){
                    auto p = q.front(); q.pop();
                    int x = p.first, y = p.second;
                    for(int k = 0; k < 8; k++){
                        int nx = x+di[k], ny = y+dj[k];
                        if(inRange(nx,0,h)&&inRange(ny,0,w)&&mat[nx][ny]=='1'){
                            mat[nx][ny] = '0';
                            q.push({nx,ny});
                        }
                    }
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<cstdio>

#define W 50
#define H 50

using namespace std;
void dfs(int w, int h);
int map[H][W];
int dy[8] = {1,1,0,-1,-1,-1,0,1}, dx[8] = {0,-1,-1,-1,0,1,1,1};
int w,h;

int main(){
  int i = 0,j = 0,is=0;
  cin >> w >> h;

  while(w != 0 && h != 0){
    for(i = 0;i < h;i++){
      for(j = 0;j<w;j++){
	cin>>map[i][j];
      }
    }
    i = j = 0;
    is = 0;
    while(i <= h-1  && j <= w-1){
      if(map[i][j] == 1){
	dfs(i,j);
	cout << "dfs end" << endl;
	is++;
      }
      i++;
      if(i == h){
	i = 0;
	j++;
      }
    }
    cout <<"the number of island is " << is << endl;
    cin >> w >> h;
  }
  return 0;
}

void dfs(int x,int y){
  cout << "x is " << x << " y is " << y <<endl;
  int i = 0;
  map[x][y] = 0;
  for(i = 0;i < 8; i++){
    if(x + dx[i] > -1 && y + dy[i] > -1 && x + dx[i] < h && y + dy[i] < w){
      if(map[x + dx[i]][y + dy[i]] == 1){
	dfs(x + dx[i],y + dy[i]);
      }
    }
  }
}