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 <cstdio>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <math.h>
#include <map>
#include <functional>
#include <iomanip>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <typeinfo>
#define PI 3.14159265359
#define INF 99999999
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(n) rep(i, n)
#define EPS 1e-10
#define pb push_back
#define mp make_pair
typedef long long ll;
using namespace std;
typedef pair<int, int> P;

bool isLeap(int y);
ll gcd(ll a, ll b);
ll lcm(ll a, ll b);
double distanceAB(double xa, double ya, double xb, double yb);
void trace(int A[], int N);

/*
class Target
{
public:
	vector <string> draw(int n)
	{

	}
};
*/


int w, h, c[50][50];
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};

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


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


bool isLeap(int y)
{
	return y % 400 == 0
						? true : y % 100 == 0
						? false : y % 4 == 0
						? true : false;
}

ll gcd(ll a, ll b)
{
	return (b > 0) ? gcd(b, a % b) : a;
}

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

void trace(int A[], int N)
{
	REP(N)
	{
		if (i > 0) cout << " ";
		cout << A[i];
	}
	cout << endl;
}

double distanceAB(double xa, double ya, double xb, double yb)
{
	return sqrt((xb-xa)*(xb-xa) + (yb-ya)*(yb-ya));
}

C++: #include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
				
				
void del_island(int i,int j,vector<vector<int>> &v){	
	v[i][j] = 0;
	if(v[i-1][j] == 1){
		del_island(i-1,j,v);
	}
	if(v[i+1][j] == 1){
		del_island(i+1,j,v);
	}
	if(v[i][j-1] == 1){
		del_island(i,j-1,v);
	}
	if(v[i][j+1] == 1){
		del_island(i,j+1,v);
	}
	if(v[i-1][j-1] == 1){
		del_island(i-1,j-1,v);
	}
	if(v[i-1][j+1] == 1){
		del_island(i-1,j+1,v);
	}
	if(v[i+1][j-1] == 1){
		del_island(i+1,j-1,v);
	}
	if(v[i+1][j+1] == 1){
		del_island(i+1,j+1,v);
	}

}


int count_island(int w, int h,vector<vector<int>> v){
	int count = 0;
	for(int i = 1 ; i <= h ;i++){
		for(int j = 1 ;j <= w ;j++){
			//cout << v[i][j] ;
			if(v[i][j] == 1){
				count++;
				//cout << "hello" <<endl;
				del_island(i,j,v);
			}
		}
	}
	return count;
}
	
int main(){ 
	int w,h;
	vector<vector<int>> v(60,vector<int>(60,0));
	int count;

	while(1){
		cin >> w >> h;
		if(w == 0 && h == 0){
			break;
		}
		count = 0; 
		for(int i = 1; i <= h ; i++){
			for(int j = 0 ; j <= w ; j++){
				v[i][j] = 0;
			}
		}

		for(int i = 1;i <= h; i++){
			for(int j = 1; j <= w; j++){
				cin >> v[i][j];
			}
		}

		count = count_island(w,h,v);


		cout << count << endl;
	}
}

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

#define lint int64_t
const lint MOD = 1e9 + 7;

const int di[] = {0, -1, -1, -1, 0, 1, 1, 1},
    dj[] = {1, 1, 0, -1, -1, -1, 0, 1};

void dfs(int i, int j, vector<vector<int> >& f, int h, int w) {
    f[i][j] = 0;
    for (int k = 0; k < 8; k++) {
        int ni = i + di[k], nj = j + dj[k];
        if (0 <= ni and ni < h and 0 <= nj and nj < w and f[ni][nj] == 1) {
            dfs(ni, nj, f, h, w);
        }
    }
}

int main() {
    while (true) {
        int h, w;
        cin >> w >> h;
        if (h == 0 and w == 0) {
            break;
        }
        vector<vector<int> > f(h, vector<int>(w));
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                cin >> f[i][j];
            }
        }

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

    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
constexpr ll mod=1e9+7;
int w,h;
vector<vector<int>> field;
vector<vector<bool>> seen;

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

void dfs(pair<int,int> v)
{
  seen[v.first][v.second]=true;
  for (int i = 0; i < 8; ++i)
  {
    int nx=v.first+dx[i];
    int ny=v.second+dy[i];
    if(nx<0||nx>=h||ny<0||ny>=w) continue;
    if(field[nx][ny]==0) continue;
    if(seen[nx][ny]) continue;
    dfs({nx,ny});
  }
}

int main()
{while(true){
  
  cin>>w>>h;
  if(w==0&&h==0) break;
  field.resize(h);
  for (int i = 0; i < h; ++i)
  {
    field[i].resize(w);
    for (int j = 0; j < w; ++j)
    {
      cin>>field[i][j];
    }
  }
  seen=vector<vector<bool>>(h,vector<bool>(w,false));

  int cnt=0;
  for (int i = 0; i < h; ++i)
  {
    for (int j = 0; j < w; ++j)
    {
      if(seen[i][j]) continue;
      if(field[i][j]==0) continue;
      dfs({i,j});
      cnt++;
    }
  }
  cout<<cnt<<endl;
}}

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");
    while(true) {
        int w, h;
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        rep(i, h) rep(j, w) {
                int temp;
                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;
}