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++: /**
 *    author:  otera    
**/
#include<iostream>
#include<string> 
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm> 
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

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

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

void dfs(int i, int j) {
    c[i][j] = '0';
    rep(k, 8) {
        int ni = i + dx[k], nj = j + dy[k];
        if(0 <= ni and ni < h and 0 <= nj and nj < w and c[ni][nj] == '1') {
            dfs(ni, nj);
        }
    }
}

void solve() {
    while(cin >> w >> h) {
        if(w == 0 and h == 0) break;
        c.assign(h, vector<char>(w));
        rep(i, h) {
            rep(j, w) {
                cin >> c[i][j];
            }
        }
        int ans = 0;
        rep(i, h) {
            rep(j, w) {
                if(c[i][j] == '1') dfs(i, j), ++ ans;
            }
        }
        cout << ans << endl;
    }
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
#include <deque>
#include <assert.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <math.h>
#include <bitset>
#include <iomanip>
#include <complex>
#include <ios>
#include <iomanip>
using namespace std;
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define ALL(x) (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define IDX(vec, element_iter) distance((vec).begin(), element_iter)
#define print(x) cout<<(x)<<'\n'
#define print_vec(v) rep(i,v.size()){if(i!=0)cout<<" ";cout<<v[i];}cout<<'\n'
#define pe(x) cout<<(x)<<" "
#define DEBUG(x) cout<<#x<<": "<<x<<'\n'
#define DEBUG_VEC(v) cout<<#v<<":";rep(i,v.size())cout<<" "<<v[i];cout<<'\n'
#define pb push_back
#define mp make_pair
#define MOD 1000000007 //10^9+7
#define fi first
#define sc second
using ll=long long;
using pi=pair<int,int>;
using pll=pair<ll,ll>;
using vi=vector<int>;
using vll=vector<ll>;
using vstr=vector<string>;


int w,h,c[50][50];
int dx[] = {-1,0,1,-1,1,-1,0,1};
int dy[] = {1,1,1,0,0,-1,-1,-1};

bool inRange(int y,int x){
    if(0<=x && x<w && 0<=y && y<h) return true;
    else return false;
}

void dfs(int x,int y){
    c[x][y] = 0;
    rep(i,8){
        int x_next=x+dx[i],y_next=y+dy[i];
        if(inRange(x_next,y_next) && c[x_next][y_next]==1){
            dfs(x_next,y_next);
        }
    }
}

void solve() {
    while(1){
        cin >> w >> h;
        if(w==0 && h==0) break;
        rep(row,h){
            rep(col,w){
                cin >> c[row][col];
            }
        }

        int ans = 0;
        rep(row,h){
            rep(col,w){
                if(c[row][col]==1){
                    ans++;
                    dfs(row,col);
                }
            }
        }

        print(ans);
    }
}
 
int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
	cout<<fixed<<setprecision(20);
    //int q; cin >> q;
	//while (q--)
		solve();
    return 0;
}

C++: #include<iostream>
using namespace std;
const int MAXW = 51;
const int MAXH = 51;
int map[MAXW][MAXH];
int w,h;
int dx[] = {-1,0,1,-1,1,-1,0,1};
int dy[] = {1,1,1,0,0,-1,-1,-1};
int dfs(int x,int y){
  if(map[x][y] == 0)return 0;
  map[x][y] = 0;
  for(int i = 0;i < 9;i++){
    int nx = x+dx[i];
    int ny = y+dy[i];
    if(nx >= 0 && nx < w & ny >= 0 && ny < h)
      dfs(nx,ny);
  }
  return 1;
}

int main(){
  while(true){
    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[j][i];
      }
    }
    int count = 0;
    for(int i = 0;i < h;i++){
      for(int j = 0;j < w;j++){
        count += dfs(j,i);
      }
    }
    cout << count << endl;
  }
}

C++: #include<iostream>

using namespace std;

const int MaxI = 51;
int C[MaxI][MaxI];
int Dh[] = {-1,-1,-1,0,0,1,1,1};
int Dw[] = {-1,0,1,-1,1,-1,0,1};
int H, W;

void dfs(int i, int j);

int main() {

  while(cin>>W>>H && !(W==0 && H==0)) {
    for(int i=0; i<H; i++) {
      for(int j=0; j<W; j++) {
	cin>>C[i][j];
      }
    }

    int ans = 0;
    for(int i=0; i<H; i++) {
      for(int j=0; j<W; j++) {
	if(C[i][j] == 1) {
	  dfs(i,j);
	  ans++;
	}
      }
    }
    cout<<ans<<endl;
  }
}

void dfs(int i, int j) {
  C[i][j] = 0;
  for(int a=0; a<8; a++) {
    if(0<=i+Dh[a] && i+Dh[a]<H && 0<=j+Dw[a] && j+Dw[a]<W && C[i+Dh[a]][j+Dw[a]] == 1) {
      dfs(i+Dh[a], j+Dw[a]);
    }
  }
}