Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)
var route [][]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func dfs(h, w int) {
	if route[h][w] == 0 {
		return
	}

	route[h][w] = 0
	for _, x := range []int{-1, 0, 1} {
		for _, y := range []int{-1, 0, 1} {
			if route[h+x][w+y] == 0 {
				continue
			}
			dfs(h+x, w+y)
		}
	}
}

func main() {
	writer := bufio.NewWriter(os.Stdout)
	for {
		w, h := scanInt(), scanInt()
		if w == 0 && h == 0 {
			break
		}

		route = make([][]int, h+2)
		route[0] = make([]int, w+2)
		for i := 1; i <= h; i++ {
			route[i] = make([]int, w+2)
			for j := 1; j <= w; j++ {
				route[i][j] = scanInt()
			}
		}
		route[h+1] = make([]int, w+2)

		total := 0
		for i := 1; i <= h; i++ {
			for j := 1; j <= w; j++ {
				if route[i][j] == 1 {
					total++
					dfs(i, j)
				}
			}
		}
		fmt.Fprintln(writer, total)
	}
	writer.Flush()
}

C++: #include <bits/stdc++.h>
#ifdef _DEBUG
#include "debug.hpp"
#else
#define debug(...)
#endif
#define REP(i, m, n) for(int (i) = (m); (i) < (n); ++i)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) for(int (i) = (n)-1; (i) >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define FI first
#define SE second
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9+7;
const ll LINF = 1LL<<60;
template <class T, class U>
inline bool chmax(T &a, const U &b) {
  if(a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U>
inline bool chmin(T &a, const U &b) {
  if(a > b) {
    a = b;
    return true;
  }
  return false;
}

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

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

void dfs(int y, int x) {
  if(vis[y][x]) return;
  vis[y][x] = true;
  rep(i,8) {
    int nx = x+dx[i];
    int ny = y+dy[i];
    if(nx < 0 || w <= nx || ny < 0 || h <= ny || !c[ny][nx]) continue;
    dfs(ny, nx);
  }
}

int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  while(true) {
    cin >> w >> h;
    if(w == 0 && h == 0) break;
    c.assign(h, vector<int>(w));
    rep(i,h) rep(j,w) cin >> c[i][j];
    vis.assign(h, vector<bool>(w, false));
    int ans = 0;
    rep(i,h) rep(j,w) if(c[i][j] && !vis[i][j]) {
      ans++;
      dfs(i, j);
    }
    cout << ans << '\n';
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <string>
#include <queue>
#include <map>
#include <stack>
using namespace std;
 
int dx[8]={1,-1,0,0,1,1,-1,-1};
int dy[8]={0,0,1,-1,-1,1,-1,1};
 
 
int main() {
int yoko,tate,px,py;
while(cin>>yoko>>tate &&yoko){
 
    int m[55][55]={0};
 
string st;
//cin>>yoko>>tate;
for(int i=1;i<tate+1;i++){
for(int j=1;j<yoko+1;j++){
cin>>m[i][j];
}
}
int ans=0;

for(int i=0;i<tate+1;i++){
for(int j=0;j<yoko+1;j++){
if (m[i][j]==0) continue;  

stack<pair<int,int> > s;
s.push(make_pair(i,j));
m[i][j]=0;
//cout<<"pxpy "<<px<<" "<<py<<endl;
while(s.empty()==false){
px=s.top().first;
py=s.top().second;
s.pop();
 
for(int i=0;i<8;i++){
//cout<<px+dx[i]<<" "<<py+dy[i]<<endl;
    if(0<px+dx[i] && px+dx[i]<tate+1 && py+dy[i]>0 && py+dy[i]<yoko+1){
     
        if(m[px+dx[i]][py+dy[i]]==1){
            m[px+dx[i]][py+dy[i]]=0;
            s.push(make_pair(px+dx[i],py+dy[i]));
            
//cout<<px+dx[i]<<" "<<py+dy[i]<<" "<<ans<<endl;
        }
 
    }}
}
 ans++;
}}
cout<<ans<<endl;
 
}
 
 
 
    return 0;
}

C++: #include <bits/stdc++.h>
#include<iostream>
#include <string>
#include <cstring>
using namespace std;
const int dy[8] = {1, 1, 1, 0, -1, -1, -1, 0};
const int dx[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

int H, W;
vector<vector<bool>>seen(50, vector<bool>(50));
vector<vector<int>>field(50, vector<int>(50, 0));

void dfs(int h, int w) {
    if (seen[h][w]) {
        return;
    }

    seen[h][w] = true;
    for (int dir=0; dir < 8; dir++) {
        int nh = h + dy[dir];
        int nw = w + dx[dir];
        if (nh < 0 || nw < 0 || nh >= H || nw >= W) {
            continue;
        }

        if (field[nh][nw] == 0) {
            continue;
        }

        dfs(nh, nw);
    }
}

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++)
        {
            seen.at(i).at(j) = 0;
            cin >> field.at(i).at(j);
        }
    }

    int island_count = 0;
    for (int i = 0; i < H; i++)
    {
        for (int j = 0; j < W; j++)
        {
            if (seen[i][j]) {
                continue;
            }

            if (field[i][j] == 0) {
                continue;
            }

            dfs(i, j);
            island_count += 1;
        }
    }

    cout << island_count << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> p;

int field[55][55];
int w, h;
int dx[] = {0, 1, 0, -1, 1, -1, -1, 1};
int dy[] = {1, 0, -1, 0, 1, 1, -1, -1};

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

int main(){

  while(cin >> w >> h, w|h){
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        cin >> field[i][j];
      }
    }
    int sum = 0;
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
        if(field[i][j] == 1){
          sum++;
          dfs(i, j);
        }
      }
    }
    cout << sum << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vb=vector<bool>;
