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 <stdio.h>
#include <iostream>
#include <queue>
#include <vector>


#define rep(i,a,b)(long long i=a;i<b;i++)

using namespace std;


struct POINT {
	int x;
	int y;
	POINT(int X, int Y){
		x = X;y = Y;
	}
};

queue<POINT>	usedWay;
int SizeX,SizeY;
int Map[50][50] = {0};
vector<int>	result;


void SearchIsland(int X, int Y){
	usedWay.push(POINT(X,Y));
	Map[X][Y] = 2;

	while(usedWay.empty() == false){
		POINT NowPos = usedWay.front();
		usedWay.pop();

		if(NowPos.y-1 >= 0){
			//	左上
			if(NowPos.x-1 >= 0 && Map[NowPos.x-1][NowPos.y-1] == 1){
				Map[NowPos.x-1][NowPos.y-1] = 2;
				usedWay.push(POINT(NowPos.x - 1,NowPos.y - 1));
			}
		
			//	真上
			if(					  Map[NowPos.x][NowPos.y-1] == 1){
				Map[NowPos.x][NowPos.y-1] = 2;
				usedWay.push(POINT(NowPos.x,NowPos.y - 1));
			}

			//	右上
			if( NowPos.x + 1 < SizeX && Map[NowPos.x+1][NowPos.y-1] == 1){
				Map[NowPos.x+1][NowPos.y-1] = 2;
				usedWay.push(POINT(NowPos.x + 1,NowPos.y - 1));
			}
		}

		//	左
		if(NowPos.x-1 >= 0 && Map[NowPos.x-1][NowPos.y] == 1){
			Map[NowPos.x-1][NowPos.y] = 2;
			usedWay.push(POINT(NowPos.x - 1,NowPos.y));
		}

		//	右
		if( NowPos.x + 1 < SizeX && Map[NowPos.x+1][NowPos.y] == 1){
			Map[NowPos.x+1][NowPos.y] = 2;
			usedWay.push(POINT(NowPos.x + 1,NowPos.y));
		}

		if(NowPos.y + 1 < SizeY){
			//	左下
			if(NowPos.x-1 >= 0 && Map[NowPos.x-1][NowPos.y+1] == 1){
				Map[NowPos.x-1][NowPos.y+1] = 2;
				usedWay.push(POINT(NowPos.x - 1,NowPos.y + 1));
			}
		
			//	真下
			if(					  Map[NowPos.x][NowPos.y+1] == 1){
				Map[NowPos.x][NowPos.y+1] = 2;
				usedWay.push(POINT(NowPos.x,NowPos.y + 1));
			}

			//	右下
			if( NowPos.x + 1 < SizeX && Map[NowPos.x+1][NowPos.y+1] == 1){
				Map[NowPos.x+1][NowPos.y+1] = 2;
				usedWay.push(POINT(NowPos.x + 1,NowPos.y + 1));
			}
		}


	}
}

int main(void){

	while(true){

		int NumIsland = 0;

		cin >> SizeX;
		cin >> SizeY;

		if(SizeX == 0 && SizeY == 0)
			break;

		int tmp = SizeX;
		SizeX = SizeY;
		SizeY = tmp;

		//	読み込み
		for(int i = 0 ; i < SizeX; ++i)
			for(int j = 0 ; j < SizeY ; ++j)
				cin >> Map[i][j];


		for(int i = 0 ; i < SizeX ; ++i){
			for(int j = 0 ; j < SizeY ; ++j){
				if(Map[i][j] == 1){
					SearchIsland(i,j);
					NumIsland++;
				}
			}
		}

		result.push_back(NumIsland);
	}

	for(auto it = result.begin() ; it != result.end(); ++it)
		cout << *it << endl;

	return 0;
}

C++: #include<iostream>
#include<cstring>
int map[99][99];
int xi[]={-1,-1,-1,0,0,1,1,1};
int yi[]={-1,0,1,-1,1,-1,0,1};

using namespace std;

void map_clear(int x,int y)
{
  map[x][y]=0;
  for(int i=0;i<8;i++)
    {
      if(x+xi>=0 && y+yi>=0 && map[x+xi[i]][y+yi[i]] == 1)
	map_clear(x+xi[i],y+yi[i]);
    }
}

