Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

C++: #include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, x) \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vector<ll>>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;

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

const ll INF = 1LL << 50;

const ll mod = 1e9 + 7;

int main() {
  ll w, h;
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0) break;
    vvll mp(h, vll(w, -1));
    vvll mk(h, vll(w, -1));
    rep(i, h) rep(j, w) cin >> mp[i][j];

    ll ans = 0;
    queue<P> que;
    bool pushcond;
    rep(i, h) {
      rep(j, w) {
        pushcond = mk[i][j] == -1 && mp[i][j] == 1;
        if (pushcond) {
          ans++;
          mk[i][j] = ans;
          que.emplace(i, j);
        }
        while (!que.empty()) {
          ll x, y;
          tie(y, x) = que.front();
          que.pop();
          mk[i][j] = ans;
          rep(k, 9) {
            ll nx = x + dx[k];
            ll ny = y + dy[k];
            bool insidecond = 0 <= nx && nx < w && 0 <= ny && ny < h;
            if (!insidecond) continue;
            pushcond = mk[ny][nx] == -1 && mp[ny][nx] == 1;
            if (pushcond) {
              mk[ny][nx] = ans;
              que.emplace(ny, nx);
            }
          }  // k
        }    // while
      }      // j
    }        // i

    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <tuple>
#include <sstream>
#include <set>

struct Node
{
  int data;
  Node* parent;
  int rank;
  Node() : data(-1), parent(this), rank(0){}
  Node& operator=(int data){ this->data = data; return *this; }
  Node* Find(){
    if (this == this->parent){
      return this;
    } else {
      auto root = this->parent->Find();
      this->parent = root;
      return root;
    }
  }
  static void Union(Node& lhs, Node& rhs){
    Node* lhs_root = lhs.Find();
    Node* rhs_root = rhs.Find();
    if (lhs_root->rank > rhs_root->rank){
      rhs_root->parent = lhs_root;
    } else if (rhs_root->rank > lhs_root->rank){
      lhs_root->parent = rhs_root;
    } else if (rhs_root != lhs_root){
      lhs_root->parent = rhs_root;
      rhs_root->rank += 1;
    }
    return;
  }
};

bool is_not_space(int c){ return c != ' '; }
std::string& ltrim(std::string& str){
  str.erase(str.begin(), std::find_if(str.begin(), str.end(), is_not_space));
  return str;
}
std::string& rtrim(std::string& str){
  str.erase(std::find_if(str.rbegin(), str.rend(), is_not_space).base(), str.end());
  return str;
}
std::string trim(std::string& str){ return ltrim(rtrim(str)); }
std::vector<std::pair<int, int>> const delta = {{-1,1}, {0,1}, {1,1}, {1,0}};

int main(void)
{
  std::string buff;
  while(true){
    std::getline(std::cin, buff);
    int w, h;
    {
      std::stringstream ss(buff);
      ss >> w;
      ss >> h;
    }
    if(w == 0 && h == 0){ break; }
    std::vector<Node> V((w+2)*(h+2));
    auto to_index = [=](int i, int j) -> size_t { return i*(w+2) + j; };
    std::vector<std::vector<int>> C(h+2, std::vector<int>(w+2, 0));
    for(int i=1; i<=h; ++i){
      std::getline(std::cin, buff);
      std::stringstream ss(buff);
      for(int j=1; j<=w; ++j){
        ss >> C[i][j];
        if(C[i][j] == 1){ V[to_index(i,j)].data = to_index(i,j); }
      }
    }
    for(int i=1; i<=h; ++i){
      for(int j=1; j<=w; ++j){
        if(C[i][j] == 0){ continue; }
        for(auto& d: delta){
          int di = d.first, dj = d.second;
          if(C[i+di][j+dj] == 1){ Node::Union(V[to_index(i,j)], V[to_index(i+di, j+dj)]); }
        }
      }
    }
    std::set<int> cluster;
    for(int i=1; i<=h; ++i){
      for(int j=1; j<=w; ++j){
        cluster.insert(V[to_index(i,j)].Find()->data);
      }
    }
    cluster.erase(-1);
    std::cout << cluster.size() << std::endl;

  }

  return 0;
}

C++: #include<iostream>
using namespace std;
void dfs(int,int);
int box[50][50];
int main(){
  int w,h,i,I,a;
  while(1){
    a=0;
    cin>>w>>h;
    if(w==0&&h==0) break;
    for(i=0;i<=h+1;i++){
      for(I=0;I<=w+1;I++){
	box[i][I]==0;
      }
    }
    for(i=1;i<=h;i++){
      for(I=1;I<=w;I++){
	cin>>box[i][I];
      }
    }
    for(i=1;i<=h;i++){
      for(I=1;I<=w;I++){
	if(box[i][I]==1){
	  dfs(i,I);
	  a++;
	}
      }
    }
    cout<<a<<endl;
  }
  return 0;
}
  void dfs(int i,int I){
  if(box[i][I]==0) return;
  box[i][I]=0;
  dfs(i,I+1);
  dfs(i+1,I+1);
  dfs(i+1,I);
  dfs(i+1,I-1);
  dfs(i,I-1);
  dfs(i-1,I-1);
  dfs(i-1,I);
  dfs(i-1,I+1);
  }

C++: #include <bits/stdc++.h>
#define REP(i,n,N) for(int i=n;i<N;i++)
#define p(S) cout<<S<<endl
using namespace std;

int chizu[51][51];

void labeling(int y,int x,int w,int h){
	chizu[y][x]=0;
	REP(dy,-1,2){
		REP(dx,-1,2){
			if(0 <= y+dy && y+dy < h && 0 <= x+dx && x+dx < w){
				if(chizu[y+dy][x+dx]){
					labeling(y+dy,x+dx,w,h);
				}
			}
		}
	}
}

int main(){
	int w,h;
	while(cin>>w>>h&&w&&h){

		int count=0;
		REP(i,0,h){
			REP(j,0,w){
				cin>>chizu[i][j];
			}
		}
		REP(i,0,h){
			REP(j,0,w){
				if(chizu[i][j]){
					count++;
					labeling(i,j,w,h);
				}
			}
		}

		p(count);
	}
	return 0;
}