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<iomanip>
#include<cstdlib>
#include<stdio.h>
#include<time.h>
#include<math.h>
#include<algorithm>
#include<string>
#include<vector>
#include<list>
#include<queue>
#include<stack>
#include<deque>
#include<set>
#include<map>
#define REP(i, N) for(ll i = 0; i < N; ++i)
#define FOR(i, a, b) for(ll i = a; i < b; ++i)
#define ALL(a) (a).begin(),(a).end()
#define pb push_back
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int qx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int qy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

vector<vector<int> > m;
int w, h;

void cal(ll x, ll y) {
	REP(i, 8) {
		ll nx = x + qx[i];
		ll ny = y + qy[i];
		if(nx >= 0 && nx < h && ny >= 0 && ny < w && m[nx][ny] == 1) {
			m[nx][ny] = 0;
			cal(nx, ny);
		}
	}
}

int main(void) {
	while(true) {
		cin>>w>>h;
		if(w == 0 && h == 0) break;
		m.resize(h);
		REP(i, h) {
			m[i].resize(w);
		}
		REP(i, h) {
			REP(j, w) {
				cin>>m[i][j];
			}
		}
		ll ans = 0;
		REP(i, h) {
			REP(j, w) {
				if(m[i][j] == 1) {
					++ans;
					cal(i, j);
				}
			}
		}
		cout<<ans<<endl;
	}
}

C++: #include<iostream>

using namespace std;

const int maxWH = 52;
int H, W;
int C[maxWH][maxWH] = {0};

void dfs(int hh, int ww) {
  int dh[] = {-1, 0, 1, 0, -1, -1, 1, 1};
  int dw[] = {0, -1, 0, 1, -1, 1, -1, 1};
  //cout<<hh<<" "<<ww<<endl;
  if(0<=hh && 0<=ww && hh<H && ww<W) {
    if(C[hh][ww] == 1) {
      C[hh][ww] = 0; // false
      /*
      cout<<"now "<<hh<<" "<<ww<<endl;
      for(int i=0; i<8; i++) {
        cout<<hh+dh[i]<<" "<<ww+dw[i]<<endl;
      }
      */
      for(int i=0; i<8; i++) {
        dfs(hh+dh[i], ww+dw[i]);
      }
    }
  }
}

int main() {
  while(cin>>W>>H && !(W==0 && H==0)) {
    for(int i=0; i<H; i++) {
      for(int j=0; j<W; j++) {
	cin>>C[i][j];
      }
    }

    /*
    cout<<endl;
    for(int i=0; i<H; i++) {
      for(int j=0; j<W; j++) {
	cout<<C[i][j]<<" ";
      }
      cout<<endl;
    }
    */
    
    int ans = 0;
    for(int i=0; i<H; i++) {
      for(int j=0; j<W; j++) {
	if(C[i][j] == 1) {
	  ans++;
	  dfs(i,j);
	}
      }
    }
    cout<<ans<<endl;
  }
}

C++: #include <bits/stdc++.h>
#ifdef _DEBUG
#include "debug.hpp"
#else
#define debug(...)
#endif
#define REP(i, m, n) for(int (i) = (m); (i) < (n); ++i)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) for(int (i) = (n)-1; (i) >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define FI first
#define SE second
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9+7;
const ll LINF = 1LL<<60;
template <class T, class U>
inline bool chmax(T &a, const U &b) {
  if(a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U>
inline bool chmin(T &a, const U &b) {
  if(a > b) {
    a = b;
    return true;
  }
  return false;
}

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

int h, w;
vector<vector<bool>> vis;
vector<vector<int>> c;

void dfs(int y, int x) {
  if(vis[y][x]) return;
  vis[y][x] = true;
  rep(i,8) {
    int nx = x+dx[i];
    int ny = y+dy[i];
    if(nx < 0 || w <= nx || ny < 0 || h <= ny || !c[ny][nx]) continue;
    dfs(ny, nx);
  }
}

int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  while(true) {
    cin >> w >> h;
    if(w == 0 && h == 0) break;
    c.assign(h, vector<int>(w));
    rep(i,h) rep(j,w) cin >> c[i][j];
    vis.assign(h, vector<bool>(w, false));
    int ans = 0;
    rep(i,h) rep(j,w) if(c[i][j] && !vis[i][j]) {
      ans++;
      dfs(i, j);
    }
    cout << ans << '\n';
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <string>
#include <queue>
#include <map>
#include <stack>
using namespace std;
 
int dx[8]={1,-1,0,0,1,1,-1,-1};
int dy[8]={0,0,1,-1,-1,1,-1,1};
 
 
int main() {
int yoko,tate,px,py;
while(cin>>yoko>>tate &&yoko){
 
    int m[55][55]={0};
 
string st;
//cin>>yoko>>tate;
for(int i=1;i<tate+1;i++){
for(int j=1;j<yoko+1;j++){
cin>>m[i][j];
}
}
int ans=0;

for(int i=0;i<tate+1;i++){
for(int j=0;j<yoko+1;j++){
if (m[i][j]==0) continue;  

stack<pair<int,int> > s;
s.push(make_pair(i,j));
m[i][j]=0;
//cout<<"pxpy "<<px<<" "<<py<<endl;
while(s.empty()==false){
px=s.top().first;
py=s.top().second;
s.pop();
 
for(int i=0;i<8;i++){
//cout<<px+dx[i]<<" "<<py+dy[i]<<endl;
    if(0<px+dx[i] && px+dx[i]<tate+1 && py+dy[i]>0 && py+dy[i]<yoko+1){
     
        if(m[px+dx[i]][py+dy[i]]==1){
            m[px+dx[i]][py+dy[i]]=0;
            s.push(make_pair(px+dx[i],py+dy[i]));
            
//cout<<px+dx[i]<<" "<<py+dy[i]<<" "<<ans<<endl;
        }
 
    }}
}
 ans++;
}}
cout<<ans<<endl;
 
}
 
 
 
    return 0;
}

C++: #include <bits/stdc++.h>
#include<iostream>
#include <string>
#include <cstring>
using namespace std;
const int dy[8] = {1, 1, 1, 0, -1, -1, -1, 0};
const int dx[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

int H, W;
vector<vector<bool>>seen(50, vector<bool>(50));
vector<vector<int>>field(50, vector<int>(50, 0));

void dfs(int h, int w) {
    if (seen[h][w]) {
        return;
    }

    seen[h][w] = true;
    for (int dir=0; dir < 8; dir++) {
        int nh = h + dy[dir];
        int nw = w + dx[dir];
        if (nh < 0 || nw < 0 || nh >= H || nw >= W) {
            continue;
        }

        if (field[nh][nw] == 0) {
            continue;
        }

        dfs(nh, nw);
    }
}

int main() {
    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++)
        {
            seen.at(i).at(j) = 0;
            cin >> field.at(i).at(j);
        }
    }

    int island_count = 0;
    for (int i = 0; i < H; i++)
    {
        for (int j = 0; j < W; j++)
        {
            if (seen[i][j]) {
                continue;
            }

            if (field[i][j] == 0) {
                continue;
            }

            dfs(i, j);
            island_count += 1;
        }
    }

    cout << island_count << endl;
    }
    return 0;
}

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

int field[55][55];
int w, h;
int dx[] = {0, 1, 0, -1, 1, -1, -1, 1};
int dy[] = {1, 0, -1, 0, 1, 1, -1, -1};

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

int main(){

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