Go: package main

import (
	"fmt"
)

func main() {
	var h, w int
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			return
		}
		matrix := make([][]int, h)
		Sean := make([][]bool, h)
		for i := 0; i < h; i++ {
			matrix[i] = make([]int, w)
			Sean[i] = make([]bool, w)
			for j := 0; j < w; j++ {
				fmt.Scan(&matrix[i][j])
			}
		}
		var count int
		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				if matrix[i][j] == 1 && Sean[i][j] == false {
					search(i, j, h, w, matrix, Sean)
					count++
				}
			}
		}
		fmt.Println(count)
	}
}
func search(x int, y int, h int, w int, matrix [][]int, Sean [][]bool) {
	if x < 0 || y < 0 || x >= h || y >= w || matrix[x][y] == 0 {
		return
	}
	if Sean[x][y] == true {
		return
	}

	Sean[x][y] = true

	search(x+1, y, h, w, matrix, Sean)
	search(x-1, y, h, w, matrix, Sean)
	search(x, y+1, h, w, matrix, Sean)
	search(x, y-1, h, w, matrix, Sean)
	search(x+1, y-1, h, w, matrix, Sean)
	search(x-1, y+1, h, w, matrix, Sean)
	search(x+1, y+1, h, w, matrix, Sean)
	search(x-1, y-1, h, w, matrix, Sean)
}

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;
}