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;

void dfs(int i, int j, const vector<vector<int> >& c, vector<vector<int> >& arrived, int count) {
    arrived[i][j] = count;

    if (c[i + 1][j] == 1 && arrived[i + 1][j] == 0) {
        dfs(i + 1, j, c, arrived, count);
    }
    if (c[i - 1][j] == 1 && arrived[i - 1][j] == 0) {
        dfs(i - 1, j, c, arrived, count);
    }
    if (c[i][j + 1] == 1 && arrived[i][j + 1] == 0) {
        dfs(i, j + 1, c, arrived, count);
    }
    if (c[i][j - 1] == 1 && arrived[i][j - 1] == 0) {
        dfs(i, j - 1, c, arrived, count);
    }
    if (c[i + 1][j + 1] == 1 && arrived[i + 1][j + 1] == 0) {
        dfs(i + 1, j + 1, c, arrived, count);
    }
    if (c[i + 1][j - 1] == 1 && arrived[i + 1][j - 1] == 0) {
        dfs(i + 1, j - 1, c, arrived, count);
    }
    if (c[i - 1][j + 1] == 1 && arrived[i - 1][j + 1] == 0) {
        dfs(i - 1, j + 1, c, arrived, count);
    }
    if (c[i - 1][j - 1] == 1 && arrived[i - 1][j - 1] == 0) {
        dfs(i - 1, j - 1, c, arrived, count);
    }
}

void Main() {
    while (true) {
        int w, h;
        cin >> w >> h;
        if (w == 0 && h == 0) {
            break;
        }

        vector<vector<int> > c(h + 2, vector<int>(w + 2, 0));
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                int ct;
                cin >> ct;
                c[i + 1][j + 1] = ct;
            }
        }

        vector<vector<int> > arrived(h + 2, vector<int>(w + 2, 0));
        int count = 0;
        for (int i = 0; i < c.size(); ++i) {
            for (int j = 0; j < c[i].size(); ++j) {
                if (c[i][j] == 1 && arrived[i][j] == 0) {
                    ++count;
                    dfs(i, j, c, arrived, count);
                }
            }
        }

        set<int> ans;
        for (int i = 0; i < c.size(); ++i) {
            for (int j = 0; j < c[i].size(); ++j) {
                if (arrived[i][j] > 0) {
                    ans.insert(arrived[i][j]);
                }
            }
        }
        cout << ans.size() << endl;
    }
}

int main() {
    std::cout << std::fixed << std::setprecision(15);
    Main();
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<cstdlib>


#define rep(n,cnt) for(int cnt=0;cnt<n;++cnt)

using namespace std;

template <typename TYPE>
void printArray(TYPE* array, int size){
  rep(size,i) cout << array[i] << "\t" << flush;
  cout << endl;
}

int StringToInt(string str){
  return atoi(str.c_str());
}

#define MAX_W 50
#define MAX_H 50

class State{
public:
  int y;
  int x;
  int island_count;
};

int w,h,c[MAX_W][MAX_H];
int count = 0;

void dfs(int sy, int sx){
  c[sy][sx] = 0;

  for(int vy=-1;vy<=1;++vy){
    for(int vx=-1;vx<=1;++vx){
      int y = sy + vy;
      int x = sx + vx;
      if(c[y][x] == 1) dfs(y,x);
    }
  }
}

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

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

using namespace std;


int W, H;
int map[55][55];



void dfs(int cx, int cy) {
	map[cy][cx] = 0;
	
	for (int dx = -1; dx <= 1; dx++) {
		for (int dy = -1; dy <= 1; dy++) {
			if (map[cy + dy][cx + dx] == 1) dfs(cx + dx, cy + dy);
		}
	}
}

int solver() {
	int cnt = 0;
	
	for (int i=0; i<H; i++) {
		for (int j=0; j<W; j++) {
			if (map[i][j] == 1) {
				dfs(j, i);
				cnt++;
			}
		}
	}
	
	return cnt;
}

int main() {
	
	while (1) {
		cin >> W >> H; if (W==0 && H==0) break;
		
		memset(map, 0, sizeof(map));
		for (int i=0; i<H; i++)
			for (int j=0; j<W; j++) cin >> map[i][j];
		
		cout << solver() << endl;
	}
	
	return 0;
}

C++: #include <iostream>

using namespace std;

#define rep(i, n) for(int i = 0; i < (n); ++i)
#define repi(i, a, n) for(int i = a; i < (n); ++i)

int w, h;
int island[100][100];

void dfs(int x, int y) {
  island[y][x] = 0;

  repi(mat_y, -1, 2) {
    repi(mat_x, -1, 2) {
      if (x + mat_x < 0 || y + mat_y < 0 || x + mat_x >= w || y + mat_y >= h) continue;
      if (island[y + mat_y][x + mat_x]) dfs(x + mat_x, y + mat_y);
    }
  }
}

int main(){
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0) break;

    rep(y, h) {
      rep(x, w) {
        cin >> island[y][x];
      }
    }
    int cnt = 0;
    rep(y, h) {
      rep(x, w) {
        if (island[y][x]) {
          ++cnt;
          dfs(x, y);
        }
      }
    }
    cout << cnt << endl;
  }
}

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

// How Many Islands?(1160)
const int dx[] = {0, -1, -1, -1, 0, 1, 1, 1};
const int dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};

void solve(vector<vector<int> >&f, int x, int y)
{
	if (x < 0 || x >= (int)f[0].size() || y < 0 || y >= (int)f.size() || f[y][x] == 0)
		return;

	f[y][x] = 0;
	for (int i = 0; i < 8; ++i)
		solve(f, x+dx[i], y+dy[i]);
	return;
}

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

		vector<vector<int> > f(h, vector<int>(w));
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j)
				cin >> f[i][j];
		}

		int ans = 0;
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j) {
				if (f[i][j]) {
					solve(f, j, i);
					++ans;
				}
			}
		}
		cout << ans << endl;
	}


	return 0;
}

C++: #include <cstdio>
#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define all(a) (a).begin(), (a).end()

int w, h;
int c[51][51];

void dfs( int i, int j )
{
  c[i][j] = 0;

  for( int dx = -1; dx <= 1; ++dx ) for( int dy = -1; dy <= 1; ++dy )
  {
    int ni = i+dy, nj = j+dx;

    if( ni >= 0 && ni < h && nj >= 0 && nj < w && c[ni][nj] == 1 )
      dfs( ni, nj );
  }

  return;
}

int main()
{
  while( scanf( "%d%d", &w, &h ), w|h )
  {
    rep( i, h ) rep( j, w )
      scanf( "%d", &c[i][j] );

    int cnt = 0;
    rep( i, h ) rep( j, w ) if( c[i][j] == 1 )
      dfs( i, j ), ++cnt;    

    printf( "%d\n", cnt );
  }
  
  return 0;
}