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 "bits/stdc++.h"
 
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define int ll
#define fi first
#define se second
#define SORT(a) sort(a.begin(),a.end())
#define rep(i,n) for(int i = 0;i < (n) ; i++) 
#define REP(i,n) for(int i = 0;i < (n) ; i++) 
#define MP(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define INF LLONG_MAX/2
#define all(x) (x).begin(),(x).end()
#define debug(x) cerr<<#x<<": "<<x<<endl
#define debug_vec(v) cerr<<#v<<":";rep(i,v.size())cerr<<" "<<v[i];cerr<<endl
/*----------------------------------------------------------------*/

// int MOD = 998244353;
int MOD = 1000000007;

int w,h;
int c[51][51];
int dx[8] = {-1,0,1,-1,1,-1,0,1};
int dy[8] = {-1,-1,-1,0,0,1,1,1};
bool isin(int x,int y){
    if(x < h && 0 <= x && y < w && 0 <= y)return true;
    else return false;
}
void dfs(int x,int y){
    c[x][y] = 2;
    rep(i,8)if(isin(x+dx[i],y+dy[i]) && c[x+dx[i]][y+dy[i]] == 1)dfs(x+dx[i],y+dy[i]);
}

/*----------------------------------------------------------------*/
signed main(){

    while(1){
        cin >> w >> h;
        if(w == 0)break;
        int cnt = 0;
        rep(i,h)rep(j,w)cin >> c[i][j];
        rep(i,h)rep(j,w){
            if(c[i][j] == 1){
                cnt++;
                dfs(i,j);
            }
        }
        // rep(i,h){
        //     rep(j,w){
        //         cerr << c[i][j] << " ";
        //     }
        //     cerr << endl;
        // }
        cout << cnt << endl;
    }
  
	return 0;
}
/*----------------------------------------------------------------*/

// g++ -std=c++14 code1.cpp
// sudo pip3 install --upgrade online-judge-tools
// rm -r -f test;oj dl https://code-festival-2018-quala.contest.atcoder.jp/tasks/code_festival_2018_quala_c
// rm -r -f test;oj dl https://pakencamp-2019-day3.contest.atcoder.jp/tasks/pakencamp_2019_day3_c
// rm -r -f test;oj dl https://arc054.contest.atcoder.jp/tasks/arc054_b

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

bool passed[50][50];
char field[50][50];


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

void dfs(int y,int x){
	if(passed[y][x])
		return;
	passed[y][x] = true;
	for(int i = 0; i < 8; i++){
		int ny = y + dy[i];
		int nx = x + dx[i];
		if(ny >= 0 && nx >= 0 && ny < h && nx < w && !passed[ny][nx] && field[ny][nx] != '0'){
			dfs(ny,nx);
		}
	}
}

int main(){

	while(cin >> w >> h && !(w == 0 && h == 0)){
		for(int i = 0; i < h; i++){
			fill(passed[i],passed[i]+w,false);
			for(int j = 0; j < w; j++){
				cin >> field[i][j];
			}
		}
		int cnt = 0;
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				if(field[i][j] == '0' || passed[i][j]){
					continue;
				}
				else{
					cnt++;
					dfs(i,j);
				}
			}
		}
		cout << cnt << endl;
	}

	return 0;
}

C++: #include<iostream>
using namespace std;

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

int ma[60][60];

int W,H;
void dfs(int nx,int ny){
	if(ma[ny][nx]==0)return;
	ma[ny][nx]=0;
	int re=0;
	for(int i=0;i<8;i++){
		int nex=nx+dx[i],ney=ny+dy[i];
		if(nex>=0&&ney>=0&&nex<W &&ney<H)dfs(nex,ney);
	}
}


int main(){
	int sx,sy;
	while(cin>>W>>H && W+H){
		for(int i=0;i<H;i++)for(int j=0;j<W;j++){
			cin>>ma[i][j];
		}
		int ans=0;
		for(int i=0;i<H;i++)for(int j=0;j<W;j++){
			if(ma[i][j]==1){ans++;dfs(j,i);}
		}
		cout<<ans<<endl;
	}
	
}

C++: #include <algorithm>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>

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

using namespace std;

typedef long long ll;

int w, h;
int c[55][55];
int ans;

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(20) << fixed;

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

    rep(y, 30) { rep (x, 30) { c[y][x] = 0; } }

    rep(y, h) {
      rep (x, w) {
        cin >> c[y][x];
      }
    }
    ans = 0;
    rep(y, h) {
      rep(x, w) {
        if (c[y][x] == 1) {
          dfs(x, y);
          ans++;
        }
      }
    }
    cout << ans << endl;
  }

  return 0;
}