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>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) FOR(i,0,n)
#define repr(i,n) for(int i=(n)-1;0<=i;--i)
#define each(e,v) for(auto&& e:(v))
#define all(v) begin(v),end(v)
#define dump(x) cerr<<#x<<": "<<(x)<<endl
#define INF 100000000
#define IINF 9999999999999;
using vint = vector<int>;
using ll = long long;
using vll = vector<ll>;
template <class T> void chmin(T& a, const T& b) { a = min(a, b); }
template <class T> void chmax(T& a, const T& b) { a = max(a, b); }

int prime[25] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
long long int dp[101][20] = {0};

bool earth[51][51];

void erase_island(int h, int w) {
    earth[h][w] = false;
    //cout << "# " << h << " " << w << endl;
    if(earth[h - 1][w - 1]) erase_island(h - 1, w - 1);
    if(earth[h - 1][w]) erase_island(h - 1, w);
    if(earth[h - 1][w + 1]) erase_island(h - 1, w + 1);
    if(earth[h][w + 1]) erase_island(h, w + 1);
    if(earth[h + 1][w + 1]) erase_island(h + 1, w + 1);
    if(earth[h + 1][w]) erase_island(h + 1, w);
    if(earth[h + 1][w - 1]) erase_island(h + 1, w - 1);
    if(earth[h][w - 1]) erase_island(h, w - 1);
}

int main() {
    int w, h;
    cin >> w >> h;
    rep(i, h) rep(j, w) {
        int temp;
        cin >> temp;
        if(temp == 0) earth[i][j] = false;
        else earth[i][j] = true;
    }

    int ans = 0;
    rep(i, h) rep(j, w) {
        if(earth[i][j]) erase_island(i , j), ++ans;
    }

    cout << ans << endl;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i=0; i<(n); ++i)
using namespace std;

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

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

void dfs(int i, int j){
    c[i][j]=0;
    rep(dir,8){
        int vy=i+dy[dir];
        int vx=j+dx[dir];
        if(vy>=0&&vy<h&&vx>=0&&vx<w&&c[vy][vx]==1){
            dfs(vy,vx);
        }
    }
    return;
}

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

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

using ll = long long;
#define REP(i, n) for(int (i)=0; (i)< (n); ++i)
#define REPR(i, n) for(int (i)=(n); (i)>=0; --i)
#define FOR(i, n, m) for(int (i)=(n); (i)<(m); ++i)

int w, h;
vector<vector<int>> c(50, vector<int>(50)), homon(50, vector<int>(50));
int dx[] = {0, 1, 0, -1, 1, -1, -1, 1};
int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};

void dfs(int y, int x){
  homon[y][x] = 1;
  REP(i, 8){
    int nx = x + dx[i], ny = y+dy[i];
    if(nx < 0 || w <= nx || ny < 0 || h <= ny) continue;
    if(homon[ny][nx] || c[ny][nx] == 0) continue;
    dfs(ny, nx);
  }
}

int main(){
  vector<int> ans;
  while(true){
    cin >> w >> h;
    if(w==0 && h== 0) break;
    REP(i, h){
      REP(j, w){
        cin >> c[i][j];
      }
    }
    REP(i, h){
      REP(j, w){
        homon[i][j] = 0;
      }
    }
    int cnt = 0;
    REP(i, h){
      REP(j, w){
        if(homon[i][j]==1 || c[i][j]==0) continue;
        dfs(i, j);
        cnt++;
      }
    }
    ans.push_back(cnt);
  }
  REP(i, ans.size()){
    cout << ans[i] << endl;
  }
  return 0;
}

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

using ll=long long;
#define int ll

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

int read(){
	int i;
	scanf("%lld",&i);
	return i;
}

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

void isLand(int i,int j){
    if(land[i][j]==1){
    	land[i][j] = 0;
		REP(k,8){
			int newx=i+dy[k];
			int newy=j+dx[k];
			isLand(newy,newx);
		}
    }
}

