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;

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 < 2 * W; j++){
                if(c[i][j] == '1'){
                    cnt++;
                    dfs(i, j);
                }
            }
        }
        cout << cnt << endl;
        scanf("%d %d\n", &W, &H);
        cnt = 0;
    }
}

C++: #include <bits/stdc++.h>
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template<class T> inline bool chmax(T &a, T b) { if (a<b) {a=b; return 1;} return 0;}
template<class T> inline bool chmin(T &a, T b) { if (b<a) {a=b; return 1;} return 0;}

const int dw[] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dh[] = {0, 1, 1, 1, 0, -1, -1, -1};

vector<vector<int>> to;
vector<vector<int>> seen;

int W, H;

void dfs(int a, int b) {
    seen[a][b] = 1;
    rep(i, 8) {
        int nh = a + dh[i], nw = b + dw[i];
        if (nh < 0 || H <= nh || nw < 0 || W <= nw) continue;
        if (seen[nh][nw]) continue;
        if (to[nh][nw] == 0) continue;
        dfs(nh, nw);
    }
}

void solve(int w, int h) {
    to = vector<vector<int>>(h, vector<int>(w));
    seen = vector<vector<int>>(h, vector<int>(w, 0));
    rep(i, h) rep(j, w) cin >> to[i][j];
    int ans = 0;
    rep(i, h) rep(j, w) if ((!seen[i][j]) && (to[i][j] == 1)) {
        dfs(i, j);
        ans++;
    }
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    while (true) {
        cin >> W >> H;
        if (W == 0 && H == 0) break;
        solve(W, H);
    }
    return 0;
}

C++: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>

using namespace std;

// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

// container util
#define all(x) (x).begin(), (x).end()

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                         \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
       << " " << __FILE__ << endl;

// typedef
typedef long long lint;
typedef unsigned long long ull;
typedef complex<long double> Complex;
typedef pair<int, int> P;
typedef tuple<int, int, int> TP;
typedef vector<int> vec;
typedef vector<vec> mat;

// constant
const int MOD = (int)1e9 + 7;
const int INF = (int)1e18;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int ddx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[] = {1, 1, 0, -1, -1, -1, 0, 1};

// conversion
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

//
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
//

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int w, h;

  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0) break;
    int mp[55][55];
    bool used[55][55];

    queue<pair<int, int>> que;
    rep(y, h) {
      rep(x, w) {
        cin >> mp[y][x];
        if (mp[y][x] == 0)
          used[y][x] = false;
        else
          used[y][x] = true;
      }
    }

    int ans = 0;

    rep(y, h) {
      rep(x, w) {
        if (used[y][x]) {
          ans++;
          que.push(make_pair(x, y));
          while (!que.empty()) {
            pair<int, int> p = que.front();
            que.pop();

            rep(i, 8) {
              int nx = p.first + ddx[i];
              int ny = p.second + ddy[i];
              if (0 <= nx && nx < w && 0 <= ny && ny < h && used[ny][nx]) {
                used[ny][nx] = false;
                que.push(make_pair(nx, ny));
              }
            }
          }
        }
      }
    }

    cout << ans << "\n";
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;


int n,m;
int dy[8]={0,1,-1,0,1,-1,1,-1},dx[8]={1,0,0,-1,1,1,-1,-1};
int t[100][100];
bool u[100][100]={};
stack<pair<int,int>>S;
void DFS(int a,int b)
{
	S.push(make_pair(a,b));
	u[a][b]=true;
	while(!S.empty())
	{	
		bool flag=true;
		int h=S.top().first,w=S.top().second;
		for(int i=0;i<8;i++)
		{
			int p=h+dy[i],q=w+dx[i];
			if(t[p][q]==1&&!u[p][q])
			{
				S.push(make_pair(p,q));
				u[p][q]=true;
				flag=false;
			}
		}
		if(flag)S.pop();
	}
}

int main()
{	
	while(cin>>n>>m)
	{
		int ans=0;
		if(n==0&&m==0)break;
		for(int i=0;i<=m+1;i++)
		{
			for(int j=0;j<=n+1;j++)
			{
				if(i==0||i==m+1||j==0||j==n+1)t[i][j]=0;
				else cin>>t[i][j];
			}
		}
		for(int i=1;i<=m;i++)
		{
			for(int j=1;j<=n;j++)
			{
				if(u[i][j]||t[i][j]==0)continue;
				ans++;
				DFS(i,j);
			}
		}
		cout<<ans<<endl;
		for(int i=0;i<100;i++)
		{
			for(int j=0;j<100;j++)u[i][j]=false;
		}
	}
	return 0;
}

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

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

void dfs(int x,int y){
  filed[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<h&&0<=ny&&ny<w&&filed[nx][ny]==1)dfs(nx,ny);
    }
  }
}

void solve(){
  int res=0;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(filed[i][j]==1){
	dfs(i,j);
	res++;
      }
    }
  }
  cout<<res<<endl;
}

int main(){
  
while(1){
    cin>>w>>h;
    if(w+h==0)break;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin>>filed[i][j];
      }
    }
    solve();
  }
  return 0;
}