Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

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;
}