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>
using namespace std;

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

int map[51][51];
int w, h;

//void printmap() {
//	for (int i = 0 ; i < h; i++) {
//		for (int j = 0; j < w; j++)
//			cout << map[i][j] << " ";
//		cout << endl;
//	}
//}

void oneIsland(int x, int y) {
	for (int i = 0; i < 8; i++) {
		int nextX = x + dx[i];
		int nextY = y + dy[i];
		if (x < 0 || w <= x) continue;
		if (y < 0 || w <= y) continue;
		if (map[nextY][nextX] == 0) continue;
		map[nextY][nextX] = 0;
		oneIsland(nextX, nextY);
	}
}

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 >> map[i][j];
			}
		}
		
		int count = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (map[i][j] == 1) {
					map[i][j] = 0;
					oneIsland(j, i);
					//printmap();
					count++;
				}
			}
		}
		cout << count <<endl;
	}
	return 0;
}

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

int main() {

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

  long long int w, h;

  while( true ) {

    cin >> w >> h;

    if ( w == 0 ) break;

    int map[52][52] = {0};

    for ( int y = 0; y < h; y++ ) {
      for ( int x = 0; x < w; x++ ) {

	cin >> map[x+1][y+1];

      }
    }

    int ans = 0;

    while( true ) {

      bool flag = false;
      for ( int y = 1; y < h + 1; y++ ) {
	for ( int x = 1; x < w + 1; x++ ) {

	  if ( map[x][y] == 1 ) {
	    queue<int> qx, qy;
	    qx.push(x);
	    qy.push(y);
	    map[x][y] = 0;
	    while( qx.size() > 0 ) {

	      int lx = qx.front();
	      int ly = qy.front();
	      for ( int i = 0; i < 8; i++ ) {
		if ( map[lx+dx[i]][ly+dy[i]] == 1 ) {
		  map[lx+dx[i]][ly+dy[i]] = 0;
		  qx.push(lx+dx[i]);
		  qy.push(ly+dy[i]);
		}
	      }
	      qx.pop();
	      qy.pop();

	    }
	    flag = true;
	    break;
	  }

	}
	if ( flag ) break;
      }
      if ( flag == false ) break;

      ans++;

    }

    cout << ans << endl;

  }

  return 0;

}

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

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

void solve(int x,int y){
	if(map[y][x] == 1){
		map[y][x] = 0;
		for(int i=-1;i<2;i++){
			for(int j=-1;j<2;j++){
				if(i==0&&j==0){
					continue;
				}
				if(y+i >= 0 && y+i < h && x+i >= 0 && x+i <= w){
					solve(x+j,y+i);
				}
			}
		}
	}
}

int main(){
	while(true){
		int count=0;

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

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

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

		cout << count << endl;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <tuple>
#include <queue>

using namespace std;

class Solver {
public:
	static void solve() {
		int w, h;
		cin >> w >> h;
		vector<int> res;
		while (w > 0 && h > 0) {
			vector<vector<bool>> grid(h, vector<bool>(w));
			for (int i = 0; i < h; i++) {
				for (int j = 0; j < w; j++) {
					int now;
					cin >> now;
					grid[i][j] = now;
				}
			}

			int cnt = 0;
			vector<vector<int>> visited(h, vector<int>(w, false));

			for (int i = 0; i < h; i++) {
				for (int j = 0; j < w; j++) {
					if (!grid[i][j] || visited[i][j])continue;

					cnt++;

					queue<tuple<int, int>> que;
					que.push(make_tuple(i, j));
					while (!que.empty()) {
						auto pos = que.front();
						que.pop();
						int y = get<0>(pos);
						int x = get<1>(pos);
						if (y < 0 || h <= y || x < 0 || w <= x
							|| !grid[y][x] || visited[y][x])continue;

						visited[y][x] = true;

						que.push(make_tuple(y - 1, x));
						que.push(make_tuple(y + 1, x));
						que.push(make_tuple(y, x - 1));
						que.push(make_tuple(y, x + 1));
						que.push(make_tuple(y - 1, x - 1));
						que.push(make_tuple(y + 1, x - 1));
						que.push(make_tuple(y - 1, x + 1));
						que.push(make_tuple(y + 1, x + 1));
					}
				}
			}

			res.emplace_back(cnt);

			cin >> w >> h;
		}

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

int main() {
	Solver::solve();
}

C++: #define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <fstream>
#include <set>
#include <utility>
#include <algorithm>
#include <queue>
#include <stack>
#include <deque>
#include <numeric>
#include <sstream>
#include <list>
#include <map>
#include <functional>
#include <iterator>

#define rep(i,p,n) for(int i=(int)p;i<(int)n;i++)
#define repp(i,p,n) for(int i=(int)p;i>=(int)n;i--)
#define Sort(x) sort(x.begin(),x.end())
#define PSort(x) sort(begin(x), end(x), pairCompare)
#define ll long long
#define INF 2000000000
#define pb push_back
#define mp make_pair
#define PI 3.14159265358979
int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
using namespace std;

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

void dfs(int y, int x) {
	if (v[y][x] == 1)v[y][x] = 0;

	for (int i = -1; i <= 1; i++) {
		for (int j = -1; j <= 1; j++) {
			int nx = x + i, ny = y + j;

			if (!(nx < 0 || ny < 0 || nx >= w||ny >= h)&&v[ny][nx] == 1) dfs(ny, nx);
		}
	}
	
	return;
}

int main(){
	while (1) {
		int ans=0;
		cin >> w >> h;
		if (w == 0 && h == 0)break;
		v.resize(h, vector<int>(w));
		rep(i, 0, h) {
			rep(j, 0, w) {
				cin >> v[i][j];
			}
		}

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

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

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

void dfs(const int x, const int y, const int label) {
	field[y][x] = label;
	for(int i = 0; i < 8; ++i) {
		const int nx = x + dx[i], ny = y + dy[i];
		if(nx < 0 || ny < 0 || nx >= w || ny >= h)
			continue;

		if(field[ny][nx] == 1)
			dfs(nx, ny, label);
	}
}

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

	while(cin >> w >> h, w) {
		for(int i = 0; i < h; ++i)
			for(int j = 0; j < w; ++j)
				cin >> field[i][j];

		int label = 2;
		for(int i = 0; i < h; ++i)
			for(int j = 0; j < w; ++j)
				if(field[i][j] == 1)
					dfs(j, i, label++);

		cout << label - 2 << endl;
	}

	return EXIT_SUCCESS;
}