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 <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <complex>
#include <array>
#include <iomanip>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

template<typename T> void chmin(T &a, T b) { if (a > b) a = b; }
template<typename T> void chmax(T &a, T b) { if (a < b) a = b; }

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }
#define INF 1LL<<60

void dfs(int cur_x, int cur_y, vector<vector<int> >& c, vector< vector<bool> >& visited, int H, int W) {
    visited[cur_y][cur_x] = true;
    for(int dx=-1; dx<=1; dx++)
        for(int dy=-1; dy<=1; dy++)
            if(
            (dx !=0 || dy != 0)
            && (cur_x+dx>=0)
            && (cur_x+dx<W)
            && (cur_y+dy>=0)
            && (cur_y+dy<H)
            && c[cur_y+dy][cur_x+dx] == 1
            && !visited[cur_y+dy][cur_x+dx]) {
                dfs(cur_x+dx, cur_y+dy, c, visited, H, W);
            }
}

int main() {
    while(1) {
        int W, H;
        cin >> W >> H;
        if(W==0 && H==0) break;
        vector<vector<int> > c(H, vector<int>(W));
        vector<vector<bool> > visited(H, vector<bool>(W, false));
        REP(i, H)
            REP(j, W) cin >> c[i][j];

        int ans = 0;
        REP(i, H)
            REP(j, W)
                if(!visited[i][j] && c[i][j] == 1) {
                    ans++;
                    dfs(j, i, c, visited, H, W);
                }
        cout << ans << endl;
    }
    return 0;
}

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

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

void dfs(int y, int x, int id) {
    island[y][x] = id;
    rep(i, 8) {
        int nx = x+dx[i];
        int ny = y+dy[i];
        if(nx>=0 && nx<w && ny>=0 && ny<h &&
                c[ny][nx] == 1 && island[ny][nx] != id) {
            dfs(ny, nx, id);
        }
    }
}
    
int main(void){
    while(cin >> w >> h && w) {
        rep(y, h) rep(x, w) cin >> c[y][x];
        ans = 0;
        memset(island, -1, sizeof(island));
        rep(y, h) {
            rep(x, w) {
                if(c[y][x]==1 && island[y][x]==-1) {
                    dfs(y, x, ans);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
}

C++: #include<iostream>
#include<queue>
#include<utility>
using namespace std;
void search(int, int);
int c[50][50] = {}, map[50][50] = {};
void mapDraw(int,int);
queue< pair<int, int> > q;
int w, h;

int main() {

	while (1) {
		
		//initialize
		int count = 0;
		for (int i = 0; i < 50; i++) {
			for (int j = 0; j < 50; j++) {
				c[i][j] = 0;
				map[i][j] = 0;
			}
		}


		cin >> w >> h;
		if (w == 0 && h == 0) break;
		//input
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> c[i][j];
			}
		}

		//search
		//unresearched:0, land:2, sea:1
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (map[i][j] == 0) {
					map[i][j] = c[i][j] + 1;
					if (c[i][j] == 1) {
						q.push(make_pair(i, j));
						count++;
					}
				}

				pair<int, int> cur = make_pair(i, j);
				while (!q.empty()) {
					cur = q.front();
					int x = cur.second;
					int y = cur.first;
					q.pop();

					//cerr << "( " << y << ", " << x << " )now"<< endl;

					for (int a = -1; a <= 1; a++) {
						for (int b = -1; b <= 1; b++) search(y + a, x + b);
					}
				}
			}
		}

		cout << count << endl;
	}


	return 0;
}

void search(int i, int j) {
	if (map[i][j] == 0) {
		map[i][j] = c[i][j] + 1;
		if (c[i][j] == 1) q.push(make_pair(i, j));
	}
	//mapDraw(w, h);
}

void mapDraw(int w, int h) {
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			cerr << map[i][j];
		}
		cerr << endl;
	}
	cerr << endl;
}

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

struct pos{
	int x;
	int y;
};

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

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

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

		stack<pos> st;
		int island = 0;
		for(int i = 1; i <= h; i++){
			for(int j = 1; j <= w; j++){
				//島を探す
				if(marine[i][j] == 1){
					pos p;
					p.y = i;
					p.x = j;
					st.push(p);
					marine[i][j] = 0;
					while(!st.empty()){
						pos p1 = st.top();
						st.pop();
						int x1[] = {-1,  0, 1, 0, -1,  1, 1, -1};
						int y1[] = {0 , -1, 0, 1, -1, -1, 1,  1};
						for(int k = 0; k < 8; k++){
							p = p1;
							p.y += y1[k];
							p.x += x1[k];
							if(marine[p.y][p.x] == 1){
								st.push(p);
								marine[p.y][p.x] = 0;
							}
						}
					}
					island++;
				}


			}
		}

		cout << island << endl;
	}
}

C++: #include<iostream>
using namespace std;
void dfs(int,int);
int a[52][52]={};

int main(){
  while(1){
    int count=0,w,h;
    cin>>w>>h;
    if(w+h==0)break;
    
    for(int i=1;i<=h;i++)
      for(int j=1;j<=w;j++)
	cin>>a[i][j];

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

  void dfs(int x,int y){
    a[y][x]=0;
    int nx,ny;
    int dx[8]={1,1,1,0,-1,-1,-1,0};
    int dy[8]={1,0,-1,-1,-1,0,1,1};
    for(int k=0;k<8;k++){
	nx=x+dx[k];
	ny=y+dy[k];
	if(a[ny][nx]==1){
	  dfs(nx,ny);
	}
    }
  }