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;
typedef long long ll;
typedef pair<ll,ll> pll;

ll w,h;
vector<vector<ll>> ans(50,vector<ll> (50,-1));
vector<vector<ll>> maze(50,vector<ll> (50));

vector<ll> idx={1,0,-1,0,1,1,-1,-1};
vector<ll> idy={0,1,0,-1,1,-1,1,-1};

bool dfs(ll th,ll tw,ll n) {
  if(ans[th][tw]<0&&maze[th][tw]==1) {
    ans[th][tw]=n;
    for(ll i=0;i<8;i++) {
      ll nh=th+idy[i];
      ll nw=tw+idx[i];
      if(nh>-1&&nh<h&&nw>-1&&nw<w) {
        dfs(nh,nw,n);
      }
    }
    return true;
  }
  else {
    return false;
  }
}

int main() {
  while(1) {
    cin >> w >> h;
    if(w==0&&h==0) {
      return 0;
    }
    for(ll i=0;i<h;i++) {
      for(ll j=0;j<w;j++) {
        cin >> maze[i][j];
        ans[i][j]=-1;
      }
    }
    ll count=0;
    for(ll i=0;i<h;i++) {
      for(ll j=0;j<w;j++) {
        if(dfs(i,j,count)) {
          count++;
        }
      }
    }
    cout << count << endl;
  }
}

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

int w,h;
int mp[50][50];

void rec(int y,int x){
	if(mp[y][x]==0)return;
	mp[y][x]=0;
	if(x+1<w)rec(y,x+1);
	if(y+1<h)rec(y+1,x);
	if(x-1>=0)rec(y,x-1);
	if(y-1>=0)rec(y-1,x);
	if(x+1<w&&y-1>=0)rec(y-1,x+1);
	if(y+1<h &&y+1<h)rec(y+1,x+1);
	if(x-1>=0&&y+1<h)rec(y+1,x-1);
	if(y-1>=0&&x-1>=0)rec(y-1,x-1);
}

int main(){
	int x,y,ret;
	while(cin >> w >> h && w && h){
		ret = 0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin >> mp[i][j];
			}
		}
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				if(mp[i][j]){
					rec(i,j);
					ret++;
				}
			}
		}
		cout << ret << endl;
	}
	return 0;
}

C++: #include <iostream>

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

using namespace std;

int area[50][50], cnt = 0;
int w, h;

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

  for(int dx = -1 ; dx <= 1 ; dx++){
    for(int dy = -1 ; dy <= 1 ; dy++){
      int nx = x + dx, ny = y + dy;

      if(0 <= nx && nx < w && 0 <= ny && ny < h && area[nx][ny] == 1){
	dfs(nx, ny);
      }
    }
  }
}

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

    rep(i, h){
      rep(j, w){
	cin >> area[i][j];
      }
    }

    rep(i, w){
      rep(j, h){
	if(area[i][j] == 1){
	  dfs(i, j);
	  cnt++;
	}
      }
    }
    
    cout << cnt << '\n';
    cnt = 0;
  }
}

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

int main(){
  bool map[52][52];
  int w,h;

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

    int land=0;

    for(int i=1;i<=h;i++){
      for(int j=1;j<=w;j++){
        int c;
        cin >> c;
        if(c==1) map[i][j]=true;
        land++;
      }
    }

    int visited=0,cnt=0;
    queue<int> cur;
    int d[8]={-w-3,-w-2,-w-1,-1,1,w+1,w+2,w+3}

    while(visited<land){
      cnt++;
      for(int i=0;i<(w+2)*(h+2);i++){
        if(map[i/(w+2)][i%(w+2)]){
          map[i/(w+2)][i%(w+2)]=false;
          visited++;
          cur.push(i);
          break;
        }
      }
      while(!cur.empty()){
        for(int i=0;i<8;i++){
          if(map[(cur.front()+d[i])/(w+2)][(cur.front()+d[i])%(w+2)]){
            map[(cur.front()+d[i])/(w+2)][(cur.front()+d[i])%(w+2)]=false;
            visited++;
            cur.push(cur.front()+d[i]);
          }
        }
        cur.pop();
      }
    }

    cout << cnt << endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define All(v) (v).begin(),(v).end()
int dy[8] = {-1,0,1,0,-1,1,1,-1};
int dx[8] = {0,1,0,-1,1,1,-1,-1};

int h,w,cnt=0;
int mas[55][55];
void dfs(int y,int x){
    mas[y][x]=0;
    for(int i=0;i<8;i++){
        int ny=y+dy[i];
        int nx=x+dx[i];
        if(ny<0||nx<0||ny>=h||nx>=w)continue;
        if(mas[ny][nx]==1){
            dfs(ny,nx);
        }
    }
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(true){
        cin>>w>>h;
        if(h==0&&w==0)break;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin>>mas[i][j];
            }
        }
        cnt=0;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(mas[i][j]==1){
                    cnt++;
                    dfs(i,j);
                }
            }
        }
        cout<<cnt<<endl;
    }
}

C++: #include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repr(i,a,n) for(int i=a;i>=n;i--)
#define INF 999999999
#define pb(a) push_back(a)

using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef vector<pii> VP;

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

int w,h;

int dfs(int i, int j, int c[][51]) {
    if(c[i][j] == 1) {
        c[i][j] = 0;
        rep(di,i-1,i+2) {
            rep(dj,j-1,j+2) {
                if(di != i || dj != j) {
                    if(di<0 || di>=h || dj<0 || dj>=w) continue;
                    if(c[di][dj] == 1) dfs(di,dj,c);
                }
            }
        }
        return 1;
    }
    return 0;
}

int main() {
    int c[51][51];

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

        rep(i,0,h) {
            rep(j,0,w) {
                cin >> c[i][j];
            }
        }

        int ret = 0;
        rep(i,0,h) {
            rep(j,0,w) {
                ret += dfs(i,j,c);
            }
        }

        cout << ret << endl;
    }
    return 0;
}