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 _GLIBCXX_DEBUG
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

void dfs (vector<vector<int>> &vec, vector<vector<int>> &shd, int h, int w) {
  if (h < 0 || h >= vec.size() || w < 0 || w >= vec[0].size()) return;
  else if (!vec[h][w]) return;
  else if (shd[h][w]) return;
  else shd[h][w] = 1;

  dfs(vec, shd, h + 1, w);
  dfs(vec, shd, h , w + 1);
  dfs(vec, shd, h - 1, w);
  dfs(vec, shd, h, w - 1);
  dfs(vec, shd, h + 1, w + 1);
  dfs(vec, shd, h + 1, w - 1);
  dfs(vec, shd, h - 1, w + 1);
  dfs(vec, shd, h - 1, w - 1);

  return;
}
     
int main() {
  int w, h, cou=0;
  vector<int> vec_ans(1000,2147483647);

  cin >> w >> h;

  do {
    int ans=0;
    vector<vector<int>> vec(h, vector<int>(w, 0));
    vector<vector<int>> shd(h, vector<int>(w, 0));
    rep(i, h) rep(j, w) cin >> vec[i][j];

    rep(i, h) rep(j, w) {
      if (!shd[i][j] && vec[i][j]) {
        ++ans;
        dfs(vec, shd, i, j);
      }
    }

    vec_ans[cou] = ans;
    ++cou;
    cin >> w >> h;
  } while (w || h);

  for (int i=0; vec_ans[i] != 2147483647; ++i) {
    cout << vec_ans[i] << endl;
  }
}

C++: #include<stdio.h>
#include<string.h>
#include<utility>
#include<iostream>
#include<queue>

using namespace std;
typedef long long ll;
 
#define REP(i,n) for(int i=0;i<(int)(n);i++)
 
