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;

typedef long long int ll;
typedef pair<int,int> P;

#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<(b);i++)
#define all(x) x.begin(),x.end()

const ll mod = 1e9+7;
const ll INF = 1e9;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

bool valid(int lx,int ux,int ly,int uy,int x,int y){
	return (lx<=x&&x<ux&&ly<=y&&y<uy);
}

void dfs(vector<vector<int>> &s,int w,int h,int x,int y){
	s[y][x]=0;
	repi(dx,-1,2){
		repi(dy,-1,2){
			if(valid(0,w,0,h,x+dx,y+dy)&&s[y+dy][x+dx]==1){
				dfs(s,w,h,x+dx,y+dy);
			}
		}
	}
}

int main()
{
	int w,h;
	while(cin>>w>>h,w!=0&&h!=0){
		vector<vector<int>> s(h,vector<int>(w,0));
		rep(y,h)rep(x,w)cin>>s[y][x];

		int ans=0;
		rep(y,h){
			rep(x,w){
				if(s[y][x]==1)ans++,dfs(s,w,h,x,y);
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<map>
#include<string>
#include<cstring>
#include<bitset>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<numeric>
using namespace std;

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

#define rep(i,x) for(int i=0;i<x;i++)
#define re(i,x,y) for(int i=x;i<y;i++)

long long INF=1e9;
const long long mod=2019;
#define ll long long

int H,W;
vector<vector<int>>field;
void dfs(int h,int w){
  field[h][w]=0;
  for(int dh=-1;dh<=1;dh++){
    for(int dw=-1;dw<=1;dw++){
      int nh=h+dh,nw=w+dw;
      if(nh<0||nh>=H||nw<0||nw>=W)continue;
      if(field[nh][nw]==0)continue;
      dfs(nh,nw);
    }
  }
}

int main(){
  while(cin>>W>>H){
    if(H==0||W==0)break;
    field.assign(H,vector<int>(W,0));
    for(int h=0;h<H;h++){
      for(int w=0;w<W;w++){
        cin>>field[h][w];
      }
    }
    int count=0;
    for(int h=0;h<H;h++){
      for(int w=0;w<W;w++){
        if(field[h][w]==0)continue;
        dfs(h,w);
        count++;
      }
    }
    cout<<count<<endl;
  }
}

C++: #include <cstdio>
#define f(n)for(int i=0;i<n;i++)
#define s(w)scanf("%d",&w)
int a,n,m,d[99][99];
int u[16]={-1,-1,-1,0,0,1,1,1,-1,0,1,-1,1,-1,0,1};
void k(int x,int y){
	if(d[x][y]){f(8)k(x+u[i],y+u[i+8]);d[x][y]=0;}
}
int main(){
	a=0;
	s(n);s(m);
	f(9801)d[i/99][i%99]=0;
	f(n*m)s(d[i/n+1][i%n+1]);
	f(n*m)if(d[i/n+1][i%n+1])a++,k(i/n+1,i%n+1);
	printf("%d\n",a);
	main();
}

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

using namespace std;

    int w,h;
    int c[55][55];
    vector<vector<bool> > seen;
    int mx[8] = {-1,1,0,-1,1,-1,0,1};
    int my[8] = {0,0,1,1,1,-1,-1,-1};
    
    void dfs(int x,int y){
    	::seen[x][y] = true;
    	
    	for(int i = 0; i < 8; i++){
			int nx = x + mx[i];
			int ny = y + my[i];
			
			if(nx < 0 || nx >= h || ny < 0 || ny >= w)	continue;
			if(::seen[nx][ny])	continue;
			if(c[nx][ny] == 0)	continue;
			
			dfs(nx,ny);
		}
    }
int main(){
	while(true){
	cin >> w >> h;
	if(w == 0 && h == 0) break;
	::seen.assign(55,vector<bool>(55,false)); //initilaization
	
	
	int count = 0;
	for(int i = 0; i < h; i++){
		for(int j = 0; j < w; j++){
			cin >> c[i][j];
		}
	}
	
	for(int i = 0; i < h; i++){
		for(int j = 0; j < w; j++){
			if(::seen[i][j])	continue;
			if(c[i][j] == 0)	continue;
			
			dfs(i,j);
			count++;
		}
	}
	cout << count << endl;
	}
}

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

using namespace std;

#ifdef _DEBUG
#define debug_io fstream cin("input.txt");ofstream cout("output.txt");
#else
#define debug_io ;
#endif

int main()
{
	debug_io
	int w, h;
	while (cin >> w >> h, w + h) {
		int tile[55][55];
		REP(i, h) {
			REP(j, w) {
				cin >> tile[i][j];
			}
		}

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

		int ans = 0;
		REP(i, h) {
			REP(j, w) {
				if (tile[i][j] == 1) {
					vector<pair<int, int>> bfs;
					bfs.push_back({ i,j });

					while (bfs.size()) {
						auto now = bfs.front();
						bfs.erase(bfs.begin());
						int x = now.second;
						int y = now.first;
						tile[y][x] = 2;
						REP(k, 8) {
							int nx = max(0,min(x + dx[k],w-1));
							int ny = max(0, min(y + dy[k], h - 1));
							if (tile[ny][nx] == 1) {
								bfs.push_back({ ny,nx });
							}
						}
					}
					ans++;
					
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e12;

int w, h;
int c[51][51];
bool f[51][51];
int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
bool contain(int y, int x){
   return (0 <= y && y < h && 0 <= x && x < w);
}

void dfs(int y, int x){
    f[y][x] = true;
    rep(i, 0, 8){
        int ny = y + dy[i];
        int nx = x + dx[i];
        if(!contain(ny, nx)) continue;
        if(f[ny][nx] || c[ny][nx] == 0) continue;
        dfs(ny, nx);
    }
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(cin >> w >> h, w, h){
        rep(i, 0, h){
            rep(j, 0, w){
                cin >> c[i][j];
                f[i][j] = false;
            }
        }
        int ans = 0;
        rep(i, 0, h){
            rep(j, 0, w){
                if(c[i][j] == 1 && f[i][j] == false){
                    dfs(i, j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
}