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<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<string>
#include<cmath>
#include<cstdint>
#include<cassert>
using namespace std;

typedef int64_t i64;typedef int32_t i32;
typedef int16_t i16;typedef int8_t i8;
typedef uint64_t ui64;typedef uint32_t ui32;
typedef uint16_t ui16;typedef uint8_t ui8;

typedef i32 int__;
#define rep(i,j) for(int__ i=0;i<j;i++)
#define repeat(i,j,k) for(int__ i=(j);i<(k);i++)
#define repeat_eq(i,j,k) for(int__ i=(j);i<=(k);i++)
#define all(v) begin(v),end(v)

const i32 INF=1<<30;//=10E10

vector< vector<i32> > M;
i32 w,h;

void dfs(int y,int x,int id){
  int dx[]={1,0,-1,0,1,-1,1,-1};
  int dy[]={0,1,0,-1,1,1,-1,-1};
  M[y][x]=id;
  rep(i,8){
      int nx=x+dx[i];
      int ny=y+dy[i];
      if(0<=nx && nx<w and 0<=ny && ny<h){
	if(M[ny][nx]==1)dfs(ny,nx,id);
      }
  }
}

int main()
{
  while(true){
  
    cin>>w>>h;
    if(!w and !h)break;
    M.clear();
    M.resize(h,vector<i32>(w));
    rep(i,h){
      rep(j,w){
	cin>>M[i][j];
      }
    }
    
    int ans=0;
    rep(i,h)rep(j,w){
      if(M[i][j]==1){
	dfs(i,j,ans++ +2);
      }
    }
    
    cout<<ans<<endl;
  }
  
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) FOR(i,0,n)
#define repr(i,n) for(int i=(n)-1;0<=i;--i)
#define each(e,v) for(auto&& e:(v))
#define all(v) begin(v),end(v)
#define dump(x) cerr<<#x<<": "<<(x)<<endl
#define INF 100000000
#define IINF 9999999999999;
using vint = vector<int>;
using ll = long long;
using vll = vector<ll>;
template <class T> void chmin(T& a, const T& b) { a = min(a, b); }
template <class T> void chmax(T& a, const T& b) { a = max(a, b); }

bool earth[51][51];

void erase_island(int h, int w) {
    earth[h][w] = false;
    //cout << "# " << h << " " << w << endl;
    if(earth[h - 1][w - 1]) erase_island(h - 1, w - 1);
    if(earth[h - 1][w]) erase_island(h - 1, w);
    if(earth[h - 1][w + 1]) erase_island(h - 1, w + 1);
    if(earth[h][w + 1]) erase_island(h, w + 1);
    if(earth[h + 1][w + 1]) erase_island(h + 1, w + 1);
    if(earth[h + 1][w]) erase_island(h + 1, w);
    if(earth[h + 1][w - 1]) erase_island(h + 1, w - 1);
    if(earth[h][w - 1]) erase_island(h, w - 1);
}

int main() {
    //ofstream outputfile("output.txt");
    while(true) {
        int w = 0, h = 0;
        cin >> w >> h;
        if(w == 0 && h == 0) break;
        int temp;
        rep(i, h) {
            rep(j, w) {
                cin >> temp;
                if(temp == 0) earth[i][j] = false;
                else earth[i][j] = true;
            }
        }

        int ans = 0;
        rep(i, h) {
            rep(j, w) {
                if(earth[i][j]) {
                    erase_island(i, j);
                    ++ans;
                }
            }
        }

        cout << ans << endl;
    }

    return 0;
}

C++: #define  _USE_MATH_DEFINES
#define  _USE_MATH_DEFINES

#define  rep(i, n) for(int i = 0; i < n; i++)
#define  repx(i, a, n) for(int i = a; i < n; i++)
#define  loop while(1)
#define  lli long long int
#define  aryout(ary) rep(i, ary.size()) cout << ary[i] << (i < ary.size() - 1 ? " " : "\n");
 
#include <iostream>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <utility>
#include <set>

using namespace std;

void dfs (vector<vector<int>> &umi, int h, int w, int i, int j) {
  umi[i][j] = 2;
  for (int di = -1; di <= 1; di++)
    for (int dj = -1; dj <= 1; dj++)
      if (umi[i + di][j + dj] == 1)
        dfs (umi, h, w, i + di, j + dj);
}

int bfs (vector<vector<int>> &umi, int h, int w) {
  int cnt = 0;
  stack<vector<int>> gnd;
  vector<int> tmp0(2, 0);
  vector<int> tmp1(2, 0);

  repx (i, 1, h + 1) repx (j, 1, w + 1) {
    if (umi[i][j] == 1) {
      umi[i][j] = 2;
      cnt += 1;
      tmp0[0] = i; tmp0[1] = j;
      gnd.push(tmp0);

      while (gnd.size() > 0) {
        tmp0[0] = gnd.top()[0];
        tmp0[1] = gnd.top()[1];
        gnd.pop();

        //cout << "cnt: " << cnt << endl;
        //repx (i, 0, h + 2) repx (j, 0, w + 2)
          //cout << umi[i][j] << (j < w + 1 ? " " : "\n");

        umi[tmp0[0]][tmp0[1]] = 2;
        for (int di = -1; di <= 1; di++) {
          for (int dj = -1; dj <= 1; dj++) {
            tmp1[0] = tmp0[0] + di; tmp1[1] = tmp0[1] + dj;
            if (umi[tmp1[0]][tmp1[1]] == 1) {
              gnd.push(tmp1);
              umi[tmp1[0]][tmp1[1]] = 2;
              //cout << tmp1[0] << " " << tmp1[1] << " (" << umi[tmp1[0]][tmp1[1]] << ") " <<  "has pushed." << endl;
            }
          }
        }
      }
    }
  }
  return cnt;
}


int main() {

  int w, h;
  
  loop {
    cin >> w >> h;
    if (w + h == 0) break;

    vector<vector<int>> umi(h + 2, vector<int>(w + 2, 0));
    rep (i, h) rep (j, w) cin >> umi[i + 1][j + 1];

    /*
    int cnt_by_DFS = 0;
    repx (i, 1, h + 1) repx (j, 1, w + 1) {
      if (umi[i][j] == 1) {
        cnt_by_DFS += 1;
        dfs (umi, h, w, i, j);
      }
    }
    cout << cnt_by_DFS << endl;
    */
    cout << bfs (umi, h, w) << endl;
  }

  return 0;
}