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 <string>
#include <stack>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;


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

void dfs(int px , int py){
	int tx , ty;
	land[py][px]=0;
	for(int i=-1 ; i<=1 ; ++i){
		for(int j=-1 ; j<=1 ; ++j){
			tx=px+i , ty=py+j;
			if(tx<0 || tx>=w || ty<0 || ty>=h) continue;
			if(land[ty][tx]) dfs(tx , ty);
		}
	}
}


int main(){
	int cnt=0;
	while(cin >> w >> h , w||h){
		for(int i=0 ; i< h ; ++i){
			for(int j=0 ; j< w ; ++j){
				cin >> land[i][j];
			}
		}
		for(int i=0 ; i<h ; ++i){
			for(int j=0 ; j<w ; ++j){
				if(land[i][j]){
					dfs( j , i);
					cnt++;
				}
			}
		}
		cout << cnt << endl;
		cnt=0;
	}
	return 0; 
}

C++: #include"bits/stdc++.h"

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;


vector<string>road;
int ix[] = { 0,1,1,1,0,-1,-1,-1 };
int iy[] = { -1,-1,0,1,1,1,0,-1 };
void dfs(int y, int x, int h, int w) {
	road[y][x] = '0';
	rep(i, 8) {
		int dx = x + ix[i], dy = y + iy[i];
		if (dx < 0 || w <= dx || dy < 0 || h <= dy || road[dy][dx] == '0')continue;
		dfs(dy, dx, h, w);
	}
}

int main() {
	while (1) {
		int h, w, cnt = 0;
		string k;
		cin >> w >> h;
		if (h == 0 || w == 0)break;
		road.resize(h);
		road.assign(h, "");
		rep(i, h)rep(j, w) {
			cin >> k;
			road[i] += k;
		}
		rep(i, h)rep(j, w) {
			if (road[i][j] == '0')continue;
			dfs(i, j, h, w);
			cnt++;
		}
		cout << cnt << endl;
	}
	return 0;
}

C++: #include <math.h>

#include <algorithm>
#include <bitset>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

void dfs(int x, int y, vector<vector<int>>& field) {
  if (0 <= x && x < field.size() && 0 <= y && y < field[0].size()) {
    if (field[x][y] == 0) {
      return;
    }
  } else {
    return;
  }

  field[x][y] = 0;
  for (int dx = -1; dx <= 1; dx++) {
    for (int dy = -1; dy <= 1; dy++) {
      dfs(x + dx, y + dy, field);
    }
  }
  return;
}

int main() {
  vector<int> ans_v;
  for (int k = 0; k < 100; k++) {
    int w, h;
    cin >> w >> h;
    if (w == 0) {
      break;
    }

    vector<vector<int>> field(h, vector<int>(w));
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> field[i][j];
      }
    }
    int ans = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (field[i][j] == 1) {
          dfs(i, j, field);
          ans++;
        }
      }
    }

    ans_v.push_back(ans);
  }

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

  return 0;
}

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

using namespace std;

#define rep(i, n) for(int i=0; i<(n); i++)
#define FOR(i, m, n) for(int i=(m);i<(n);i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define SORT(x) sort((x).begin(),(x).end())
#define REVE(x) reverse((x).begin(),(x).end())
#define mp make_pair
#define pb push_back

typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<vector<int>> VVI;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<bool> VB;

int h, w;
VVI c(55, VI(55));
vector<VB> visited(55, VB(55));

int d1[] = { 1,1,1,0,-1,-1,-1,0 };
int d2[] = { -1,0,1,1,1,0,-1,-1 };

void dfs(int x, int y) {
	if (x < 0 || y < 0 || x >= h || y >= w || c[x][y] == 0 || visited[x][y])return;
	visited[x][y] = true;
	rep(i, 8)dfs(x + d1[i], y + d2[i]);
}


int main() {
	while (true) {
		cin >> w >> h;
		if (w == 0 && h == 0)break;

		rep(i, 52)rep(j, 52) { c[i][j] = 0; visited[i][j] = false; }
		rep(i, h)rep(j, w)cin >> c[i][j];



		int cnt = 0;
		rep(i, h)rep(j, w) {
			//printf("visited[%d][%d]=%s", i, j, visited[i][j] ? "true" : "false");
			if (c[i][j] == 1 && visited[i][j] == false) {
				//puts(" -> o");
				dfs(i, j);
				cnt++;
			}
			else {
				//puts(" -> x");
			}
		}
		cout << cnt << endl;
	}
}

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

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

void dfs(int x, int y) {
  if(x < 0 || h <= x || y < 0 || w <= y || field[x][y] == 0) return; 
  field[x][y] = 0;
  for(int i = -1; i <= 1; i++) {
    for(int j = -1; j <= 1; j++) {
      if(i == 0 && j == 0) continue;
      dfs(x+i, y+j);
    }
  }
}

int main() {

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

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

    cout << ans << endl;
  }
}

C++: #include <iostream>

using namespace std;

// 0: 海, 1: 陸
int board[50][50] = {{0}};

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

bool DFS(int i, int j, int w, int h) {
  if (i < 0 || i >= h || j < 0 || j >= w) return false;

  if (!board[i][j]) return false;

  board[i][j] = 0;
  for (int d = 0; d < 8; ++d)
    DFS(i + dy[d], j + dx[d], w, h);

  return true;
}

int main() {
  for (;;) {
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0) break;

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

    int ans = 0;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (DFS(i, j, w, h)) ++ans;
      }
    }
    cout << ans << endl;
  }
  return 0;
}