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 <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>

using namespace std;

inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}

typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EACH(t,i,c) for(t::iretator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

const double EPS = 1e-10;

int seamap[52][52];
static int num = 0;

void dfs(int x, int y) {
	if(seamap[x][y] == 1) {
		if(seamap[x-1][y-1]) {
			seamap[x-1][y-1] = 0;
			dfs(x-1, y-1);
		}
		if(seamap[x][y-1]) {
			seamap[x][y-1] = 0;
			dfs(x, y-1);
		}
		if(seamap[x+1][y-1]) {
			seamap[x+1][y-1] = 0;
			dfs(x+1, y-1);
		}
		if(seamap[x-1][y]) {
			seamap[x-1][y] = 0;
			dfs(x-1, y);
		}
		if(seamap[x+1][y]) {
			seamap[x+1][y] = 0;
			dfs(x+1, y);
		}
		if(seamap[x-1][y+1]) {
			seamap[x-1][y+1] = 0;
			dfs(x-1, y+1);
		}
		if(seamap[x][y+1]) {
			seamap[x][y+1] = 0;
			dfs(x, y+1);
		}
		if(seamap[x+1][y+1]) {
			seamap[x+1][y+1] = 0;
			dfs(x+1, y+1);
		}
	}
}


int main() {
	int w, h;

	REP(i, 52) {
		REP(j, 52) {
			seamap[i][j] = 0;
		}
	}

	while(cin >> w >> h, (w!=0 && h!=0)) {
		FOR(i, 1, h+1) {
			FOR(j, 1, w+1) {
				int p;
				cin >> p;
				seamap[i][j] = p;
			}
		}

		FOR(i, 1, h+1) {
			seamap[i][w+1] = 0;
		}

		FOR(j, 1, w+1) {
			seamap[h+1][j] = 0;
		}
	
		num = 0;

		FOR(i, 1, 50) {
			FOR(j, 1, 50) {
				if(seamap[i][j]) {
					num++;
					dfs(i, j);
				}
			}
		}

		cout << num << endl;
	}
}

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

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

void solve2(int y, int x) {
    using P = pair<int, int>;
    int dx[] = { 0, 1, 1, 1, 0, -1, -1, -1 };
    int dy[] = { 1, 1, 0, -1, -1, -1, 0, 1 };
    queue<P> que;
    que.push(P(y, x));
    while (que.size()) {
        P p = que.front(); que.pop();
        rep(d, 8) {
            int py = p.first + dy[d];
            int px = p.second + dx[d];
            if (0 <= py && py < h && 0 <= px && px < w && hw[py][px] == 1) {
                hw[py][px] = 0;
                que.push(P(py, px));
            }
        }
    }
}

int solve() {

    int ans = 0;

    rep(y, h) {
        rep(x, w) {
            if (hw[y][x] == 1) {
                hw[y][x] = 0;
                solve2(y, x);
                ans++;
            }
        }
    }

    return ans;

}

int main() {

    while (cin >> w >> h and (w or h)) {
        rep(i, h) rep(j, w) cin >> hw[i][j];
        cout << solve() << 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+1][y+1]){f(8)k(x+u[i],y+u[i+8]);d[x+1][y+1]=0;}
}
int main(){
	a=0;
	s(n);s(m);
	f(9801)d[i/99][i%99]=0;
	f(n*m)s(d[i/n][i%n]);
	f(n*m)if(d[i/n][i%n])a++,k(i/n,i%n);
	printf("%d\n",a);
	main();
}

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

int W,H;
int dx[]={-1,0,1,0,-1,-1,1,1};
int dy[]={0,-1,0,1,-1,1,-1,1};
int f[50][50];

void dfs(int x, int y)
{
	f[x][y]=0;
	for(int i=0; i<8; i++)
	{
		int tx=x+dx[i], ty=y+dy[i];
		if(tx<0||ty<0||tx>=W||ty>=H) continue;
		if(f[tx][ty]==0) continue;
		
		dfs(tx,ty);
	}
}

int main()
{
	while(scanf("%d%d", &W,&H), (W||H))
	{
		for(int i=0; i<H; i++)
		for(int j=0; j<W; j++)
			scanf("%d", &f[j][i]);
			
		int ans=0;
		for(int i=0; i<H; i++)
		for(int j=0; j<W; j++)
		{
			if(f[j][i])
			{
				ans++;
				dfs(j,i);
			}
		}
		
		printf("%d\n", ans);
	}
}

C++: #include <iostream>

using namespace std;

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

int w, h;
bool visited[50][50] = {};
int m[50][50];

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

int main(){

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

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

  

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

  cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
using namespace std;

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

#define rep(i,a) for(int i=0;i<(a);++i)
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;

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

bool infield(int x, int y)
{
    return x >= 1 && x <= w && y >= 1 && y <= h;
}

int bfs(int sx, int sy)
{
    queue<P> que;
    que.push(P(sx, sy));
    while(!que.empty()) {
        P p = que.front(); que.pop();
        int x = p.first, y = p.second;
        if(!infield(x, y) || field[y][x] == 0)
            continue;

        field[y][x] = 0;
        for(int i = 0; i < 8; i++) {
            int nx = x + dx[i], ny = y + dy[i];
            que.push(P(nx, ny));
        }
    }
}

int main()
{
    while(cin >> w >> h && w && h) {
        for(int i = 1; i <= h; i++) 
            for(int j = 1; j <= w; j++) 
                cin >> field[i][j];

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

        cout << ans << endl;
    }
}