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++: #define _crt_secure_no_warnings

#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif


#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;

vector<vector<int>>data;

int x[] = { 0, 1, 1, 1, 0, -1, -1, -1 };
int y[] = { 1, 1, 0, -1, -1, -1, 0, 1 };

void serch(int i, int j)
{
	data[i][j] = 0;
	for (size_t k = 0; k < 8; k++)
	{
		if (data[i + x[k]][j + y[k]])
		{
			serch(i + x[k], j + y[k]);
		}
	}
}

int main()
{
	int N, M;
	int ans = 0;
	while (1)
	{
		data.clear();
		ans = 0;
		cin >> N >> M;
		if (N == 0 && M == 0)break;
		swap(M, N);
		data.resize(N + 2);
		for (size_t i = 0; i < N + 2; i++)
		{
			data[i].resize(M + 2);
			for (size_t j = 0; j < M+2; j++)
			{
				data[i][j] = 0;
			}
		}
		for (size_t i = 1; i <= N; i++)
		{
			for (size_t j = 1; j <= M; j++)
			{
				cin >> data[i][j];
			}
		}
		for (size_t i = 1; i <= N; i++)
		{
			for (size_t j = 1; j <= M; j++)
			{
				if (data[i][j])
				{
					ans++;
					serch(i, j);
				}
			}
		}
		cout << ans<<endl;
	}
}

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

int prime[25] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
long long int dp[101][20] = {0};

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() {
    int w, h;
    cin >> w >> h;
    rep(i, h) rep(j, w) {
        int temp;
        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;
}

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

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

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

void dfs(int i, int j){
    c[i][j]=0;
    rep(dir,8){
        int vy=i+dy[dir];
        int vx=j+dx[dir];
        if(vy>=0&&vy<h&&vx>=0&&vx<w&&c[vy][vx]==1){
            dfs(vy,vx);
        }
    }
    return;
}

int main(){
    while(1){
        cin >> w >> h;
        if(h==0&&w==0)break;
        c = vector <vector <int> >(h, vector <int> (w) );
        rep(i,h)rep(j,w)cin >> c[i][j];
        int cnt=0;
        rep(i,h)rep(j,w){
            if(c[i][j]==1){
                dfs(i,j);
                cnt++;
            }
        }
        cout << cnt << endl;
    }
    return 0;
}

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

using ll = long long;
#define REP(i, n) for(int (i)=0; (i)< (n); ++i)
#define REPR(i, n) for(int (i)=(n); (i)>=0; --i)
#define FOR(i, n, m) for(int (i)=(n); (i)<(m); ++i)

int w, h;
vector<vector<int>> c(50, vector<int>(50)), homon(50, vector<int>(50));
int dx[] = {0, 1, 0, -1, 1, -1, -1, 1};
int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};

void dfs(int y, int x){
  homon[y][x] = 1;
  REP(i, 8){
    int nx = x + dx[i], ny = y+dy[i];
    if(nx < 0 || w <= nx || ny < 0 || h <= ny) continue;
    if(homon[ny][nx] || c[ny][nx] == 0) continue;
    dfs(ny, nx);
  }
}

int main(){
  vector<int> ans;
  while(true){
    cin >> w >> h;
    if(w==0 && h== 0) break;
    REP(i, h){
      REP(j, w){
        cin >> c[i][j];
      }
    }
    REP(i, h){
      REP(j, w){
        homon[i][j] = 0;
      }
    }
    int cnt = 0;
    REP(i, h){
      REP(j, w){
        if(homon[i][j]==1 || c[i][j]==0) continue;
        dfs(i, j);
        cnt++;
      }
    }
    ans.push_back(cnt);
  }
  REP(i, ans.size()){
    cout << ans[i] << endl;
  }
  return 0;
}

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

using ll=long long;
#define int ll

#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)

int read(){
	int i;
	scanf("%lld",&i);
	return i;
}

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

void isLand(int i,int j){
    if(land[i][j]==1){
    	land[i][j] = 0;
		REP(k,8){
			int newx=i+dy[k];
			int newy=j+dx[k];
			isLand(newy,newx);
		}
    }
}

signed main(){
	int w,h;
	while( w=read(),h=read(),w||h){
		cnt=0;
		
		REP(i,100){
			REP(j,100){
				land[i][j]=0;
			}
		}
		FOR(i,1,h+1){
			FOR(j,1,w+1){
				land[i][j]=read();
			}
		}
		
		FOR(i,1,h+1){
			FOR(j,1,w+1){
				if(land[i][j]==1){
					cnt++;
					isLand(i,j);
				}
			}
		}
		
		cout << cnt << endl;
	}
	
}