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>

#define PI 3.141592653
#define rep(i,a,n) for(int i=a;i<(int)n;++i)
#define SZ(x) ((int)(x).size())	//size() unsigned -> int
#define descSort(a) sort(a.begin(),a.end(),std::greater<int>())
using namespace std;
typedef long long ll;
const ll INF = 1e9 + 7;
ll gcd(ll x,ll y){
    if(x%y==0)return y;
    return gcd(y,x%y);
}

vector<vector<bool>> visited;
int dx[8]={-1,-1,0,1,1,1,0,-1};
int dy[8]={0,1,1,1,0,-1,-1,-1};
int n,m;
int dfs(int x,int y,vector<vector<int>> field){
    if(visited[x][y])return 0;
    visited[x][y]=true;
    int ret=1;
    for(int i=0;i<8;i++){
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(nx<0||ny<0||nx>=n||ny>=m)continue;
        if(visited[nx][ny]==false&&field[nx][ny]){
            ret=dfs(nx,ny,field);
        }
    }
    return ret;
}
int main(void){
    vector<int> ans;
    while(1){
        cin>>m>>n;
        if(n==0&&m==0)break;
        vector<vector<int>> field(n,vector<int>(m));
        rep(i,0,n){
            rep(j,0,m){
                cin>>field[i][j];
            }
        }
        visited=vector<vector<bool>>(n,vector<bool>(m));
        int ret=0;
        rep(i,0,n){
            rep(j,0,m){
                if(field[i][j]){
                    ret += dfs(i,j,field);
                }
            }
        }
        ans.push_back(ret);
    }
    for(auto i:ans){
        cout<<i<<endl;
    }
    return 0;
}

C++: #include<stdio.h>
int m[52][52];
void F(int x,int y)
{
	if(m[y][x])
	{
		m[y][x]=0;
		int i,j;
		for(i=-1;i<2;++i)for(j=-1;j<2;++j)F(x+i,y+j);
	}
}
int main()
{
	int h,w,i,j,r;
	while(scanf("%d%d",&w,&h),w)
	{
		for(i=1;i<=h;++i)for(j=1;j<=w;++j)scanf("%d",&m[i][j]);
		for(i=0;i<h+2;++i)m[i][w+1]=0;
		for(r=j=0;j<w+2;++j)m[h+1][j]=0;
		for(i=1;i<=h;++i)for(j=1;j<=w;++j)if(m[i][j])F(j,i),++r;
		printf("%d\n",r);
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;
#define TOSTRING(x) #x
#define SZ(x) (int)(x).size()
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(s) (s).begin(), (s).end()
#define dis distance
#define so(V) sort(ALL(V))
#define rev(V) reverse(ALL(V))
#define uni(v) v.erase( unique(ALL(v)) , v.end());

typedef long long unsigned int llu;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vector <int> > vvi;
typedef vector<vb> vvb;
const double EPS = 1e-9;
const int MOD = 1e9+7;
const int INF = (1 << 29);
const ll LINF = 1e18;
const double PI = acos(-1);


typedef pair<int,bool> P;

int w,h;

bool check(int y, int x){
    if(y < 0 || y >= h)return false;
    if(x < 0 || x >= w)return false;
    return true;
}

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, vvb &V){
    V[y][x] = false;
    REP(i,8){
        if(check(y + dy[i],x + dx[i]) && V[y + dy[i]][x + dx[i]]){
            dfs(y + dy[i], x + dx[i], V);
        }
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> w >> h;
    vi ansV;
    while(w != 0 && h != 0){
        int ans = 0;
        vvb V(h,vb(w,false));
        REP(i,h){
            REP(j,w){
                int a;
                cin >> a;
                if(a == 1)V[i][j] = true;
            }
        }
        REP(i,h){
            REP(j,w){
                if(V[i][j]){
                    dfs(i,j,V);
                    ans++;
                }
            }
        }
        ansV.push_back(ans);
        cin >> w >> h;
    }
    for(int a: ansV){
        cout << a << endl;
    }
    return 0;
}

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

typedef long long llint;
typedef long double ld;
typedef pair<int,int> P;

#define inf 1e18
#define mod 1000000007
#define sort(v) sort(v.begin(),v.end())
#define reverse(v) reverse(v.begin(),v.end())
#define rep(i,a,n) for(int i=a;i<n;i++)


priority_queue<llint,vector<llint>,greater<llint> > que;
priority_queue<llint> Que;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }


#define W 51
#define H 51

int c[H][W];


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

void solve(){
    int h,w;
    while(cin >> w >> h){
        if(w==0 && h==0)break;
        rep(i,0,51)rep(j,0,51)c[i][j]=0;
        rep(i,0,h){
            rep(j,0,w){
                cin >> c[i][j];
            }
        }
        int ans=0;
        rep(i,0,h){
            rep(j,0,w){
                if(c[i][j]==1){
                    dfs(i,j,h,w);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }


    
}

int main(){
    solve();
    return 0;
}