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>
#include <vector>
#include <string>
#include <queue>

#define rep(i, n) for(int i = 0; i < (n); i++)
#define MP make_pair
#define X first
#define Y second

using namespace std;

typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> P;

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

int main(){
	//freopen("test.txt", "r", stdin);
	int w, h;
	while(cin >> w >> h, w|h){
		vector<vi> v(h, vi(w, 0));
		rep(i, h){
			rep(j, w){
				cin >> v[i][j];
			}
		}

		int ans = 0;
		rep(i, h){
			rep(j, w){
				if(v[i][j] != 1)continue;
				ans++;
				queue<P> q;
				q.push(MP(j,i));
				while(!q.empty()){
					P p = q.front();
					q.pop();
					if(v[p.Y][p.X] != 1) continue;
					v[p.Y][p.X] = 0;
					rep(k, 8){
						int x = p.X+dx[k];
						int y = p.Y+dy[k];
						if(x < 0 || x >= w || y < 0 || y >= h) continue;
						q.push(MP(x, y));
					}
				}
			}
		}
		cout << ans << endl;
	}


	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
//using ll = long long;

int h, w;
vector< vector<int> > g;
//vector<vector<bool>> v;

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

int main() {
  while (cin >> w >> h) {
    if (h == 0 || w == 0) break;
    g.assign(h, vector<int>(w, 0));
    rep(i, h) rep(j, w) {
      cin >> g[i][j];
    }
    //v.assign(h, vector<bool>(w, false));
    int cnt = 0;
    rep(i, h) rep(j, w) {
      if (g[i][j] == 0) continue;
      dfs(i, j);
      cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>

using namespace std;

const int ddx[8] = {1,1,1,0,0,-1,-1,-1};
const int ddy[8] = {1,0,-1,1,-1,1,0,-1};

bool visited[60][60] ={false};
int v[60][60] = {0};
int h,w;

int dfs(int i ,int j){
    if(visited[i][j]) return 0;
    
    visited[i][j] = true;

    for(int x = 0; x < 8; x++){
        for(int y = 0; y < 8;y++){
            int posx = j + ddx[x];
            int posy = i + ddy[y];
            if(posx >= 0 && posx < w && posy >= 0 && posy < h){
                if(!visited[posy][posx] && v[posy][posx]){
                    dfs(posy, posx);
                }
            }
        }
    }
    return 1;
}

int main()
{
    while(cin >> w >> h, w and h){
        int ans = 0;

        for(int i = 0; i < 60 ;i++){
            for(int j = 0;j < 60; j++){
                visited[i][j] = false;
            }
        }

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

        for(int i = 0; i < h ;i++){
            for(int j = 0;j < w; j++){
                if(v[i][i] || !visited[i][j]) ans += dfs(i,j);
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
int W, H; 
char C[60][60] = {}; 
void search(int x, int y){
	C[x][y] = '0'; 
	for(int dx = -1; dx <= 1; dx++){
		for(int dy = -1; dy <= 1; dy++){
			int nx = x + dx; int ny = y + dy; 
			if(nx >= 0 && nx < H && ny >= 0 && ny < W && C[nx][ny] == '1') search(nx, ny); 
		}
	}
	return; 
}
int main() {
	for(int K; ; K++){
	cin >> W >> H; 
	if(W == 0){break;}
	for(int i = 0; i < H; i++){
		for(int j = 0; j < W; j++){
			cin >> C[i][j]; 
		}
	}
	int count = 0; 
	for(int i = 0; i < H; i++){
		for(int j = 0; j < W; j++){
			if(C[i][j] == '1'){search(i, j); count ++; }
		}
	}
	for(int i = 0; i < H; i++){
		for(int j = 0; j < W; j++){
			C[i][j] = '0'; 
		}
	}
	cout << count << endl; 
	}
}

C++: 
#include "bits/stdc++.h"
//#include <iostream>
//#include <vector>
//#include <string>
using namespace std;
#define repi(i,a,b) for(int i=(a), i##_len=(b); i<i##_len; ++i)
#define rep(i,n) repi(i,0,n)
#define repi_(i,a,b) for(int i=(a), i##_len=(b); i<=i##_len; ++i)
#define rep_(i,n) repi_(i,0,n)
#define all(x) (x).begin(),(x).end()
#define vsort(v) sort((v).begin(), (v).end())
#define gsort(v) tsort(v); reverse((v).begin(), (v).end())
#define vrev(v) reverse((v).begin(), (v).end())
#define siz(v) ((int)(v).size())
#define ers(v, n) (v).erase((v).begin() + n)
#define cnt(v, n) count(all(v), n)
#define vmin(v) *min_element(v.begin(), v.end())
#define vmax(v) *max_element(v.begin(), v.end())
#define el "\n"
#define co(x) cout<<(x)<<el
#define coc(c, a, b) if(c)co((a));else co((b))
#define cosp(x) cout<<(x)<<' '
#define YES(c) coc(c,"YES", "NO")
#define Yes(c) coc(c,"Yes", "No")
#define yes(c) coc(c,"yes", "no")
#define POSSIBLE(c) coc(c, "POSSIBLE", "IMPOSSIBLE")
#define Possible(c) coc(c, "Possible", "Impossible")
#define possible(c) coc(c, "possible", "impossible")
#define inf INT_MAX
#ifdef tqktmp_2
#define gc() getchar(); getchar()
#else
#define gc() 1
#endif
const unsigned int bf0 = (1 << 0);
const unsigned int bf1 = (1 << 1);
const unsigned int bf2 = (1 << 2);
const unsigned int bf3 = (1 << 3);
const unsigned int bf4 = (1 << 4);
const unsigned int bf5 = (1 << 5);
const unsigned int bf6 = (1 << 6);
const unsigned int bf7 = (1 << 7);

typedef long long lint;
typedef vector<int> ivec; typedef vector<string> svec;

const int
	dx8[8] = { 0,1,1,1,0,-1,-1,-1 },
	dy8[8] = { 1,1,0,-1,-1,-1,0,1 },
	dx4[4] = { 0,1,0,-1 },
	dy4[4] = { 1,0,-1,0 };

template<class T>inline bool maxi(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>inline bool mini(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }




lint w, h;
vector<ivec> m;
lint p, q;

int dfs(int x, int y) {//x, yからつながっている陸(1)をすべて2にし、1を返す
	if (m[x][y] != 1)return 0;
	m[x][y] = 2;
	rep(i, 8) {
		p = x + dx8[i], q = y + dy8[i];
		if (0 <= p && p < w && 0 <= q && q < h)dfs(p, q);
	}
	return 1;
}

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

	ivec in;
	lint ctr;
	while (cin >> h >> w) {
		if (h == 0 && w == 0)return 0;
		in.resize(h);
		rep(i, w) {
			rep(j, h) {
				cin >> in[j];
			}
			m.push_back(in);
		}
		ctr = 0;
		rep(i, w)rep(j, h) {
			ctr += dfs(i, j);
		}
		co(ctr);
		m.clear();
		vector<ivec>().swap(m);
	}
	gc();
}