#include <bits/stdc++.h>
using namespace std;
vector<vector<bool> > Cp;
vector<vector<bool> > Cr;
vector<vector<bool> > Cv;
vector<int> kq;
int n, m, dem;
char cha;
void Tries(int x, int y) {
  if (Cv[x][y] || !Cp[x][y]) {
    return;
  }
  Cv[x][y] = true;
  if (!Cp[x - 2][y] && !Cp[x + 2][y]) {
    if (!Cp[x][y - 1] && (!Cp[x - 2][y + 5] || !Cp[x + 2][y + 5])) {
      Cr[x][y] = true;
    }
    if (!Cp[x][y + 1] && (!Cp[x - 2][y - 5] || !Cp[x + 2][y - 5])) {
      Cr[x][y] = true;
    }
  }
  if (!Cp[x][y - 2] && !Cp[x][y + 2]) {
    if (!Cp[x - 1][y] && (!Cp[x + 5][y - 2] || !Cp[x + 5][y + 2])) {
      Cr[x][y] = true;
    }
    if (!Cp[x + 1][y] && (!Cp[x - 5][y - 2] || !Cp[x - 5][y + 2])) {
      Cr[x][y] = true;
    }
  }
  if (Cr[x][y] && !Cr[x - 1][y - 1] && !Cr[x - 1][y + 1] && !Cr[x + 1][y - 1] &&
      !Cr[x + 1][y + 1]) {
    ++dem;
  }
  Tries(x - 1, y);
  Tries(x + 1, y);
  Tries(x, y - 1);
  Tries(x, y + 1);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  Cp = vector<vector<bool> >(n + 4, vector<bool>(m + 4));
  Cr = vector<vector<bool> >(n + 4, vector<bool>(m + 4));
  Cv = vector<vector<bool> >(n + 4, vector<bool>(m + 4));
  n++;
  m++;
  for (int i = 2; i <= n; ++i) {
    for (int j = 2; j <= m; ++j) {
      cin >> cha;
      Cp[i][j] = (cha == '1');
    }
  }
  for (int i = 2; i <= n; ++i) {
    for (int j = 2; j <= m; ++j) {
      if (Cp[i][j] && !Cv[i][j]) {
        dem = 0;
        Tries(i, j);
        kq.push_back(dem);
      }
    }
  }
  sort(kq.begin(), kq.end());
  cout << kq.size() << endl;
  for (vector<int>::iterator tmp = kq.begin(); tmp != kq.end(); ++tmp) {
    cout << *tmp << ' ';
  }
  return 0;
}
