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++: /*
    問題をよく読もう！
    論理的に考えよう！
    サンプルを確認しよう!
    絶対に諦めるな！
    工夫をしろ！
    配列は少し多めにとっておく

    Twitterは終わるまでログアウト！
    （間違えて解法をツイートしてはいけないから）

*/
//include
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
#include<stdio.h>
#include<cstring>
//namespace
using namespace std;
//繰り返し
#define REP(i, m, n) for(int i=(int)m; i<(int)n; ++i)
#define rep(i, n) REP(i, 0, n)
//イテレータ
#define all_range(C) begin(C), end(C)
//簡略化
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
typedef pair<string,int> pst;

const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;
//最大最小
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) {if(a>b) a=b;}
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) {if(a<b) a=b;}

//関数

//最大公約数を求める
ll my_gcd(ll x, ll y) {
  ll r;

  while(y!=0) {
    r = x%y;
    x = y;
    y = r;
  }

  return x;
}

//最小公約数を求める
ll my_lcm(ll x, ll y) {

  return (x*y)/my_gcd(x,y);
}

//xのy乗を求める ※参考：10^18乗まで
ll my_pow(ll x, ll y) {
  ll sum = 1;

  while(y>0) {
    sum *= x;
  }

  return sum;
}

/*考えを書くスペース
xxxxxxxxxx  0000000000 最初のsearchで
xoooooooxx  0111111100
xxoooooxxx  0011111000
xxxoooxxxx  0001110000
xxxxoxxxxx  0000100000
xxxxxxxxxx  0000000000
xxxxoxxxxx  0000200000
xxxoooxxxx  0002220000
xxoooooxxx  0022222000
xxxxxxxxxx  0000000000
*/
//main.cpp----------------------------
#define max_h 50
#define max_w 50

void solve();
void search(int,int);

int h, w;
int c[max_h][max_w];

int main() {

  solve();

  return 0;
}

void solve() {

  while(1) {
    scanf("%d %d", &w, &h);
    if(w==0&&h==0) return;
    rep(i, h) {
      rep(j, w) {
        scanf("%d", &c[i][j]);
      }
    }

    int cnt = 0;
    rep(i, h) {
      rep(j, w) {
        if(c[i][j]==1) {
          search(i, j);
          cnt += 1;
        }
      }
    }

    cout << cnt << endl;
  }
}

void search(int x,int y) {

  if(x<0||x>=h||y<0||y>=w||c[x][y]==0)
    return;

  c[x][y] = 0;

  for(int dx=-1; dx<=1; ++dx) {
    for(int dy=-1; dy<=1; ++dy) {
      search(x+dx, y+dy);
    }
  }
}

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

#ifdef LOCAL
#include "../dump.hpp"
#else
#define dump(...)
#endif

#define rep(i,n) for(ll i=0,i##_cond=(n);i<i##_cond;++i)
#define FOR(i,a,b) for(ll i=(a),i##_cond=(b);i<i##_cond;++i)
#define ROF(i,a,b) for(ll i=(a)-1,i##_cond=(b);i>=i##_cond;--i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend() //sortで大きい順
#define UNIQUE(v) v.erase(unique(all(v)),v.end())
#define SUM(a) accumulate(all(a),0)
#define sz(x) ((ll)(x).size())
#define pb push_back
#define fst first
#define snd second
#define mp make_pair

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

const ll inf = 1ll<<62;
const ll mod = 1e9+7;

#define int ll

vi dx = {1, 0, -1, 0, 1, -1, -1, 1}, dy = {0, 1, 0, -1, 1, 1, -1, -1};

