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>
#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;
    rep(i,h+2){
      rep(j,w+2) cout << table[i][j] << " ";
      cout << endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <queue>
#include <map>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstdarg>

using namespace std;
#define INF 2e9 + 9
#define ALL(v) v.begin() , v.end()
#define Int int64_t
#define pll pair<Int,Int>
template<class T> using pql = priority_queue<T, vector<T>>;
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;
#define ll(n) Int n; cin >> n;
#define LL(...) Int __VA_ARGS___; cin >> __VA_ARGS___;
#define OUT(...) cout << __VA_ARGS___;
#define RP(n, i, s) for(Int i = s ;i < n ;i++)
#define REP(n, i) for (Int i = 0; i < n; i++)
#define REPe(n, i) for (Int i = 1; i <= n; i++)
#define RPe(n, i, s) for (Int i = s; i <= n; i++)
#define upd_max(maxer, updater) if (maxer < updater) maxer = updater;

struct node {
    Int to;
    Int length;
};


vector<Int> divisor(Int n) {
    vector<Int> ret;
    for (Int i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i * i != n) ret.push_back(n / i);
        }
    }
    return (ret);
}


vector<vector<int>> to;
vector<int> flag;
vector<int> c;

Int dfs(int from, int mae) {
    flag[from] = 1;
    Int mini = 0;
    for (auto next : to[from]) {
        if (next == mae) continue;
        if (flag[next] == 1) {
            return -1;
        } else {
            mini = min(mini, dfs(next, from));
        }
    }
    return mini;
}

int dx8[8] = {0, -1, -1, -1, 0, 1, 1, 1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};

vector<vector<Int>> a;
vector<vector<Int>> d;
Int w, h;

void dfs(int x, int y, int id) {
    if (d[y][x] != 0) return;
    d[y][x] = id;
    REP(8, i) {
        Int tox = x + dx8[i];
        Int toy = y + dy8[i];
        if (tox >= 0 && toy >= 0 && tox < w && toy < h && a[toy][tox] == 1) {
            a[toy][tox] = 0;
            dfs(tox, toy, id);
        }
    }
}


int main(void) {
    while (true) {
        cin >> w >> h;
        if (w == 0) break;
        d.assign(h, vector<Int>(w, 0));
        a.clear();
        REP(h, i) {
            vector<Int> z;
            REP(w, k) {
                Int tmp;
                cin >> tmp;
                z.push_back(tmp);
            }
            a.push_back(z);
        }
        Int cnt = 1;
        // a[y][x]

        REP(h, k) {
            REP(w, i) {
                if (a[k][i] == 1) dfs(i, k, cnt);
                /*for (auto z : a){
                cout << "Cnt : " << cnt << endl;
                    for (auto zz : z){
                        cout << zz << " ";
                    }
                    cout << endl;
                }*/
                cnt++;
            }
        }
        map<int, int> mp;
        REP(h, k) {
            REP(w, i) {
                if (d[k][i] != 0) mp[d[k][i]]++;
            }
        }
        cout << mp.size() << endl;
    }
}

C++: #include <iostream>

using namespace std;

int m[51][51];
int w, h;
int isl;
const int dx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};

void dfs(int x, int y)
{
    m[x][y] = -1;
    for (int i = 0; i < 8; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        
        if (nx < 0 || ny < 0 || nx >= w || ny >= h)
            continue;
        else if (m[nx][ny] == 1)
            dfs(nx, ny);
    }
}

int main()
{
    while (cin >> w >> h, w || h) {
        isl = 0;

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

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

    return 0;
}

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

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define abs(a) max((a),-(a))
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define repe(i,n) rep(i,(n)+1)
#define per(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define pere(i,n) rep(i,(n)+1)
#define all(x) (x).begin(),(x).end()
#define SP <<" "<<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000

typedef long long LL;
typedef long double LD;

int main(){
  while(1){
    int h,w;
    cin >> w >> h;
    if(w==0) return 0;
    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];
      }
    }
    queue<pair<int,int>> bfs;
    int nx,ny,hx[8]={-1,-1,-1,0,0,1,1,1},hy[8]={-1,0,1,-1,1,-1,0,1};
    int count=0;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        if(field[i][j]==1){
          count++;
          field[i][j]=0;
          bfs.push({j,i});
          while(!bfs.empty()){
            pair<int,int> now=bfs.front();
            bfs.pop();
            for(int i=0;i<8;i++){
              nx=now.first+hx[i];
              ny=now.second+hy[i];
              if(0<=nx&&nx<w&&0<=ny&&ny<h){
                if(field[ny][nx]==1){
                  field[ny][nx]=0;
                  bfs.push({nx,ny});
                }
              }
            }
          }
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}

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

#define REP(i,n) for(int i=0;i<(n);i++)
#define REAP(i,a,n) for(int i=(a);i<(n);i++)
#define YES cout<<"Yes"<<endl
#define NO cout<<"No"<<endl
#define fr first
#define sc second
#define pb push_back
#define All(v) v.begin(),v.end()
typedef long long ll;

int dp[51][51];

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


void check(int i,int j,int h,int w){
    dp[i][j]=2;
    for(int a=0;a<8;a++){
        int x=i+dx[a];
        int y=j+dy[a];
        if(x>=0&&x<h&&y>=0&&y<w){
            if(dp[x][y]==1){
                check(x,y,h,w);
            }
        }
    }
}

int main(){
    int h,w,cou;
    while(cin >> w >> h,w){
        cou=0;
        REP(i,h)REP(j,w)cin >> dp[i][j];
        REP(i,h){
            REP(j,w){
                if(dp[i][j]==1){
                    cou++;
                    check(i,j,h,w);
                }
            }
        }
        cout << cou << endl;
    }
}