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;

int w, h;

vector<vector<int>> makearea(void) {
	vector<vector<int>> area(h);

	for(int i = 0; i < h; i++) {
		for(int j = 0; j < w; j++) {
			int tmp;
			cin >> tmp;
			area[i].push_back(tmp);
		}
	}
	
	/*for(auto i : area) {
		for(auto j : i) {
			cout << j << " ";
		}
		cout << endl;
	}*/

	return area;
}

void dfs(int y, int x, vector<vector<int>>& area, vector<vector<bool>>& checker) {
	if(x != w - 1) {
		if(area[y][x + 1] == 1 and checker[y][x + 1] == true) {
			checker[y][x + 1] = false;
			dfs(y, x + 1, area, checker);
		}
	}
	if(x != w - 1 and y != h - 1){
		if(area[y + 1][x + 1] == 1 and checker[y + 1][x + 1]) {
			checker[y + 1][x + 1] = false;
			dfs(y + 1, x + 1, area, checker);
		}
	}
	if(y != h - 1){
		if(area[y + 1][x] == 1 and checker[y + 1][x]) {
			checker[y + 1][x] = false;
			dfs(y + 1, x, area, checker);
		}
	}
	if(x != 0 and y != h - 1){
		if(area[y + 1][x - 1] == 1 and checker[y + 1][x - 1]) {
			checker[y + 1][x - 1] = false;
			dfs(y + 1, x - 1, area, checker);
		}
	}
	if(x != 0){
		if(area[y][x - 1] == 1 and checker[y][x - 1]) {
			checker[y][x - 1] = false;
			dfs(y, x - 1, area, checker);
		}
	}
	if(x != 0 and y != 0){
		if(area[y - 1][x - 1] == 1 and checker[y - 1][x - 1]) {
			checker[y - 1][x - 1] = false;
			dfs(y - 1, x - 1, area, checker);
		}
	}
	if(y != 0){
		if(area[y - 1][x] == 1 and checker[y - 1][x]) {
			checker[y - 1][x] = false;
			dfs(y - 1, x, area, checker);
		}
	}
	if(x != w - 1 and y != 0){
		if(area[y - 1][x + 1] == 1 and checker[y - 1][x + 1]) {
			checker[y - 1][x + 1] = false;
			dfs(y - 1, x + 1, area, checker);
		}
	}
}

int search(vector<vector<int>>& area) {
	int sum = 0;
	vector<vector<bool>> checker;
	checker = vector<vector<bool>>(area.size(), vector<bool>(area[0].size(), true));

	for(int i = 0; i < h; i++) {
		for(int j = 0; j < w; j ++) {
			if(area[i][j] == 1 and checker[i][j]) {
				checker[i][j] = false;
				dfs(i, j, area, checker);
				sum++;
			}
		}
	}
	return sum;
}

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

		vector<vector<int>> area = makearea();
		int sum = search(area);

		cout << sum << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>

using namespace std;
const int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};

