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 <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <cmath>
using namespace std;
typedef long long i64;
typedef long double ld;
typedef pair<i64,i64> P;
#define rep(i,s,e) for(int i = (s);i <= (e);i++)

int w,h;

int main()
{
    while(cin >> w >> h,w)
    {
        vector<vector<int>> fie(h,vector<int>(w,0));
        rep(i,0,h - 1)rep(j,0,w - 1) cin >> fie[i][j];
        int res = 0;
        rep(i,0,h - 1)rep(j,0,w - 1){
            if(fie[i][j])
            {
                queue<P> que;
                res++;
                que.push({i,j});
                fie[i][j] = 0;
                while(!que.empty())
                {
                    auto p = que.front();
                    que.pop();

                    for(int i = p.first - 1;i <= p.first + 1;i++)
                    {
                        for(int j = p.second - 1;j <= p.second + 1;j++)
                        {
                            if(0 <= i && i < h && 0 <= j && j < w && fie[i][j])
                            {
                                que.push({i,j});
                                fie[i][j] = 0;
                            }
                        }
                    }
                }
            }
        }
        cout << res << endl;
    }
}

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

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

void dfs(int y, int x, vector< vector<int> > &v){
    v[y][x] = 0;

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

    return;
}

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

        int ans = 0;
        for(int i = 1 ; i <= h ; i++){
            for(int j = 1 ; j <= w ; j++){
                if(c[i][j] == 1){
                    ans++;
                    dfs(i, j, c);
                }
            }
        }

        cout << ans << endl;
    }
}

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

int main(){
  int w,h;
  int dx[8] = {1,1,0,-1,-1,-1,0,1};
  int dy[8] = {0,1,1,1,0,-1,-1,-1};
  while(true){
  cin>>w>>h;
  if(!w&&!h)break;
  w+=2,h+=2;
  int island[w][h];
  for(int x=0;x<w;x++){
    island[x][0] = 0;
    island[x][h-1] = 0;
  }
  for(int y=1;y<h-1;y++){
    island[0][y] = 0;
    island[w-1][y] = 0;
    for(int x=1;x<w-1;x++){
      cin>>island[x][y];
    }
  }
  int count = 0;
  queue<int> qu;
  for(int y=1;y<h-1;y++){
    for(int x=1;x<w-1;x++){
      if(island[x][y]){
	qu.push(y*w+x);
	while(!qu.empty()){
	  if(island[qu.front()%w][qu.front()/w]){
	    island[qu.front()%w][qu.front()/w] = 0;
	    for(int i=0;i<8;i++)
	      qu.push(qu.front()+dy[i]*w+dx[i]);
	  }
	  qu.pop();
	}
	count++;
      }
    }
  }
  cout<<count<<endl;
  }
  return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;

char c[102][102];

int mx, my;

void dfs(int x, int y){
    if(x < 0 || y < 0 || x >= mx || y >= my){
        return;
    }
    if(c[x][y] == '0'){
        return;
    }
    c[x][y] = '0';
    dfs(x+1, y);
    dfs(x+1, y+2);
    dfs(x+1, y-2);
    dfs(x-1, y);
    dfs(x-1, y+2);
    dfs(x-1, y-2);
    dfs(x, y+2);
    dfs(x, y-2);
    return;
}

int main(){
    int W, H, cnt = 0;
    scanf("%d %d\n", &W, &H);
    while(W != 0 || H != 0){
        mx = H;
        my = W;
        for(int k = 0; k < H; k++){
            cin.getline(c[k], sizeof(c[k]));
        }
        for(int i = 0; i < H; i++){
            for(int j = 0; j < W; j++){
                if(c[i][j] == '1'){
                    cnt++;
                    dfs(i, j);
                }
            }
        }
        cout << cnt << endl;
        scanf("%d %d\n", &W, &H);
        cnt = 0;
    }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
 
#define rep(i, n) for(int i = 0; i < n; i ++)
#define ALL(T) T.begin(), T.end()
#define mp make_pair
#define pb push_back
#define X first
#define Y second

using namespace std;
 
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;
 
int cnt, w, h;
int m[100][100];
const int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

void dfs(pii);

int main(void){
    for(;cin >> w >> h, w; cout << cnt << endl){
        cnt = 0;
        rep(y, h) rep(x, w) cin >> m[y][x];
        rep(y, h) rep(x, w) if(m[y][x] == 1) cnt ++, dfs(mp(x, y));
    }
 
    return 0;
}

void dfs(pii p){
    if(p.X < 0 || w <= p.X ||
       p.Y < 0 || h <= p.Y) return;
    
    if(m[p.Y][p.X] == 1) m[p.Y][p.X] = 0;
    else return;

    rep(i, 8) dfs(mp(p.X + dx[i], p.Y + dy[i]));
}

C++: #include <bits/stdc++.h>
using namespace std;
bool island[50][50];
int n,m;
int dx[9]={0,1,1,1,0,-1,-1,-1},dy[9]={-1,-1,0,1,1,1,0,-1};
void serch(int y,int x)
{
	island[y][x]=0;
	for(int i=0,ax,ay;i<9;i++)if((ay=dy[i]+y)<m&&(ax=dx[i]+x)<n&&ay>=0&&ax>=0&&island[ay][ax])serch(ay,ax);
}
int main() {
	while(cin>>n>>m,n,m){
		int ans=0;
		for(int i=0;i<m;i++)for(int j=0;j<n;j++)cin>>island[i][j];
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				if(island[i][j]){
					ans++;
					serch(i,j);
				}
			}
		}
		cout<<ans<<endl;
	}
}

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

using namespace std;

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

void dfs(int x,int y){
  if(grid[y][x]==0)return;
  grid[y][x]=0;
  for(int i=-1;i<2;i++){
    for(int j=-1;j<2;j++){
      int ny=y+i,nx=x+j;
      if(0<=nx && nx<w && 0<=ny && ny<h)dfs(nx,ny);
    }
  }
}

int main(void){

  while(cin >> w >> h,w|h){
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)cin >> grid[i][j];
    
    int ans=0;
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	if(grid[i][j]==1)ans++,dfs(j,i);
    cout << ans << endl;
  }

  return 0;
}

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

using namespace std;

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

void dfs(int x,int y){
  g[y][x]=0;
  for(int dy=-1;dy<=1;dy++){
    for(int dx=-1;dx<=1;dx++){
      int nx=x+dx,ny=y+dy;
      if(0<=nx && nx<w && 0<=ny && ny<h && g[ny][nx]==1)
	dfs(nx,ny);
    }
  }
}

int main(void){

  while(cin >> w >> h, w|h){
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)cin >> g[i][j];
    
    int cnt=0;
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	if(g[i][j]==1)cnt++,dfs(j,i);

    cout << cnt << endl;
  }
  
  return 0;
}