#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1600 + 10;
const int d[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
bool a[MAXN][MAXN], b[MAXN][MAXN];
int n, m;
vector<pair<int, int> > p;
vector<int> CC;
inline int Read() {
  char ch = getchar();
  while (!((ch >= '0' && ch <= '9') || ch == '-')) ch = getchar();
  int first = 0, p = 1;
  if (ch == '-') p = -1, ch = getchar();
  while (ch >= '0' && ch <= '9') first = first * 10 + ch - '0', ch = getchar();
  return first * p;
}
bool c[MAXN][MAXN];
void Contract_Expand(int lim) {
  memset(c, 0, sizeof(c));
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      int cnt = 0;
      for (int l = 0; l < 4; ++l) cnt += b[i + d[l][0]][j + d[l][1]];
      c[i][j] = (cnt >= lim);
    }
  memcpy(b, c, sizeof(b));
}
void DFS(int first, int second) {
  a[first][second] = 0;
  p.push_back(make_pair(first, second));
  for (int nx = first - 1; nx <= first + 1; ++nx)
    for (int ny = second - 1; ny <= second + 1; ++ny)
      if (a[nx][ny]) DFS(nx, ny);
}
int DFS2(int first, int second) {
  a[first][second] = 0;
  int CC = 1;
  for (int l = 0; l < 4; ++l)
    if (a[first + d[l][0]][second + d[l][1]])
      CC += DFS2(first + d[l][0], second + d[l][1]);
  return CC;
}
int Count() {
  for (int i = 0; i < (int)p.size(); ++i)
    if (!b[p[i].first][p[i].second]) a[p[i].first][p[i].second] = 1;
  int CC = 0;
  for (int i = 0; i < (int)p.size(); ++i)
    if (a[p[i].first][p[i].second]) CC += DFS2(p[i].first, p[i].second) > 6;
  return CC;
}
int main() {
  n = Read();
  m = Read();
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) a[i][j] = Read();
  memcpy(b, a, sizeof(a));
  for (int t = 1; t <= 4; ++t) Contract_Expand(4);
  for (int t = 1; t <= 8; ++t) Contract_Expand(1);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (a[i][j]) {
        p.clear();
        DFS(i, j);
        CC.push_back(Count());
      }
  sort(CC.begin(), CC.end());
  cout << CC.size() << endl;
  for (int i = 0; i < (int)CC.size(); ++i) cout << CC[i] << " ";
  cout << endl;
  fclose(stdin);
  fclose(stdout);
  return 0;
}
