Go: package main

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

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		return []byte(f1())
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

// -----------------------------------------------------------------------------

func main() {
	defer stdout.Flush()

	for {
		ans, ok := solve()
		if !ok {
			break
		}
		println(ans)
	}

}

func solve() (int, bool) {
	w := readInt()
	h := readInt()
	if w == 0 && h == 0 {
		return 0, false
	}

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

	var ans int
	for i := 1; i-1 < h; i++ {
		for j := 1; j-1 < w; j++ {
			if 0 < c[i][j] {
				ans++
				dfs(c, i, j)
			}
		}
	}
	return ans, true
}

func dfs(c [][]int, y, x int) {
	c[y][x] = 0
	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{-1, -1, -1, 0, 0, 1, 1, 1}
	for i := range dx {
		nx := x + dx[i]
		ny := y + dy[i]
		if 0 < c[ny][nx] {
			dfs(c, ny, nx)
		}
	}
}

// -----------------------------------------------------------------------------

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <set>
#include <map>
#include <algorithm>

#define int long long
#define MOD7 1000000007
#define MOD9 1000000009

#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()

using namespace std;

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

int nextInt() {int a; cin >> a; return a;}
char nextChar() {char a; cin >> a; return a;}
double nextDouble() {double a; cin >> a; return a;}
string nextString() {string a; cin >> a; return a;}

void inputVector(vector<int> &v, int &n) {rep(i,n){v.push_back(nextInt());}}
void inputVector(vector<double> &v, int &n) {rep(i,n){v.push_back(nextDouble());}}
void inputVector(vector<string> &v, int &n) {rep(i,n){v.push_back(nextString());}}

void paint(vector<string> &board, int y, int x) {
	queue<int> q;
	q.push(y * 100 + x);
	board[y][x] = '0';
	while (!q.empty()) {
		int now = q.front(); q.pop();

		int nowX = now % 100;
		int nowY = now / 100;

		for (int ay = -1; ay <= 1; ay++) {
			for (int ax = -1; ax <= 1; ax++) {
				int nx = nowX + ax;
				int ny = nowY + ay;
				if (nx < 0 || nx >= board[0].size() || ny < 0 || ny >= board.size()) continue;
				if (board[ny][nx] == '0') continue;

				q.push(ny * 100 + nx);
				board[ny][nx] = '0';
			}
		}
	}
}

signed main() {
	while (true) {
		int w, h;
		cin >> w >> h;

		if (w == 0) break;

		vector<string> board;
		rep(y, h) {
			string tmp = "";
			rep(x, w) {
				tmp += nextChar();
			}
			board.push_back(tmp);
		}

		int ret = 0;
		rep(y, h) {
			rep(x, w) {
				if (board[y][x] == '1') {
					paint(board, y, x);
					ret++;
				}
			}
		}

		cout << ret << endl;
	}

    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<cstring>
#include<string>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;

#define REP(i,a,n) for(int i = a ; i < n  ;i++)
#define rep(i,n) REP(i,0,n)

typedef long long ll;

#define MAX 100

int w, h;
int t[MAX][MAX];

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

bool destroy(int x, int y){
  if(t[y][x] == 0) return false;
  t[y][x] = 0;
  rep(i, 8){
    int nx = x + dx[i], ny = y + dy[i];
    if(nx < 0 || w <= nx || ny < 0 || h <= ny) continue;
    destroy(nx, ny);
  }
  return true;
}

void solve(){
  int res = 0;
  rep(i,h) rep(j,w) if(destroy(j,i)) res++;
  cout << res << endl;
}
int main(){
  while(cin >> w >> h){
    if( w==0 && h ==0) break;
    rep(i, h) rep(j, w) cin >> t[i][j];
    solve();
  }
}

C++: /**
 * @Author: yujirotakahashi
 * @Date:   2020-05-17T20:21:41+09:00
 * @Last modified by:   yujirotakahashi
 * @Last modified time: 2020-05-23T00:13:28+09:00
 */

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

/*******  All Required define Pre-Processors and typedef Constants *******/
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define rep(i,n) for(int i=0; i<(n); ++i)
#define rep1(i,n) for(int i=1; i<=(n); ++i)
#define Dcout(a) cout << setprecision(20) << a << endl
#define MP make_pair
#define PB push_back
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
//const ll INF = (1LL << 60);
//const double pi=acos(-1.0);
using ll = long long;
using PAIR = pair<int, int>;
#define debug cout << "line : " << __LINE__ << " debug" << endl
/****** Template of some basic operations *****/
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
/**************************************/

/******** User-defined Function *******/
using Graph = vector<vector<int>>;
int w,h;
int c[55][55];
bool seen[55][55];
int dx[8] = {-1,0,1,-1,1,-1,0,1};
int dy[8] = {-1,-1,-1,0,0,1,1,1};
void dfs(int y,int x){
    seen[y][x] = true;

    rep(i,8){
        int nx = x + dx[i], ny = y + dy[i];
        if(nx<0 || nx>=w || ny<0 || ny>=h) continue;
        if(seen[ny][nx] == true) continue;
        if(c[ny][nx] == 0) continue;

        dfs(ny,nx);
    }

    return;
}
/**************************************/

/********** Main()  function **********/
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  while(cin >> w >> h){
      if(w==0 && h==0) break;

      rep(i,h)rep(j,w) cin >> c[i][j];
      memset(seen, false, sizeof(seen));
      int cnt = 0;
      rep(i,h)rep(j,w){
          if(seen[i][j]==false && c[i][j] == 1){
              dfs(i,j);
              cnt++;
          }
      }

      cout << cnt << endl;
  }

  return 0;
}
/********  Main() Ends Here *************/