Go: package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

C++: #include <iostream>
#include <vector>
using namespace std;
#define MAX 50

int c[MAX][MAX];

void dfs(int w, int h, int x, int y);
int main(int argc, char const *argv[]){
    int w, h, ans;

    while (cin>>w>>h, w || h){
        ans = 0;
        for (int i = 0; i < h; i++){
            for (int j = 0; j < w; j++){
                cin>>c[i][j];
            }
        }
        for (int i = 0; i < w; i++){
            for (int j = 0; j < h; j++){
                if (c[i][j] == 1){
                    dfs(w, h, i, j);
                    ans++;
                }
            }
        }
        cout<<ans<<endl;
    }

    return 0;
}

void dfs(int w, int h, int x, int y){
    int dx[4] = {1, 0, -1, 0};
    int dy[4] = {0, 1, 0, -1};
    c[x][y] = 0;

    for (int i = 0; i < 4; i++){
        int nx = x + dx[i];
        int ny = y + dy[i];

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

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