main()
{
  while(1){
    int h,w; cin >> w >> h;
    if(h == 0) break;
    vvi s(h,vi(w));
    rep(i,h) rep(j,w) cin >> s[i][j];
    vvi look(h,vi(w));

    int ans = 0;
    rep(i,h) rep(j,w){
      if(not look[i][j] and s[i][j]){
	ans++;
	queue<pii> q;

	q.push(pii(i,j));
	look[i][j] = 1;

	while(not q.empty()){
	  pii p = q.front();
	  q.pop();

	  rep(k, sz(dx)){
	    int nx = p.fst + dx[k], ny = p.snd + dy[k];
	    if(0 <= nx and nx < h and 0 <= ny and ny < w and s[nx][ny] and look[nx][ny] == 0){
	      q.push(pii(nx, ny));
	      look[nx][ny] = 1;
	    }
	  }
	}
      }
    }
    //dump(look);
    cout << ans << endl;
  }
}

C++: #include<bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define reps(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
	int w, h;
	int c[50][50] = {};

	void dfs(int i, int j) {
		c[i][j] = 0;

		for (int di = -1; di <= 1;di++) {
			for (int dj = -1; dj <= 1;dj++) {
				int ni = i + di;
				int nj = j + dj;

				if (0 <= ni&&ni < h && 0 <= nj&&nj < w && c[ni][nj] == 1) {
					dfs(ni, nj); 
				}
			}
		}
		return;
}

int main() {
	cin.sync_with_stdio(false);
	vector<int>ans;
	while (1) {
		cin >> w >> h;
		rep(i, h) {
			rep(j, w) {
				cin >> c[i][j];
			}
		}
		int res = 0;
		rep(i, h) {
			rep(j, w) {
				if (c[i][j] == 1) {
					dfs(i, j);
					res++;
				}
			}
		}
		ans.push_back(res);

		if (w == 0&&h == 0) {
			break;
		}
	}
	rep(i, ans.size()) {
		cout << ans[i] << endl;
	}

	return 0;
}

C++: /*
    Problem 25
    https://onlinejudge.u-aizu.ac.jp/problems/1160
*/
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 55;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
int h, w;
vector<vector<bool>> visited(MAX, vector<bool>(MAX));
vector<vector<int>> G(MAX, vector<int>(MAX));
/* function */
void init() {
    // for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) G[i][j] = false;
    // for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) visited[i][j] = false;
    for (int i = 0; i < h*w; i++) G[i/w][i%w] = false;
    for (int i = 0; i < h*w; i++) visited[i/w][i%w] = false;
}
void inputG() { // for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cin >> G[i][j];
    for (int i = 0; i < h*w; i++) cin >> G[i/w][i%w];}
bool check(int x, int y) {
    if (x < 0 || h < x || y < 0 || w < y) return false;
    if (visited[x][y] || !G[x][y]) return false;
    return true;
}
void bfs(int sx, int sy) {
    queue<pii> q;
    q.push(pii(sx, sy));
    visited[sx][sy] = true;
    while (!q.empty()) {
        pii p = q.front(); q.pop();
        int x = p.first, y = p.second;
        // 八方向を探索
        for (int dx = -1; dx <= 1; dx++) for (int dy = -1; dy <= 1; dy++) {
            int nx = x + dx, ny = y + dy;
            if (check(nx, ny)) {
                q.push(pii(nx, ny));
                visited[nx][ny] = true;
            }
        }
    }
}
void dfs(int x, int y) {
    // 訪れた
    visited[x][y] = true;
    // 八方向を探索
    for (int dx = -1; dx <= 1; dx++) for (int dy = -1; dy <= 1; dy++) {
        int nx = x + dx, ny = y + dy;
        // 範囲外 or 訪れたことがある(visited[i][j] == true) or 海(G[i][j] = 0)
        if (check(nx, ny))
            // 再帰的に探索
            dfs(nx, ny);
    }
}
void solve() {
    int ans = 0;
    for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) {
        if (!visited[i][j] && G[i][j]) {
            ans++;
            //bfs(i, j);
            dfs(i, j);
        }
    }
    cout << ans << '\n';
}

/* main */
int main(){
    while (1) {
        cin >> w >> h;
        if (h == 0) return 0;
        init();
        inputG();
        solve();
    }
}