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 <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <utility>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstring>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI=3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for(int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y) - 1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

int W, H;
vector< vs32 > field(50, vs32(50));

void dfs(int y, int x)
{
    field[y][x] = 0;

    for (int dy = -1; dy <= 1; ++dy)
    {
        for (int dx = -1; dx <= 1; ++dx)
        {
            int nx = x + dx;
            int ny = y + dy;
            if (IN(0, W, nx) && IN(0, H, ny) && field[ny][nx] == 1)
            {
                dfs(ny, nx);
            }
        }
    }

    return;
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    while (true)
    {
        cin >> W >> H;
        if (H == 0 || W == 0) break;

        rep (i, H) rep (j, W) cin >> field[i][j];

        int ans = 0;
        rep (i, H)
        {
            rep (j, W)
            {
                if (field[i][j] == 1)
                {
                    dfs(i, j);
                    ++ans;
                }
            }
        }
        cout << ans << "\n";
    }

    return 0;
}

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

int myMap[50][50];
int w, h;
int ans;

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

void del(int x, int y){
    myMap[x][y] = 0;
    if (safe(x+1, y) && myMap[x+1][y]) del(x+1, y);
    if (safe(x+1, y+1) && myMap[x+1][y+1]) del(x+1, y+1);
    if (safe(x+1, y-1) && myMap[x+1][y-1]) del(x+1, y-1);
    if (safe(x, y+1) && myMap[x][y+1]) del(x, y+1);
    if (safe(x, y-1) && myMap[x][y-1]) del(x, y-1);
    if (safe(x-1, y-1) && myMap[x-1][y-1]) del(x-1, y-1);
    if (safe(x-1, y+1) && myMap[x-1][y+1]) del(x-1, y+1);
    if (safe(x-1, y) && myMap[x-1][y]) del(x-1, y);
    return;
}

int main(){

    while (1){
	cin >> w >> h;
	if (w == 0 && h == 0) break;

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

	ans = 0;	
	for (int i = 0; i < w; i++){
	    for (int j = 0; j < h; j++){
		if (myMap[i][j] == 1){
		    ans++;
		    del(i, j);
		}
	    }
	}

	cout << ans << endl;
    }
    
}

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

class Solve {
private:
	using vb = std::vector<bool>;
	using vvb = std::vector<vb>;

	int h, w;
	vvb map;

	void dfs(int r, int c)
	{
		if (!map[r][c]) return;
		map[r][c] = false;
		for (int i{r - 1}; i <= r + 1; i++)
			for (int j{c - 1}; j <= c + 1; j++)
				dfs(i, j);
	}

public:
	bool is_last_query{};
	Solve()
	{
		scanf("%d%d", &w, &h);
		if (w == 0)
		{
			is_last_query = true;
			return;
		}
		map.resize(h + 2, vb(w + 2));
		for (int i{1}; i <= h; i++)
			for (int j{1}; j <= w; j++)
			{
				int c;
				scanf("%d", &c);
				map[i][j] = c == 1;
			}

		int count{};
		for (int i{1}; i <= h; i++)
			for (int j{1}; j <= w; j++)
			{
				if (!map[i][j]) continue;
				count++;
				dfs(i, j);
			}

		printf("%d\n", count);
	}
};

int main()
{
	while (!Solve().is_last_query);

	return 0;
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <queue>
#include <complex>

#define INF 100000000
#define EPS 1e-9

using namespace std;

typedef long long ll; 

#define MAX_W 50
#define MAX_H 50

int W, H;
int c[MAX_H][MAX_W];

vector<int> ans_list;

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

void solve(int x, int y){
  
  c[y][x] = 0;
  
  for(int i = 0; i < 8; i++){
    int nx = x + dx[i];
    int ny = y + dy[i];
    if(nx >= 0 && nx < W && ny >= 0 && ny < H && c[ny][nx] == 1)
      solve(nx, ny);
  }

}

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

    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] == 1){
	  ans++;
	  solve(j,i);
	}
      }
    }
    
    ans_list.push_back(ans);

  }

  for(int i = 0; i < ans_list.size(); i++){
    cout << ans_list[i] << endl;
  }
  
  return 0;
  
}

C++: # include <vector>
# include <list>
# include <map>
# include <set>
# include <deque>
# include <stack>
# include <bitset>
# include <algorithm>
# include <functional>
# include <numeric>
# include <utility>
# include <sstream>
# include <iostream>
# include <iomanip>
# include <cstdio>
# include <cmath>
# include <cstdlib>
# include <cctype>
# include <string>
# include <cstring>
# include <ctime>
# include <queue>


# define FOR(i, a, b) for(int i{a}; i < (b); ++i)
# define REP(i, n)    FOR(i, 0, n)

using namespace std;

using ll = long long;

template<typename T>
using Vec = vector<T>;


template<typename T>
using vec = vector<T>;
template <class T>
using vv = vector<vector<T>>;

constexpr int ocean = 0;

void dfs(const vv<int>& map, vv<bool>& reached, int r, int c) {
    reached[r][c] = true;
    if (reached[r][c] == ocean)
        return;
    for (int i = -1; i <= 1; ++i)
        for (int j = -1; j <= 1; ++j)
        {
            if (i == 0 && j == 0)
                continue;
            int y = r + i;
            int x = c + j;
            if (!reached[y][x] && map[y][x] != ocean)
                dfs(map, reached, y, x);
        }
}

int main()
{

    while (true)
    {
        int w, h;
        cin >> w >> h;

        if (w == 0)
            break;

        vv<int> map(h + 2, vec<int>(w + 2, ocean));

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

        vv<bool> hasReached(h + 2, vec<bool>(w + 2, false));

        // init
        

        int count{};

        for (int i = 1; i < map.size(); ++i)
            for (int j = 1; j < map[0].size(); ++j)
            {
                if (!hasReached[i][j] && map[i][j] != ocean)
                {
                    ++count;
                    dfs(map, hasReached, i, j);
                }
            }

        cout << count << endl;
    }

    return 0;
}