/*
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello world!\n");
return 0;
}
*/
/*
#include<stdio.h>
int count[1000] = {0},sum=0;
char map[105][105] = {0};
void f(int x, int y)
{
count[sum]++;
map[x][y] = 0;
if(map[x-1][y]=='L')
{
f(x-1, y);
}
if(map[x+1][y]=='L')
{
f(x+1, y);
}
if(map[x][y-1]=='L')
{
f(x, y-1);
}
if(map[x][y+1]=='L')
{
f(x, y+1);
}
if(map[x-1][y-1]=='L')
{
f(x-1, y-1);
}
if(map[x+1][y+1]=='L')
{
f(x+1, y+1);
}
if(map[x-1][y+1]=='L')
{
f(x-1, y+1);
}
if(map[x+1][y-1]=='L')
{
f(x+1, y-1);
}
}
int main()
{
int n,m,i,j;
scanf ("%d %d",&n,&m);
for(i=1; i<=m; i++)
{
for(j=1; j<=n; j++)
{
scanf(" %c",&map[i][j]);
}
}
for(i=1; i<=m; i++)
{
for(j=1; j<=n; j++)
{
if(map[i][j]=='L')
{
f(i,j);
sum++;
}
}
}
printf("%d", sum);
return 0;
}
*/
#include<stdio.h>
int map[30][30] = {0}, count[1000] = {0},sum=0;
void f(int x, int y)
{
count[sum]++;
map[x][y] = 0;
if(map[x-1][y]==1)
{
f(x-1, y);
}
if(map[x+1][y]==1)
{
f(x+1, y);
}
if(map[x][y-1]==1)
{
f(x, y-1);
}
if(map[x][y+1]==1)
{
f(x, y+1);
}
}
int main()
{
int i,j,n,temp=0;
scanf ("%d",&n);
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
scanf ("%1d",&map[i][j]);
}
}
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
if(map[i][j]==1)
{
f(i,j);
sum++;
}
}
}
printf("%d\n", sum);
for(i=0; i<sum-1; i++)
{
if(count[i]>count[i+1])
{
temp=count[i];
count[i]=count[i+1];
count[i+1]=temp;
}
}
for(i=0; i<sum; i++)
{
printf("%d\n", count[i]);
}
}
/*
8
00101011
11101101
01110111
10011110
10010101
11101011
00101101
11110001
*/