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<queue>
#include<algorithm>
#include<functional>
#define rep(i, n) for(int i = 0; i < n; i++)
#define repp(i, a, b) for(int i = a; i <= b; i++)
using namespace std;
using ll = long long;
using vel = vector<ll>;
using vvel = vector<vel>;

vvel maze;
ll w, h;

ll dfs(ll y, ll x) {
    if(maze[y][x]!=1) return 0;
    maze[y][x] = -1;
    repp(dy, -1, 1) {
        repp(dx, -1, 1) {
            if(y+dy < 0 || y+dy >= h || x+dx < 0 || x+dx >= w ) continue;
            if(dx == 0 && dy == 0) continue;
            dfs(y+dy, x+dx);
        }
    }
    return 1;
};

int main() {
    while(cin >> w >> h, w) {
        maze.assign(h, vel(w, 0));
        rep(y, h) rep(x, w) cin >> maze[y][x];
        ll ans = 0;
        rep(y, h) {
            rep(x, w) {
                if(maze[y][x] == 1) 
                    ans += dfs(y, x);
            }
        }
        cout << ans << endl;
    }
}

C++: // AOJ 1160
#include<iostream>
#define rep(i,a) for( int i = 0; i != (a); ++i )

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

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

void dfs( int y, int x )
{
	c[y][x] = false;

	rep( i, 8 )
	{
		int nx = x+dx[i], ny = y+dy[i];

		if( nx >= 0 && nx < w && ny >= 0 && ny < h && c[ny][nx] )
			dfs( ny, nx );
	}

	return;
}

int main()
{
	while( std::cin >> w >> h, w|h )
	{
		rep( i, h )
			rep( j, w )
				std::cin >> c[i][j];

		int ans = 0;
		rep( i, h )
			rep( j, w )
				if( c[i][j] )
					dfs( i, j ), ++ans;

		std::cout << ans << std::endl;
	}

	return 0;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>

using namespace std;

inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}

typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EACH(t,i,c) for(t::iretator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

const double EPS = 1e-10;

int seamap[52][52];
int num = 0;

void dfs(int x, int y) {
	if(seamap[x][y]) {
		seamap[x][y] = 0;

		if(seamap[x-1][y-1]) {
			dfs(x-1, y-1);
		}
		if(seamap[x][y-1]) {
			dfs(x, y-1);
		}
		if(seamap[x+1][y-1]) {
			dfs(x+1, y-1);
		}
		if(seamap[x-1][y]) {
			dfs(x-1, y);
		}
		if(seamap[x+1][y]) {
			dfs(x+1, y);
		}
		if(seamap[x-1][y+1]) {
			dfs(x-1, y+1);
		}
		if(seamap[x][y+1]) {
			dfs(x, y+1);
		}
		if(seamap[x+1][y+1]) {
			dfs(x+1, y+1);
		}
	}
}


int main() {
	int w, h;

	REP(i, 52) {
		REP(j, 52) {
			seamap[i][j] = 0;
		}
	}

	while(cin >> w >> h, (w!=0 && h!=0)) {
		FOR(i, 0, 52) {
			FOR(j, 0, 52) {
				seamap[i][j] = 0;
			}
		}

		FOR(i, 1, h+1) {
			FOR(j, 1, w+1) {
				int p;
				cin >> p;
				seamap[i][j] = p;
			}
		}

		num = 0;

		FOR(i, 1, 50) {
			FOR(j, 1, 50) {
				if(seamap[i][j]) {
					num++;
					dfs(i, j);
				}
			}
		}

		cout << num << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
using ll = long long;
string char_to_string(char val) {
  return string(1, val);
}
int char_to_int(char val) {
  return val - '0';
}
template<class T> inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template<class T> inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int vector_finder(std::vector<ll> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance( vec.begin(), itr );
  if (index != vec.size()) { // 発見できたとき
    return 1;
  }
  else { // 発見できなかったとき
    return 0;
  }
}

int w, h;
vector<vector<int>> c;
int ans;

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

void dfs(int _x, int _y) {
  // 訪問したマスは0に変えて
  c[_x][_y] = 0;
  // 次行くマスを決めて
  for(int i = 0; i < 8; ++i) {
    if(_x + dx[i] < 0 || _x + dx[i] >= h || _y + dy[i] < 0 || _y + dy[i] >= w) continue;
    if(c[_x + dx[i]][_y + dy[i]] == 1) dfs(_x + dx[i], _y + dy[i]);
  }
}

int main() {
  while(true) {
    cin >> w >> h;
    c.resize(h);
    if(w == 0 && h == 0) break;
    REP(k, h) {
      REP(m, w) {
        int C; cin >> C;
        c[k].push_back(C);
      }
    }
    ans = 0;
    REP(i, h) {
      REP(k, w) {
        if(c[i][k] == 1) {
          dfs(i, k);
          ans++;
        }
      }
    }
    cout << ans << endl;
    c.clear();
  }
}

C++: #include <iostream>

using namespace std;

int map[50][50];

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

void dfs(int x, int y,int w, int h){
	if(x<0 || y<0 || w<=x || h<=y || map[y][x] == 0) return;
	else{
		map[y][x] = 0;
		for(int i=0;i<8;++i){
			dfs(x+dx[i],y+dy[i],w,h);
		}
	}
}

int solve(int w,int h){
	int ans;
	
	int i,j,k;
	ans = 0;
	for(i=0;i<h;++i){
		for(j=0;j<w;++j){
			if(map[i][j]==1){
				map[i][j] = 0;
				for(int k=0;k<8;++k){
					dfs(j+dx[k],i+dy[k],w,h);
				}
				ans++;
			}
		}
	}
	return ans;
}

int main(void){
	int w,h;
	int i,j;
	
	while(cin>>w>>h,w){
		for(i=0;i<h;++i){
			for(j=0;j<w;++j){
				cin >> map[i][j];
			}
		}
		
		cout << solve(w,h) << endl;
	}
	
}