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 <queue>
#include <utility>

using namespace std;

using Point = pair<int,int>;

int map[50][50];

void BFS(const Point &p0){
  queue<Point> q;
  q.push(p0);
  while(!q.empty()){
    auto p = q.front();q.pop();
    int y = p.first , x = p.second;
    map[y][x] = 0;

    for(int i=-1;i<=1;++i){
      for(int j=-1;j<=1;++j){
        if(map[i+y][j+x]){
          q.push(Point(i+y,j+x));
        }
      }
    }
  }

}

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

    int num = 0;
    for(int i=1;i<h+1;++i){
      for(int j=1;j<w+1;++j){
        if(map[i][j]){
          BFS(Point(i,j));
          num++;
        }
      }
    }
    cout << num << endl;
  }

  return 0;
}

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

using namespace std;

int field[52][52];

void DFS(int x, int y){
    if(field[x][y]==1){
        field[x][y]=-1;
        DFS(x-1,y-1);
        DFS(x,y-1);
        DFS(x+1,y-1);
        DFS(x-1,y);
        DFS(x+1,y);
        DFS(x-1,y+1);
        DFS(x,y+1);
        DFS(x+1,y+1);
    }
}


int main(){
    int w,h;
    while(cin>>w>>h){
        if(w==0&&h==0)break;
        for(int i=0; i<50; i++){
            for(int j=0; j<50; j++){
                field[i][j]=0;
            }
        }
        
        for(int i=1; i<=h; i++){
            for(int j=1; j<=w; j++){
                cin>>field[i][j];
            }
        }
        int sum=0;
        for(int i=1; i<=h; i++){
            for(int j=1; j<=w; j++){
                if(field[i][j]==1){
                    sum++;
                    DFS(i,j);
                }
            }
        }
        cout<<sum<<endl;
    }
}

C++: ///
// File:  1160.cpp
// Author: ymiyamoto
//
// Created on Sun Nov 12 16:15:22 2017
//

#include <array>
#include <cstdint>
#include <iostream>

using namespace std;

void dfs(int32_t h, int32_t w, array<array<uint32_t, 50>, 50> &arr, int32_t i, int32_t j)
{
  arr[i][j] = 0;

  for (int32_t l = -1; l <= 1; l++) {
    for (int32_t k = -1; k <= 1; k++) {
      if (0 <= i + l && i + l < h && 0 <= j + k && j + k < w) {
        if (arr[i + l][k + j] == 1) {
          dfs(h, w, arr, i + l, k + j);
        }
      }
    }
  }
}

int32_t main()
{
  while (true) {
    uint32_t w, h;
    cin >> w >> h;
    if (w == 0) break;

    array<array<uint32_t, 50>, 50> arr;
    for (uint32_t i = 0; i < h; i++) {
      for (uint32_t j = 0; j < w; j++) {
        cin >> arr[i][j];
      }
    }

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

  return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<list>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for(int i=0;i<n;++i)
#define rrep(i, n) for(int i=n;i>=0;--i)
const int inf=1e9+7;
const ll mod=1e9+7;
const ll big=1e18;
const double PI=2*asin(1);

int main() {
    int W, H;
    int island[55][55];
    int ans;
    vector<int> ansarr;
    queue<pair<int, int> > que;
    pair<int, int> state;
    while(true){
        cin>>W>>H;
        if((W==0) && (H==0)) break;
        for(int i=0;i<H;++i){
            for(int j=0;j<W;++j){
                cin>>island[i][j];
            }
        }
        ans = 0;
        for(int i=0;i<H;++i){
            for(int j=0;j<W;++j){
                if(island[i][j]==1){
                    ans++;
                    que.push(mp(i, j));
                    while(!que.empty()){
                        state = que.front();que.pop();
                        if(island[state.fi][state.se]==0) continue;
                        island[state.fi][state.se] = 0;
                        if((state.fi>0) && (island[state.fi-1][state.se]==1)){
                            que.push(mp(state.fi-1, state.se));
                        }
                        if((state.fi<H-1) && (island[state.fi+1][state.se]==1)){
                            que.push(mp(state.fi+1, state.se));
                        }
                        if((state.se>0) && (island[state.fi][state.se-1]==1)){
                            que.push(mp(state.fi, state.se-1));
                        }
                        if((state.se<W-1) && (island[state.fi][state.se+1]==1)){
                            que.push(mp(state.fi, state.se+1));
                        }
                        if((state.fi>0) && (state.se>0) && (island[state.fi-1][state.se-1]==1)){
                            que.push(mp(state.fi-1, state.se-1));
                        }
                        if((state.fi>0) && (state.se<W-1) && (island[state.fi-1][state.se+1]==1)){
                            que.push(mp(state.fi-1, state.se+1));
                        }
                        if((state.fi<H-1) && (state.se>0) && (island[state.fi+1][state.se-1]==1)){
                            que.push(mp(state.fi+1, state.se-1));
                        }
                        if((state.fi<H-1) && (state.se<W-1) && (island[state.fi+1][state.se+1]==1)){
                            que.push(mp(state.fi+1, state.se+1));
                        }
                    }
                }
            }
        }
        ansarr.push_back(ans);
    }
    for(int i=0;i<ansarr.size();++i){
        cout<<ansarr[i]<<endl;
    }
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int m[55][55];

void fill_(int x,int y,int i){
    if(m[x][y]!=1){
        return;
    }
    m[x][y]=i;
    for(int dx=-1;dx<=1;++dx){
        for(int dy=-1;dy<=1;++dy){
            if(dx||dy){
                fill_(x+dx,y+dy,i);
            }
        }
    }
}

int main(void){
    // Your code here!
    while(1){
        for(int i=0;i<55;++i)fill(m[i],m[i]+55,-1);
        
        
        int w,h;
        cin>>w>>h;
        if(w==0&&h==0)break;
        for(int x=0; x<h;++x){
            for(int y=0;y<w;++y){
                int t;cin>>t;
                m[x+1][y+1]=t;
            }
        }
        int res=0;
        for(int x=1;x<=h;++x){
            for(int y=1;y<=w;++y){
                if(m[x][y]==1){
                    ++res;
                    fill_(x,y,res+1);
                }
            }
        }
        cout<<res<<endl;
    }
}