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 <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <cstring>

using namespace std;
using ll = long long;
using pint = pair<int, int>;
using Graph = vector<vector<int>>;

ll INF = 1LL << 60;

int W, H;

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

int main() {
	while (cin >> W >> H) {
		if (W == 0 && H == 0) break;
		Graph field(H, vector<int>(W));
		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] == 1) {
					dfs(field, h, w);
					count++;
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}

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

int W,H;
int C[111][111];

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

bool used[111][111];
void solve(int x,int y){
  if( used[x][y] ) return;
  used[x][y] = true;
  for(int i=0;i<8;i++){
    int nx = x + dx[i], ny = y + dy[i];
    if( nx < 0 || ny < 0 || nx >= W || ny >= H ) continue;
    if( C[nx][ny] == 1 )
      solve( nx, ny );
  }
}

int main(){

  while(  cin >> W >> H && (W||H) ){
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
	cin >> C[j][i];
      }
    }
    
    int res = 0;
    memset(used,0,sizeof(used));
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
	if( !used[j][i] && C[j][i] == 1 )
	  {
	    res++;
	    solve(j,i);
	  }
      }
    }
    cout << res << endl;
  }


}

C++: #include <iostream>

using namespace std;
int field[50][50];
int w, h, ans;
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1}, dy[8] = {-1, -1, -1, 0,  0, 1, 1, 1};

void check(int x, int y)
{
    if (field[x][y])
        field[x][y] = 0;
    else
        return;
        
    for (int i=0; i<8; i++) {
        int nx = x+dx[i], ny = y+dy[i];
        if (0<= nx && nx<w && 0<=ny&&ny<h) {
            check(nx, ny);
        }
    }
}

int main()
{
    while (cin>>w>>h, w||h) {
        for (int y=0; y<h; y++) {
            for (int x=0; x<w; x++) {
                int i; cin >> i;
                field[x][y] = i;
                }
           }
       
       ans = 0;
       for (int y=0; y<h; y++) {
          for (int x=0; x<w; x++) {
               if (field[x][y]) {
                       ans++;
                       check(x, y);
               }  
            }   
        }

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

C++: #include <math.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define ll long long
#define INF 1e9
#define MOD 1e9 + 7
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, a, n) for (int i = a; i < n; i++)
#define all(in) in.begin(), in.end()
int c[51][51];
int w, h;
void dfs(int x, int y) {
    c[x][y] = 0;
    for (int i = -1; i <= 1; i++) {
        for (int k = -1; k <= 1; k++) {
            int new_x = x + i, new_y = y + k;
            if (0 <= new_x && new_x < h && 0 <= new_y && new_y < w &&
                c[new_x][new_y] == 1) {
                dfs(new_x, new_y);
            }
        }
    }
}

int main() {
    while (true) {
        int ans = 0;
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        rep(i, h) rep(k, w) cin >> c[i][k];
        for (int x = 0; x < h; x++) {
            for (int y = 0; y < w; y++) {
                if (c[x][y] == 1) {
                    ans++;
                    dfs(x, y);
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

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

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

int mesh[50][50];
int n, m;

struct Point
{
	int x;
	int y;
};

int main()
{
	deque<Point> stack;
	while (true)
	{
		scanf("%d %d", &n, &m);
		if (n == 0 && m == 0) break;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				int a;
				scanf("%d", &a);
				mesh[i][j] = a;
			}
		}
		int count = 0;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if (mesh[i][j] == 1)
				{
					Point pt = {j, i};
					stack.push_front(pt);
					mesh[i][j] = 0;
					while (!stack.empty())
					{
						Point point = stack.front();
						stack.pop_front();
						for (int k = 0; k < 8; k++)
						{
							if (mesh[point.y + dy[k]][point.x + dx[k]] == 1)
							{
								Point pt = {point.x + dx[k], point.y+dy[k]};
								stack.push_front(pt);
								mesh[point.y + dy[k]][point.x + dx[k]] = 0;
							}
						}
					}
					count++;
				}
			}
		}
		printf("%d\n", count);
	}
}

C++: #include<iostream>
#include<sstream>
#include<fstream>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<functional>
#include<utility>
#include<bitset>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cstdio>

using namespace std;

#define REP(i,n) for(int i=0;i<int(n);i++)
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
typedef long long ll;
typedef pair<int,int> P;

int L[55][55];
int w,h;
bool inside(int i,int j){
	return (1<=i && i<=h && 1<=j && j<=w);
}

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

void dfs(int i,int j){
	L[i][j]=0;
    REP(k,8){
    int x=i+dx[k],y=j+dy[k];
	if(inside(x,y)){
		if(L[x][y]==1) dfs(x,y);
	}
    }
}

int main(){
	  int i,j;

	  while(1){
		  cin >> w >> h;
		  if(w==0 && h==0) break;
		  for(i=1;i<=h;i++) for(j=1;j<=w;j++) cin >> L[i][j];

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

		  cout << ans << endl;

	  }

	  return 0;
}