int main()
{
  int w,h;
  for(;cin>>w>>h,w;)  
    {
      memset(map,0,sizeof(map));
      for(int i=0;i<h;i++)
	for(int j=0;j<w;j++)
	  cin>>map[i][j];
      int c=0;
      for(int i=0;i<h;i++)
	for(int j=0;j<w;j++)
	  if(map[i][j]==1)
	    {
	      c++;
	      //   cout<<i<<"  "<<j<<endl;
	      map_clear(i,j);
	    }
      cout<<c<<endl;
    }
}

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

using namespace std;
using ll = long long int;

vector<pair<int, int>> dirs = {
    {-1, -1}, {-1, -0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, -0}, {1, 1},
};

void dfs(vector<vector<int>> &field, vector<vector<bool>> &visit, int x, int y,
         function<bool(int, int)> check) {
  visit[y][x] = true;

  for (const auto &dir : dirs) {
    int nx = x + dir.first;
    int ny = y + dir.second;
    if (check(nx, ny))
      dfs(field, visit, nx, ny, check);
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  while (true) {
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0)
      return 0;
    vector<vector<int>> field(h, vector<int>(w));
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        cin >> field[y][x];
      }
    }
    vector<vector<bool>> visit(h, vector<bool>(w, false));
    auto check = [&](int x, int y) {
      return (0 <= x && x < w) && (0 <= y && y < h) && field[y][x] == 1 &&
             !visit[y][x];
    };
    int ans = 0;
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        if (visit[y][x] || field[y][x] == 0)
          continue;
        ans++;
        dfs(field, visit, x, y, check);
      }
    }
    cout << ans << endl;
  }

  return 0;
}

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

vector<vector<int>> c(50, vector<int>(50));

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

vector<vector<bool>> seen(50, vector<bool>(50));
void dfs(vector<vector<int>> &c, int sy, int sx, int h, int w) {
    seen[sy][sx] = true;
    for(int i = 0; i < 8; i++){
        int ey = sy + dy[i], ex = sx + dx[i]; 
        if(ey < 0 || ey >= h || ex < 0 || ex >= w) continue;
        if(c[ey][ex] == 0 || seen[ey][ex]) continue;
        dfs(c, ey, ex, h, w);
    }
}

int main(){
    int w,  h;
    vector<int> cnt;
    int check = -1;
    cin >> w >> h;
    while (w != 0 || h != 0)
    {
        for(int i = 0 ; i < h; i++){
            for (int j = 0; j < w; j++)
            {
               cin >> c[i][j];
               seen[i][j] = false;
            }
        }
        cnt.push_back(0);
        check++;
        for (int i = 0; i < h; i++)
        {
            for (int j = 0; j < w; j++)
            {
                if(!seen[i][j] && c[i][j] == 1){
                    dfs(c, i, j, h, w);
                    cnt[check]++;
                }
            }
        }
        cin >> w >> h;
    }
    for (int i = 0; i < cnt.size(); i++)
    {
        cout << cnt[i] << endl;
    }
    
}

C++: #include<iostream>
using namespace std;
#define REP(i,n) for(int i=0;i<(int)(n);i++)
const int WMAX = 100, HMAX = 100;
int w, h;
int island[WMAX][HMAX];

void dfs(int x, int y) {
  if (x < 0 or w <= x) return;
  if (y < 0 or h <= y) return;
//  cout << "(" << x << " " << y << ")" << endl;
  if (!island[x][y]) return;
  island[x][y] = false;
  int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1},
      dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};

  REP(dir, 8) {
    dfs(x + dx[dir], y + dy[dir]);
  }
}

int solve() {
  int ans = 0;
  REP(y, h) {
    REP(x, w) {
      if (island[x][y]) {
        ans++;
        dfs(x, y);
      }
    }
  }
  return ans;
}

int main() {
  while (cin >> w >> h and w) {
    REP(y, h) {
      REP(x, w) {
        int c;
        cin >> c;
        island[x][y] = c;
      }
    }

    cout << solve() << endl;
  }
}