#include <stdio.h>
#define ll long long
#define rep(i,l,r)for(ll i=l;i<r;i++)

//union-find
#define UFLIMIT (1<<17)
int unicnt[UFLIMIT+10];//正ならcnt、非正なら根のindex
void ufinit(int n){rep(i,0,n)unicnt[i]=1;}
int root(int x){return unicnt[x]<=0?-(unicnt[x]=-root(-unicnt[x])):x;}
int same(int x,int y){return root(x)==root(y);}
void uni(int x,int y){if((x=root(x))==(y=root(y)))return;if(unicnt[x]<unicnt[y]){int t=x;x=y;y=t;}unicnt[x]+=unicnt[y];unicnt[y]=-x;}
#undef UFLIMIT

ll d8[]={0,1,1,0,-1,1,-1,-1,0};
int main(){
	ll w,h;
	while(scanf("%lld%lld",&w,&h),w){
		ll a[60][60]={};
		ll umi=0;
		rep(i,1,h+1)rep(j,1,w+1)scanf("%lld",&a[i][j]);
		ufinit(w*h);
		rep(i,1,h+1)rep(j,1,w+1){
			if(a[i][j]){
				rep(k,0,8){
					ll ii=i+d8[k];
					ll jj=j+d8[k+1];
					if(a[ii][jj])uni((i-1)*w+(j-1),(ii-1)*w+(jj-1));		
				}
			}else{
				umi++;
			}
		}
		ll ans=0;
		rep(i,1,h+1)rep(j,1,w+1)if(unicnt[(i-1)*w+(j-1)]>0)ans++;
		printf("%lld\n",ans-umi);
	}
}
