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 <iostream>
using namespace std;

#define REP(i, n) for(int i = 0; i < n; i++)

const int MAX_N = 50;
int w, h; // wは横の個数 hは縦の個数
int c[MAX_N][MAX_N]; //マップ
int dx = 1, dy = 1; //移動単位
int island; //記憶

void sch(int x, int y) { //ひとつ前のx座標とy座標

	c[x][y] = 0;

	if ((x + dx < w) && (c[x + dx][y] == 1)) {
		sch(x + dx, y);
		return;
	}
	if (((x - dx >= 0) && (c[x - dx][y] == 1))) {
		sch(x - dx, y);
		return;
	}
	if ((y + dy < h) && (c[x][y + dy] == 1)) {
		sch(x, y + dy);
		return;
	}
	if ((y - dy >= 0) && (c[x][y - dy] == 1)) { 
		sch(y, y - dy); 
		return;
	}
	if (x + dx < w && y + dy < h && c[x + dx][y + dy] == 1) {
		sch(x + dx, y + dy); 
		return;
	}
	if (x - dx >= 0 && y + dy < h && c[x - dx][y + dy] == 1) {
		sch(x - dx, y + dy);
		return;
	}
	if (x + dx < w && y - dy >= 0 && c[x + dx][y - dy] == 1) {
		sch(x + dx, y - dy);
		return;
	}
	if (x - dx >= 0 && y - dy >= 0 && c[x - dx][y - dy] == 1) {
		sch(x - dx, y - dy);
		return;
	}

	island++;
	return;
}

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

	while (1) {
		island = 0;
		cin >> w >> h;
		if (w == 0 && h == 0) break;

		REP(i, w) {
			REP(j, h) cin >> c[i][j];
		}

		REP(i, w) {
			REP(j, h) if (c[i][j] == 1) sch(i, j);
		}
		cout << island << "\n";
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, a, b) for (int i = (a); i < ((int)b); ++i)
#define rrep(i, a, b) for (int i = (int)(b) - 1; i >= ((int)a); --i)
#define sz(c) ((int)c.size())
#define all(c) c.begin(),c.end()

using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

void dump_impl(string str) {}
template <class H, class... T>
void dump_impl(string s, const H &head, const T&... tail) {
    int par = 0;
    rep(i, 0, sz(s)) {
        char ch = s[i];
        if (ch == ',' && par == 0) {
            cerr << " = " << head << ", ";
            dump_impl(s.substr(i + 1), tail...);
            return;
        } else {
            cerr << ch;
            if (ch == '(') par++;
            if (ch == ')') par--;
        }
    }
}
#define dump(...) do { cerr << "\x1b[33;1m"; dump_impl(#__VA_ARGS__ ",", __VA_ARGS__); cerr << "\x1b[0m" << endl; } while (0)

signed main() {
    int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
    int dy[] = {-1, 0, 1, 0, -1, 1, 1, -1};
    while (1) {
        int W, H;
        cin >> W >> H;
        if (W == 0 && H == 0) break;
        vvi A(H, vi(W));
        rep(i, 0, H) {
            rep(k, 0, W) {
                cin >> A[i][k];
            }
        }
        vector<vector<bool>> vis(H, vector<bool>(W));
        int ans = 0;
        rep(y, 0, H) {
            rep(x, 0, W) {
                if (vis[y][x]) continue;
                if (A[y][x] == 0) continue;
                queue<pii> q;
                q.push({y, x});
                vis[y][x] = true;
                while (sz(q)) {
                    int py, px;
                    tie(py, px) = q.front(); q.pop();
                    rep(dir, 0, 8) {
                        int yy = py + dy[dir];
                        int xx = px + dx[dir];
                        if (yy < 0 || xx < 0 || yy >= H || xx >= W) continue;
                        if (vis[yy][xx]) continue;
                        if (A[yy][xx] == 0) continue;
                        q.push({yy, xx});
                        vis[yy][xx] = true;
                    }
                }
                ans++;
            }
        }
        cout << ans << endl;
    }
}

