#include <stdio.h>
#include <memory.h>
int c[50][50];
int w,h;
void solve(int i,int j){
  c[i][j]=0;
  if(i<h-1&&c[i+1][j]==1)
    solve(i+1,j);
  if(i>0&&c[i-1][j]==1)
    solve(i-1,j);
  if(j>0&&c[i][j-1]==1)
    solve(i,j-1);
  if(j<w-1&&c[i][j+1]==1)
    solve(i,j+1);
  if(i>0&&j>0&&c[i-1][j-1]==1)
    solve(i-1,j-1);
  if(i>0&&j<w-1&&c[i-1][j+1]==1)
    solve(i-1,j+1);
  if(i<h-1&&j>0&&c[i+1][j-1]==1)
    solve(i+1,j-1);
  if(i<h-1&&j<w-1&&c[i+1][j+1]==1)
    solve(i+1,j+1);
}
int main(void){
  int i,j,ans;
  while(1){
    scanf("%d %d",&w,&h);
    if(w+h==0)
      break;
    ans=0;
    memset(c,0,sizeof(c));
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
	scanf("%d",&c[i][j]);
      }
    }
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
	if(c[i][j]==1){
	  ans++;
	  solve(i,j);
	}
      }
    }
    printf("%d\n",ans);
  }
  return 0;
}