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>
#include <cassert>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const long long MOD = 1000000007;
const long long INF = 1LL << 60;

int w, h, x, y;

void dfs(vector<vector<int>> &map, int x, int y)
{
  // 今いるところを0に置き換える
  map[x][y] = 0;

  for (int dx = -1; dx <= 1; dx++)
  {
    for (int dy = -1; dy <= 1; dy++)
    {
      // x方向にdx移動したものをnx，y方向にdy移動したものをnyとおく
      int nx = x + dx, ny = y + dy;

      // nxとnyが地図の範囲内かどうかとmap[nx][ny]が陸なのかを判定
      if (0 <= nx && nx < h && 0 <= ny && ny < w && map[nx][ny] == 1)
        dfs(map, nx, ny);
    }
  }
  return;
}

void solve(vector<vector<int>> &map)
{
  int ans = 0;
  for (int i = 0; i < h; i++)
  {
    for (int j = 0; j < w; j++)
    {
      if (map[i][j] == 1)
      {
        dfs(map, i, j);
        ans++;
      }
    }
  }
  cout << ans << endl;
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  while (1)
  {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    vector<vector<int>> map(h, vector<int>(w));
    for (int i = 0; i < h; i++)
    {
      for (int j = 0; j < w; j++)
      {
        cin >> map[i][j];
      }
    }
    solve(map);
  }
  return 0;
}

C++: #include<iostream>
#include <cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<utility>
#include<queue>

#define REP(i,n) for(int i = 0;i < n ;i++)
	
using namespace std;

int mp[60][60],dp[60][60];
int w,h,ans;
int dx[] = {1,1,0,-1,-1,-1,0,1},dy[] = {0,1,1,1,0,-1,-1,-1};

void dfs(int x,int y)
{
	REP(i,8)
	{
		int cx = x + dx[i],cy = y + dy[i];
		if(cx >= 0 && cx < h && cy >= 0 \
				&& cy < w && dp[cx][cy] == 0 && mp[cx][cy] == 1)
				{
					dp[cx][cy] = 1;
					dfs(cx,cy);
				
				}
				
	}
}
			

int main()
{
	for(;;)
	{
		ans = 0;
		REP(i,60)REP(j,60)dp[i][j]=0;
		cin >> w >> h;
		if(w == 0 && h == 0) break;
		REP(i,h)REP(j,w)cin >> mp[i][j];
		REP(i,h)REP(j,w)
		{
			if(mp[i][j] == 1 && dp[i][j] == 0)
			{
				ans++;
				dfs(i,j);
			}
		}
		
		cout << ans << endl;
		
	}
	return 0;

}

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

constexpr int MAX_H = 50;
constexpr int MAX_W = 50;

int H, W;
int grid[MAX_H][MAX_W];
bool reached[MAX_H][MAX_W];

#define OCTAS
#ifdef OCTAS
constexpr int dh[]{-1, -1, -1, 0, 0, +1, +1, +1};
constexpr int dw[]{-1, 0, +1, -1, +1, -1, 0, +1};
#else
constexpr int dh[]{-1, 0, 0, +1};
constexpr int dw[]{0, -1, +1, 0};
#endif

void dfs(int h, int w) {
  reached[h][w] = true;

  for (int i{}; i < sizeof(dh) / sizeof(int); ++i) {
    int nh = h + dh[i];
    if (nh < 0 || nh >= H)
      continue;
    int nw = w + dw[i];
    if (nw < 0 || nw >= W)
      continue;
    if (grid[nh][nw] == 0 || reached[nh][nw])
      continue;
    dfs(nh, nw);
  }
}

int main() {
  while (true) {
    cin >> H >> W;
    if (H == 0 && W == 0)
      break;
    for (int h{}; h < H; ++h) {
      for (int w{}; w < W; ++w) {
        reached[h][w] = false;
        cin >> grid[h][w];
      }
    }

    int island{};
    for (int h{}; h < H; ++h) {
      for (int w{}; w < W; ++w) {
        if (grid[h][w] == 0 || reached[h][w])
          continue;
        dfs(h, w);
        ++island;
      }
    }

    cout << island << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <utility>
#include <tuple>
#include <algorithm>
#include <cstdio>
#include <cmath>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

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

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

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

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

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n; i++)
using namespace std;
int table[105][105];
int check(int i, int j){
  for(int a=i-1; a<i+2; a++){
    for(int b=j-1; b<j+2; b++){
      if(table[a][b]==1){
        table[a][b]=0;
        check(a,b);
      }
    }
  }
  return 0;
}
int main(){
  int w,h;
  while(1){
    cin >> w >> h;
    if(w==0 && h==0) break;
    table[w+2][h+2]={};
    rep(i,h) rep(j,w) cin >> table[i+1][j+1];
    rep(i,h+1){
      rep(j,w+1){
        if(table[i][j]==1){
          table[i][j]=2;
          check(i,j);
        }
      }
    }
    int count=0;
    for(int i=1; i<h+1; i++) for(int j=1; j<w+1; j++) if(table[i][j]==2) count++;
    cout << count << endl;
  }
  return 0;
}