signed main(){
	int w,h;
	while( w=read(),h=read(),w||h){
		cnt=0;
		
		REP(i,100){
			REP(j,100){
				land[i][j]=0;
			}
		}
		FOR(i,1,h+1){
			FOR(j,1,w+1){
				land[i][j]=read();
			}
		}
		
		FOR(i,1,h+1){
			FOR(j,1,w+1){
				if(land[i][j]==1){
					cnt++;
					isLand(i,j);
				}
			}
		}
		
		cout << cnt << endl;
	}
	
}

C++: #include <iostream>
#include <vector>
using namespace std;
using Mat = vector<vector<bool>>;
int w, h;
const int dxy[10] = {0,1,1,1,0,-1,-1,-1,0,1};
void search(const Mat &map, Mat &searched, int x, int y, int dir) {
    if (x < 0 || w <= x || y < 0 || h <= y) {
        return;
    } else if (!map[x][y] || searched[x][y]) {
        return;
    }
    
    searched[x][y] = true;
    
    for (int i = 0; i < 8; ++i) {
        search(map, searched, x + dxy[i], y + dxy[i+2], i);
    }
}
int main(void){
    while (cin >> w >> h && w && h) {
        Mat map(w, vector<bool>(h, false));
        for (int y = 0; y < h; ++y) {
            for (int x = 0; x < w; ++x) {
                int c;
                cin >> c;
                map[x][y] = c;
            }
        }
        Mat searched(w, vector<bool>(h, false));
        int count = 0;
        for (int y = 0; y < h; ++y) {
            for (int x = 0; x < w; ++x) {
                if (searched[x][y] || !map[x][y]) continue;
                search(map, searched, x, y, -1);
                ++count;
            }
        }
        cout << count << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
struct Point{int x,y;};
int main(){
  int w,h,p[51][51]={},v[51][51]={},i,j,k,o;
  queue <Point> q;
  Point n,b;
  cin >> w >> h;
  while(w!=0){
    for(j=0;j<h;j++){
      for(i=0;i<w;i++){
	cin >> p[i][j];
	v[i][j]=0;
      }
      
    }
    o=0;
    for(j=0;j<h;j++){
      for(i=0;i<w;i++){
	if(v[i][j]==0&&p[i][j]==1){
	  o++;
	  n.x=i;n.y=j;
	  q.push(n);
	  while(!q.empty()){
	    n=q.front();
	    q.pop();
	    v[n.x][n.y]=1;
	    if(n.x+1<w&&v[n.x+1][n.y]==0&&p[n.x+1][n.y]==1) {
	      b.x=n.x+1;b.y=n.y;
	      q.push(b);
	    }
	    if(n.y+1<h&&v[n.x][n.y+1]==0&&p[n.x][n.y+1]==1) {
	      b.x=n.x;b.y=n.y+1;
	      q.push(b);
	    }
	    if(n.x-1>=0&&v[n.x-1][n.y]==0&&p[n.x-1][n.y]==1) {
	      b.x=n.x-1;b.y=n.y;
	      q.push(b);
	    }
	    if(n.y-1>=0&&v[n.x][n.y-1]==0&&p[n.x][n.y-1]==1) {
	      b.x=n.x;b.y=n.y-1;
	      q.push(b);
	    }
	    if(n.x+1<w&&n.y+1<h&&v[n.x+1][n.y+1]==0&&p[n.x+1][n.y+1]==1) {
	      b.x=n.x+1;b.y=n.y+1;
	      q.push(b);
	    }
	    if(n.x-1>=0&&n.y-1>=0&&v[n.x-1][n.y-1]==0&&p[n.x-1][n.y-1]==1) {
	      b.x=n.x-1;b.y=n.y-1;
	      q.push(b);
	    }
	    if(n.x+1<w&&n.y-1>=0&&v[n.x+1][n.y-1]==0&&p[n.x+1][n.y-1]==1) {
	      b.x=n.x+1;b.y=n.y-1;
	      q.push(b);
	    }
	    if(n.x-1>=0&&n.y+1<h&&v[n.x-1][n.y+1]==0&&p[n.x-1][n.y+1]==1) {
	      b.x=n.x-1;b.y=n.y+1;
	      q.push(b);
	    }
	  }
	}
      }
    }
    cout << o << endl;
    cin >> w >> h;
  }
  return 0;
}