#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int arr[27][27] = { 0, };
int N, num = 0;
vector<int> v;
void search(int i, int j) {
v[num]++;
arr[i][j] = -1;
if(arr[i+1][j] == 1)
search(i + 1, j);
if(arr[i-1][j] == 1)
search(i - 1, j);
if(arr[i][j+1] == 1)
search(i, j + 1);
if(arr[i][j-1] == 1)
search(i, j - 1);
}
int main() {
cin >> N;
for (int i = 0; i<27; i++) {
for (int j = 0; j<27; j++) {
arr[i][j] = 0;
}
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
int tmp;
scanf("%1d", &tmp);
arr[i][j] = tmp;
}
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
if (arr[i][j] == 1) {
v.push_back(0);
search(i, j);
num++;
}
}
}
sort(v.begin(), v.end());
cout<<v.size()<<endl;
for (int i = 0; i<v.size(); i++) {
cout << v[i] << endl;
}
}