int main()
{
    int ans,w,h;
    int c[55][55];
    queue<pair<int,int> > que;
    int dx[8]={1,1,1,0,-1,-1,-1,0};
    int dy[8]={-1,0,1,1,1,0,-1,-1};
    while(1==1)
    {
        ans=0;
        REP(i,55)REP(j,55)c[i][j]=10000;
        scanf("%d %d\n",&w,&h);
        if(w==0&&h==0)return 0;
        REP(i,h)REP(j,w)scanf("%d",&c[i+1][j+1]);
        for(int i=1;i<=h;i++)
        {
            for(int j=1;j<=w;j++)
            {
                if(c[i][j]==1)
                {
                    ans++;
                    c[i][j]=0;
                    while(!que.empty())que.pop();
                    que.push(pair<int,int>(i,j));
                    pair<int,int> p;
                    while(!que.empty())
                    {
                        p=que.front();
                        que.pop();
                        REP(k,8)
                        {
                            if(c[p.first+dy[k]][p.second+dx[k]]==1)
                            {
                                que.push(pair<int,int>(p.first+dy[k],p.second+dx[k]));
                                c[p.first+dy[k]][p.second+dx[k]]=0;
                            }
                        }
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

C++: #include<iostream>

int field[52][52];

void clear();

bool walk(int x, int y, int& a);

int main(){

	int w,h,ans;
	
	const int dif = 2;
	
	while(true){

		std::cin >> w >> h;
		if(w==0 && h==0)
			return 0;

		clear();
		for(int i=1; i<=h; i++)
			for(int j=1; j<=w; j++)
				std::cin >> field[i][j];

		ans = 2;
		for(int i=1; i<=h; i++)
			for(int j=1; j<=w; j++)
				if(walk(i, j, ans))
					ans++;

		std::cout << ans-dif << std::endl;
	}

	return 0;
}

void clear(){
	for(int i=0; i<52; i++)
		for(int j=0; j<52; j++)
			field[i][j] = 0;
}

bool walk(int x, int y, int& a){
	if(field[y][x] != 1)
		return false;

	field[y][x] = a;
	
	for(int i=-1; i<=1; i++)
		for(int j=-1; j<=1; j++)
			walk(x+i, y+j, a);

	return true;
}

C++: #include<iostream>
#include<bitset>

using namespace std;

int dx[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
int dy[8] = { 0, -1, -1, -1, 0, 1, 1, 1 };
int w, h, c[50][50];
void dfs(int c[50][50], int x, int y);

int main() {
	while (true) {
		cin >> w >> h;
		if (w == 0 && h == 0)	break;
		int counter = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> c[j][i];
			}
		}

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

	}
	return 0;
}

void dfs(int c[50][50], int x, int y) {	//(x, y) = 1ということを発見済み
	c[x][y] = 0;
	for (int i = 0; i < 8; i++) {
		if (0 <= x + dx[i] && x + dx[i] < w && 0 <= y + dy[i] && y + dy[i] < h) {
			if (c[x + dx[i]][y + dy[i]] == 1) {
				dfs(c, x + dx[i], y + dy[i]);
			}
		}
	}
}

C++: #include <iostream>

#define W_MAX 55
#define H_MAX 55

int f[H_MAX][W_MAX];

void dfs(int y,int x);


using namespace std;

int main(void)
{
	int w;
	int h;
	int i1,i2;
	int count;
	int a;

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

		//cout << "a";

		for(i1=0;i1<H_MAX;i1++){
			for(i2=0;i2<W_MAX;i1++){
				f[i1][i2]=0;

			}
		}

		for(i1=1;i1<=h;i1++){
			for(i2=1;i2<=w;i2++){
				cin >> a;
				//f[i1][i2] = a;
			}
		}


		count=0;
		for(i1=1;i1<=h;i1++){
			for(i2=1;i2<=w;i2++){
				if (f[i1][i2] == 1){
					count++;
					dfs(i1,i2);
				}
			}
		}

		cout << count << endl;

	}
	return 0;
}


void dfs(int y,int x)
{
	int i1,i2;

	f[y][x]=0;
	for(i1=-1;i1<2;i1++){
		for(i2=-1;i2<2;i2++){

			if(f[y+i1][x+i2]==1) dfs(y+i1,x+i2);
		}
	}

	return;
}

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

int w, h, x, y, ans;
int mp[52][52];
int visited[52][52];
int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy[8] = {-1, 1, 1, -1, 0, 1, -1, 0};


void bfs(int x, int y){
  int i;
  visited[y][x] = 1;
  for(i = 0; i < 8; i++){
    if(visited[y + dy[i]][x + dx[i]] == 0 && mp[y + dy[i]][x + dx[i]] == 1){
      bfs(x + dx[i], y + dy[i]);
    }
  }
}

int main(void){
  while(1){
    cin >> w >> h;
    if(w == 0) break;

    for(y = 0; y < h + 2; y++){
      if(y == 0 || y == h + 1){
        for(x = 0; x < w + 2; x++) mp[y][x] = 0;
      }else{
        for(x = 0; x < w + 2; x++){
          if(x == 0 || x == w + 1){
            mp[y][x] = 0;
          }else{
            cin >> mp[y][x];
          }
        }
      }
    }
    for(y = 0; y < h + 2; y++){
      for(x = 0; x < w + 2; x++){
        visited[y][x] = 0;
      }
    }
    ans = 0;
    for(y = 1; y < h + 1; y++){
      for(x = 1; x < w + 1; x++){
        if(mp[y][x] == 1 && visited[y][x] == 0){
          ans++;
          bfs(x, y);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

vector<vector<int> > imap(52,vector<int>(52,0));
int w,h;

void dfs(int nowx,int nowy){
  imap[nowx][nowy]=0;
  for(int i=-1;i<2;++i){
    for(int j=-1;j<2;++j){
      if(imap[nowx+i][nowy+j]==1) dfs(nowx+i,nowy+j);
    }
  }
}

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

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