int main(void) {
	int w,h;
	while(cin >> w >> h && w && h) {
		vector< vector<int> > data(w+2,vector<int>(h+2,0));
		for (int i = 1; i <= h; ++i) {
			for (int j = 1; j <= w; ++j) {
				cin >> data[i][j];
			}
		}

		int nx,ny;
		int count = 0;
		queue<int> fx,fy;
		for (int i = 1; i <= h; ++i) {
			for (int j = 1; j <= w; ++j) {
				if (data[i][j] == 1) {
					nx = j;
					ny = i;
					data[i][j] = 0;
					fx.push(nx);
					fy.push(ny);
					++count;

					while (!fx.empty()) {
						nx = fx.front();
						ny = fy.front();
						fx.pop();
						fy.pop();
						for (int d = 0; d < 8; ++d) {
							if (data[ny+dy[d]][nx+dx[d]] == 1) {
								fx.push(nx+dx[d]);
								fy.push(ny+dy[d]);
								data[ny+dy[d]][nx+dx[d]] = 0;
							}
						}
					}
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}

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

#define PIAR pair<int, int>

struct pos{
    int x;
    int y;
};

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

        stack<PIAR> st;
        int island = 0;
        int dx[] = {-1,  0, 1, 0, -1,  1, 1, -1};
        int dy[] = {0 , -1, 0, 1, -1, -1, 1,  1};
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= w; j++){
                //島を探す
                if(field[i][j] == 1){
                    st.push(PIAR(i, j));
                    field[i][j] = 0;
                    while(!st.empty()){
                    	int x = st.top().first;
                    	int y = st.top().second;
                        st.pop();
                        for(int k = 0; k < 8; k++){
                            int nextX = x + dx[k];
                            int nextY = y + dy[k];
                            if(field[nextX][nextY] == 1){
                                st.push(PIAR(nextX, nextY));
                                field[nextX][nextY] = 0;
                            }
                        }
                    }
                    island++;
                }

            }
        }

        cout << island << endl;
    }
}

C++: #include <bits/stdc++.h>
#define rep(i,s,n) for(int i=s;i<n;i++)
#define pb push_back
#define all(x) (x).begin(),(x).end()
using namespace std;
using P=pair<int,int>;
using G=vector<vector<int>>;
using ll=long long;

string subsum(int* A,int n,int m){
  bitset<2010> E(1);

  rep(i,0,n){
    E |= (E<<A[i]);
  }

  if(E.test(m)) return "yes";
  else return "no";
}

vector<bool> seen;
vector<int> d;
vector<int> f;

int H,W;
vector<vector<int>> grid;

void dfs(int h, int w){
  grid[h][w]=0;

  for(int dh=-1;dh<=1;dh++){
    for(int dw= -1;dw<=1;dw++){
      int nh=h+dh; int nw=w+dw;

      if(nh<0||nw<0||nh>=H||nw>=W) continue;
      //探索済みまたは海
      if(grid[nh][nw]==0) continue;

      dfs(nh,nw);
    }
  }
}

int main(int argc,char **argv){
  while(1){
    cin>>W>>H;
    if(W==0 && H==0) break;

    grid.assign(H,vector<int>(W,0));
    rep(h,0,H){
      rep(w,0,W) cin>>grid[h][w];
    }

    int cnt=0;
    rep(h,0,H){
      rep(w,0,W){
        if(grid[h][w]==0) continue;
        dfs(h,w);
        cnt++;
      }
    }

    cout<<cnt<<endl;


  }
  }

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

int CountIsland(int w,int h,int c[][50]);
void CheckIsland(int w,int h,int c[][50]);
void debugg(int w,int h,int c[][50]);
int main(){
	int w,h;
	int c[50][50];
	while(true){
		for(int i=0;i<50;i++){
			for(int j=0;j<50;j++){
				c[i][j]=-1;
			}
		}
		cin >> w >> h;
		if(w==0&&h==0){break;}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin >> c[i][j];
			}
		}
		cout << CountIsland(w,h,c) << endl;
	}
	return 0;
}
int CountIsland(int w,int h,int c[][50]){
	int r=0;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			if(c[i][j]==1){
				r++;
				CheckIsland(j,i,c);
			}
		}
	}
	return r;
}
void debugg(int w,int h,int c[][50]){
	cout << endl;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cout << c[i][j] << " ";
		}
		cout << endl;
	}
}
void CheckIsland(int w,int h,int c[][50]){
	c[h][w] = 0;
	for(int i=-1;i<=1;i++){
		for(int j=-1;j<=1;j++){
			if(i == -1 && h == 0 || j == -1 && w == 0 || i == 1 && h == 49 || j == 1 && w == 49||i == 0 && j == 0){continue;}
			if(c[i+h][j+w]==1){
				CheckIsland(j+w,i+h,c);
			}
		}
	}
}