C++: #include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <functional>
#include <vector>
#include <queue>
#include <string>
#include <complex>

#define REP(i,n) for (int i = 0; i < (n); i++)
#define FOR(i,s,n) for (int i = (s); i < (n); i++)
#define ALLOF(c) c.begin(), c.end()
#define RANGEOF(c,i,n) c.begin() + (i), c.begin() + (i) + (n)
#define EPS 1e-9
#define INF 1000000000
using namespace std;

template <class T> T read() { T t; cin >> t; return t; }
template <class T> vector<T> read(int n) { vector<T> v; REP(i,n) { v.push_back(read<T>()); } return v; }
template <class T> vector<vector<T>> read(int n, int m)
{
	vector<vector<T>> v;
	REP(i,n) { v.push_back(read<T>(m)); }
	return v;
}

template <class T> void write(const T &t) { cout << t << endl; }
template <class T> void write(const T &t, const T &t2) { cout << t << ' ' << t2 << endl; }
template <class T> void write(const vector<T> &v)
{
	ostringstream ss; 
	for (auto x : v) ss << x << ' ';
	auto s = ss.str();
	cout << s.substr(0, s.length() - 1) << endl;
}

struct _Reader { template <class T> _Reader operator ,(T &rhs) { cin >> rhs; return *this; } };
#define READ(t,...) t __VA_ARGS__; _Reader(), __VA_ARGS__

void solve();
int main() { solve(); return 0; }

/// template end ///



void solve()
{
	while (true)
	{
		READ(int, H, W);
		if (!(W|H)) { break; }
		auto field = read<char>(W, H);
		for (auto &f : field) { f.insert(f.begin(), '0'); f.push_back('0'); }
		field.insert(field.begin(), vector<char>(H+2, '0'));
		field.push_back(vector<char>(H+2, '0'));
		W += 2; H += 2;

		int count = 0;
		REP(sx,W) REP(sy,H) if (field[sx][sy] == '1')
		{
			typedef complex<int> P;
			queue<P> q;
			q.push(P(sx, sy));
			while (!q.empty())
			{
				P cur = q.front(); q.pop();
				if (field[cur.real()][cur.imag()] == '0') { continue; }
				field[cur.real()][cur.imag()] = '0';
				P d[] = { P(-1,-1), P(0,-1), P(1,-1), P(-1, 0), P(1, 0), P(-1,1), P(0,1), P(1,1) };
				REP(i, 8)
				{
					P next = cur + d[i];
					if (field[next.real()][next.imag()] == '1') q.push(next);
				}
			}
			count++;
		}
		write(count);
	}
}

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

int w,h;
bool map[50][50];

void Del(int x,int y){

  map[y][x]=false;

  if(x!=w-1){
    if(map[y][x+1]==true){
      Del(x+1,y);
    }

    if(y!=0){
      if(map[y-1][x+1]==true){
        Del(x+1,y-1);
      }
    }
    if(y!=h-1){
      if(map[y+1][x+1]==true){
        Del(x+1,y+1);
      }
    }

  }


  if(x!=0){
    if(map[y][x-1]==true){
      Del(x-1,y);
    }
    if(y!=0){
      if(map[y-1][x-1]==true){
        Del(x-1,y-1);
      }
    }
    if(y!=h-1){
      if(map[y+1][x-1]==true){
        Del(x-1,y+1);
      }
    }

  }


  if(y!=h-1){
    if(map[y+1][x]==true){
      Del(x,y+1);
    }
  }
  if(y!=0){
    if(map[y-1][x]==true){
      Del(x,y-1);
    }
  }

}


int main(){


  int sum;

  while(cin >> w >> h){
    if(w==0 && h==0){ break; }

    for(int i=0; i<h; i++){
      for(int j=0; j<w; j++){
        cin >> map[i][j];
      }
    }

    sum = 0;

    for(int i=0; i<h; i++){
      for(int j=0; j<w; j++){
        if(map[i][j]==1){
          sum++;
          Del(j,i);
        }
      }
    }

    cout << sum << endl;

  }



  return 0;
}