using vvb=vector<vb>;
using vd=vector<double>;
using vvd=vector<vd>;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll>;
using vs=vector<string>;
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define rep(i,n) range(i,0,n)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define range(i,a,n) for(int i=(a);i<(n);i++)

#define LINF    	((ll)1ll<<60)
#define INF     	((int)1<<30)
#define EPS     	(1e-9)
#define MOD     	(1000000007ll)
#define fcout(a)	cout<<setprecision(a)<<fixed
#define fs 			first
#define sc			second
#define PI			3.1415926535897932384

int dx[]={1,0,-1,0,1,-1,-1,1},dy[]={0,1,0,-1,1,1,-1,-1};
template<class S,class T>ostream&operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class S>auto&operator<<(ostream&os,vector<S>t){bool a=1; for(auto s:t){os<<(a?"":" ")<<s; a=0;} return os;}
void OUT(bool b){cout<<(b?"YES":"NO")<<endl;}
void Out(bool b){cout<<(b?"Yes":"No")<<endl;}
void out(bool b){cout<<(b?"yes":"no")<<endl;}
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int clz(int x) { return 31 - __builtin_clz(x); }
int clz(ll x) { return 63 - __builtin_clzll(x); }
int ctz(int x) { return __builtin_ctz(x); }
int ctz(ll x) { return __builtin_ctzll(x); }

class compare {
public:
	bool operator()(tuple<ll, ll> a, tuple<ll, ll> b) {
		return (get<1>(a) > get<1>(b));
	}
};

void solve(int h,int w){
	int in;
	int ans=0;
	vvb vv(h+2,(vb(w+2,false)));
	rep(i,h){
		rep(j,w){
			cin>>in;
			vv[i+1][j+1]=(in==1);
		}
	}
	rep(i,h){
		rep(j,w){
			if(vv[i+1][j+1]){
				stack<pll> stk;
				stk.push(pll(i+1,j+1));
				while(!stk.empty()){
					int x,y;
					tie(x,y)=stk.top();stk.pop();
					vv[x][y]=false;
					rep(k,8){
						if(vv[x+dx[k]][y+dy[k]])stk.push(pll(x+dx[k],y+dy[k]));
					}
				}
				ans++;
			}
		}
	}
	cout<<ans<<endl;
}
int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int w,h;
	while(cin>>h>>w,h){
		solve(w,h);
	}
}