#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007, dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
const double eps = 1e-8;
void read(int &k) {
  k = 0;
  char x = getchar();
  while (x < '0' || x > '9') x = getchar();
  while (x >= '0' && x <= '9') {
    k = k * 10 - 48 + x;
    x = getchar();
  }
}
int n, m, vis[1620][1620], a[1620][1620], ind, ray[1620][1620], ans[1000000],
    tot;
void floodfill(int x, int y) {
  if (vis[x][y] || !a[x][y]) return;
  vis[x][y] = ind;
  for (int dir = 0; dir < 4; dir++) floodfill(x + dx[dir], y + dy[dir]);
}
void ff2(int x, int y) {
  if (!ray[x][y]) return;
  ray[x][y] = 0;
  tot++;
  for (int dir = 0; dir < 4; dir++) ff2(x + dx[dir], y + dy[dir]);
}
int main() {
  read(n);
  read(m);
  for (int i = 4; i <= n + 3; i++)
    for (int j = 4; j <= m + 3; j++) read(a[i][j]);
  for (int i = 4; i <= n + 3; i++)
    for (int j = 4; j <= m + 3; j++)
      if (a[i][j] == 1) {
        if ((a[i][j - 1] == 0 || a[i][j - 2] == 0 || a[i][j - 3] == 0 ||
             a[i][j - 4] == 0) &&
            (a[i][j + 1] == 0 || a[i][j + 2] == 0 || a[i][j + 3] == 0 ||
             a[i][j + 4] == 0))
          ray[i][j] = 1;
        if ((a[i - 1][j] == 0 || a[i - 2][j] == 0 || a[i - 3][j] == 0 ||
             a[i - 4][j] == 0) &&
            (a[i + 1][j] == 0 || a[i + 2][j] == 0 || a[i + 3][j] == 0 ||
             a[i + 4][j] == 0))
          ray[i][j] = 1;
      }
  for (int i = 4; i <= n + 3; i++)
    for (int j = 4; j <= m + 3; j++)
      if (a[i][j] && !vis[i][j]) ++ind, floodfill(i, j);
  for (int i = 4; i <= n + 3; i++)
    for (int j = 4; j <= m + 3; j++)
      if (ray[i][j]) {
        tot = 0;
        ff2(i, j);
        if (tot >= 10) ans[vis[i][j]]++;
      }
  sort(ans + 1, ans + ind + 1);
  printf("%d\n", ind);
  for (int i = 1; i <= ind; i++) printf("%d ", ans[i]);
  return 0;
}
