Go: ///
// File:  1160.go
// Author: ymiyamoto
//
// Created on Sun Mar  4 20:45:57 2018
//
package main

import (
	"fmt"
)

var w, h int
var graph [][]string

func dfs(visited [][]bool, x, y int) {
	visited[x][y] = true

	for i := -1; i <= 1; i++ {
		for j := -1; j <= 1; j++ {
			dx := x + i
			dy := y + j
			if 0 <= dx && dx < h && 0 <= dy && dy < w && graph[dx][dy] == "1" && !visited[dx][dy] {
				dfs(visited, dx, dy)
			}
		}
	}
}

func main() {
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			break
		}

		graph = make([][]string, h)
		for i := range graph {
			graph[i] = make([]string, w)
		}

		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				var s string
				fmt.Scan(&s)
				graph[i][j] = s
			}
		}

		visited := make([][]bool, h)
		for i := range visited {
			visited[i] = make([]bool, w)
		}

		count := 0
		for i := range graph {
			for j := range graph[i] {
				if graph[i][j] == "1" && !visited[i][j] {
					dfs(visited, i, j)
					count++
				}
			}
		}

		fmt.Println(count)
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 10007;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { 1, -1, 0, 0,  1, -1,  1, -1 };
static const int dy[] = { 0, 0, 1, -1 , 1,  1, -1, -1 };

string str;
int w, h, ans;
VVI ar;

int gcd( int m, int n ) {
	if ( n == 0 ) return m;
	return gcd( n, m % n );
}

void solve( int y, int x ) {
	ar[ y ][ x ] = 0;
	for ( int i = 0; i < 8; i++ ) {
		if ( ar[ y + dy[ i ] ][ x + dx[ i ] ] ) {
			solve( y + dy[ i ], x + dx[ i ] );
		}
	}
}

int main() {
	while ( ~scanf("%d %d", &w, &h) && w && h ) {
		ar.resize( h + 2 );
		for ( int i = 0; i < h + 2; i++ ) {
			ar[ i ].resize( w + 2 );
		}
		ans = 0;
		for ( int i = 0; i < w + 2; i++ ) {
			ar[ 0 ][ i ] = 0;
			ar[ h + 1 ][ i ] = 0;
		}
		for ( int i = 1; i < h + 1; i++ ) {
			for ( int j = 1; j < w + 1; j++ ) {
				scanf( "%d", &ar[ i ][ j ] );
			}
		}
		for ( int i = 0; i < h + 2; i++ ) {
			for ( int j = 0; j < w + 2; j++ ) {
				if ( ar[ i ][ j ] ) {
					ans++;
					solve( i, j );
				}
			}
		}
		printf( "%d\n", ans );
		ar.clear();
	}
	return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <functional>
#include <cstdlib>
#include <sstream>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <complex>
#include <vector>
#include <cstdio>
#include <math.h>
#include <cmath>
#include <time.h>
#define all(c) ((c).begin(),(c).end())
#define rall(c) (c).rbegin(),(c).rend()
#define sort(v,n) sort(v,v+n)
#define vsort(v) sort(v.begin(),v.end())
#define vvsort(v) sort(v.begin(),v.end(),greater<int>())
#define ll long long
#define pb(a) push_back(a)
#define fi first
#define se second
#define inf 999999999
using namespace std;
const ll MOD = 1e9 + 7;
const double PI = acos(-1.0);
//---------------------------------------------------------------------------------------------//
int w, h;
bool c[55][55];
int ans=0;

void dfs(int nowy, int nowx) {
	c[nowy][nowx] = 0;
	for (int i = -1; i < 2; i++) {
		for (int j = -1; j < 2; j++) {
			if (nowy == 0 && i == -1)continue;
			if (nowx == 0 && j == -1)continue;
			if (nowy == h - 1 && i == 1)continue;
			if (nowx == w - 1 && j == 1)continue;
			if (c[nowy + i][nowx + j] == 1) {
				dfs(nowy + i, nowx + j);
			}
		}
	}
}



int main() {
	while (cin >> w >> h, w) {
		ans = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> c[i][j];
			}
		}
		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;
	}
}

C++: #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <ctime>
#include <cstring>
#include <functional>
#include <iostream>
#include <iomanip>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <regex>
#include <vector>

#define fix(n)          cout<<fixed<<setprecision(n)
#define rep(i,n)        for (int i = 0; i < (n); ++i)
#define all(a)          (a).begin(), (a).end()
#define sort(a)         sort(all(a))
#define uniq(a)         sort(a);(a).erase(unique(all(a), (a).end())
#define reverse(a)      reverse(all(a))
#define ctos(c)         string(1, (c))
#define out(d)          cout << (d)
#define outl(d)         std::cout<<(d)<<"\n"
#define YES()           cout << "YES" << endl
#define NO()            cout << "NO" << endl
#define Yes()           cout << "Yes" << endl
#define No()            cout << "No" << endl
#define ceil(x, y)      ((x + y - 1) / (y))
#define debug(x)        cerr << #x << ": " << (x) << '\n'
#define debug2(x, y)    cerr << #x << ": " << (x) << ", " << #y << ": " << (y) << '\n'
#define debug3(x, y, z) cerr << #x << ": " << (x) << ", " << #y << ": " << (y) << ", " << #z << ": " << (z) << '\n'
#define dbg(v)          for (size_t _ = 0; _ < v.size(); ++_){ cerr << #v << "[" << _ << "] : " << v[_] << '\n'; }
#define pb              push_back
#define fst             first
#define int             long long
#define INF             __LONG_LONG_MAX__

using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll,ll>;

const ll MOD = 1000000007; // 10^9 + 7
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

void solve2(ll w, ll h) {
  vector<vector<int>> grid(h);
  rep(i, h) {
    grid[i].resize(w);
    rep(j, w) cin >> grid[i][j];
  }

  ll ans = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (grid[i][j] != 1) continue;

      queue<ll> qx, qy;
      qx.push(j); qy.push(i); grid[i][j] = -1;
      while (!qx.empty()) {
        ll x = qx.front(); qx.pop();
        ll y = qy.front(); qy.pop();

        for (int k = 0; k < 8; k++) {
          ll nx = x + dx[k];
          ll ny = y + dy[k];

          if (nx >= 0 && nx < w && ny >= 0 && ny < h && grid[ny][nx] == 1) {
            grid[ny][nx] = -1;
            qx.push(nx);
            qy.push(ny);
          }
        }
      }

      ans++;
    }
  }
  outl(ans);
}

void solve() {
  while (true) {
    ll w, h; cin >> w >> h;
    if (w == 0 && h == 0) break;
    solve2(w, h);
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  srand((unsigned)time(NULL));
  fix(12);

  solve();
}