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;
#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define rep(i, s, n) for (int i = s; i < n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(a) a.begin(),a.end()
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
const long long MOD = 1000000007, INF = 1e17;
 
#define endl '\n'
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0)
 
template<class T>inline bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;}
template<class T>inline bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;}

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

int a[55][55],H,W;

void dfs(int i,int j,vector<vector<int>>&used){
	used[i][j] = true;
	rep(k,0,8){
		int ni = i + dy[k],nj = j + dx[k];
		if(ni<0||nj<0||H<=ni||W<=nj)continue;
		if(a[ni][nj]==0)continue;
		if(used[ni][nj])continue;
		dfs(ni,nj,used);
	}
}

signed main()
{
	//IOS();
	vector<int>ans;
	while(1){
		cin>>W>>H;
		if(W==0&&H==0)break;
		for(int i = 0;i < H;i++){
			for(int j = 0;j < W;j++){
				cin>>a[i][j];
			}
		}
		vector<vector<int>>used(H,vector<int>(W,0));
		int cnt = 0;
		for(int i = 0;i < H;i++){
			for(int j = 0;j < W;j++){
				if(used[i][j])continue;
				if(a[i][j]==0)continue;
				dfs(i,j,used);
				cnt++;
			}
		}
		ans.push_back(cnt);
	}
	for(auto e:ans){
		cout<<e<<endl;
	}
}

C++: 
/*
Problem

Category
DFS

URL:
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp

考察:


Date: 2020/04/14
*/

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long

/* ==== declarations ====== */
// functions
void solve();
void getInput();

// global variables
int w, h;
vector<vector<bool>> C;
vector<vector<int>> color;
int currentColor = 1;


/* ======================== */

signed main(){
    while(true){
        getInput();
        solve();
    }
    return 0;
}

void getInput() {
    cin>>w>>h;
    if(!w and !h) {exit(0);}
    C = vector<vector<bool>>(h, vector<bool>(w, false));
    color = vector<vector<int>>(h, vector<int>(w, 0));
    for(int i=0; i<h; i++) {
        for(int j=0; j<w; ++j) {
            bool c;
            cin>>c;
            C[i][j] = c;
        }
    }
}

void dfs(int i, int j) {
    if (i < 0 or j < 0 or i >= h or j >= w) return;
    if (!C[i][j]) return;
    if (color[i][j]) return;
    color[i][j] = currentColor;
    
    for(int a: {i-1, i, i+1})
        for(int b: {j-1, j, j+1})
            dfs(a, b);
    
}


void solve() {
    for(int i=0; i<h; i++) {
        for(int j=0; j<w; ++j) {
            dfs(i, j);
            currentColor++;
        }
    }
    set<int> colorSet {0};
    for(int i=0; i<h; i++) {
        for(int j=0; j<w; ++j) {
            colorSet.insert(color[i][j]);
        }
    }
    cout<<colorSet.size()-1<<endl;
}

C++: #include <iostream>

using namespace std;

// 0: 海, 1: 陸
int board[50][50] = {{0}};

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

bool DFS(int i, int j, int w, int h) {
  if (i < 0 || i >= h || j < 0 || j >= w) return false;
  if (!board[i][j]) return false;

  board[i][j] = 0;
  for (int d = 0; d < 8; ++d)
    DFS(i + dy[d], j + dx[d], w, h);

  return true;
}

int main() {
  for (;;) {
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0) break;

    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        cin >> board[i][j];
      }
    }

    int ans = 0;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (DFS(i, j, w, h)) ++ans;
      }
    }
    cout << ans << endl;
  }
  return 0;
}