#include <bits/stdc++.h>
using namespace std;
int h, w;
int G1[2010][2010], G[2010][2010], num[100010], cnt, sum[2010][2010];
bool vis[2010][2010];
const int dh[4] = {1, -1, 0, 0};
const int dw[4] = {0, 0, 1, -1};
inline bool isray(int r, int c) {
  return G1[r][c] && (sum[r + 4][c + 4] - sum[r - 5][c + 4] -
                          sum[r + 4][c - 5] + sum[r - 5][c - 5] <=
                      25);
}
void dfs(int bel, int r, int c) {
  G[r][c] = bel;
  for (int i = 0; i < 4; i++) {
    int nr = r + dh[i], nc = c + dw[i];
    if (nr <= 0 || nr > h || nc <= 0 || nc > w || G[nr][nc] || !G1[nr][nc])
      continue;
    dfs(bel, nr, nc);
  }
}
void dfs2(int r, int c) {
  vis[r][c] = true;
  for (int i = 0; i < 4; i++) {
    int nr = r + dh[i], nc = c + dw[i];
    if (vis[nr][nc] || !isray(nr, nc)) continue;
    dfs2(nr, nc);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> h >> w;
  for (int i = 10; i <= h + 9; i++)
    for (int j = 10; j <= w + 9; j++) cin >> G1[i][j];
  h += 20, w += 20;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + G1[i][j];
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      if (!G[i][j] && G1[i][j]) {
        cnt++;
        dfs(cnt, i, j);
      }
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      if (!vis[i][j] && isray(i, j)) {
        num[G[i][j]]++;
        dfs2(i, j);
      }
  cout << cnt << endl;
  sort(num + 1, num + cnt + 1);
  for (int i = 1; i <= cnt; i++) cout << num[i] << ' ';
  cout << endl;
  return 0;
}
