#include <bits/stdc++.h>
using namespace std;
template <class _T>
inline _T sqr(const _T &x) {
  return x * x;
}
template <class _T>
inline string tostr(const _T &a) {
  ostringstream os("");
  os << a;
  return os.str();
}
const long double PI = 3.1415926535897932384626433832795;
const long double EPS = 1e-11;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, -1, 0, 1};
int n, m;
int a[2024][2024];
bool u[2024][2024];
int u2[2024][2024];
bool u3[2024][2024];
int d[2024][2024];
int q[2024 * 2024][2];
int e[1024000];
void bfs(int x, int y, signed long long &sx, signed long long &sy, int &s) {
  int qb = 0, qe = 0;
  s = 1;
  sx = x;
  sy = y;
  u[x][y] = true;
  q[qe][0] = x;
  q[qe][1] = y;
  qe++;
  while (qb != qe) {
    x = q[qb][0];
    y = q[qb][1];
    qb++;
    for (int l = 0; l < (int)(4); l++) {
      int xn = x + dx[l];
      int yn = y + dy[l];
      if (xn < 0 || xn >= n || yn < 0 || yn >= m || !a[xn][yn] || u[xn][yn])
        continue;
      u[xn][yn] = true;
      q[qe][0] = xn;
      q[qe][1] = yn;
      sx += xn;
      sy += yn;
      s++;
      qe++;
    }
  }
}
int bfs2(int x, int y, int col) {
  if (!a[x][y])
    for (;;)
      ;
  int qb = 0, qe = 0;
  u2[x][y] = col;
  d[x][y] = 0;
  q[qe][0] = x;
  q[qe][1] = y;
  qe++;
  while (qb != qe) {
    x = q[qb][0];
    y = q[qb][1];
    qb++;
    for (int l = 0; l < (int)(4); l++) {
      int xn = x + dx[l];
      int yn = y + dy[l];
      if (xn < 0 || xn >= n || yn < 0 || yn >= m || !a[xn][yn] || u2[xn][yn])
        continue;
      d[xn][yn] = d[x][y] + 1;
      u2[xn][yn] = col;
      q[qe][0] = xn;
      q[qe][1] = yn;
      qe++;
    }
  }
  return qe;
}
int main() {
  cout << setiosflags(ios::fixed) << setprecision(10);
  scanf("%d%d", &n, &m);
  for (int i = 0; i < (int)(n); i++) {
    for (int j = 0; j < (int)(m); j++) {
      scanf("%d", &a[i][j]);
    }
  }
  int cnt = 0;
  memset((u), 0, sizeof(u));
  memset((u2), 0, sizeof(u2));
  memset((u3), 0, sizeof(u3));
  for (int i = 0; i < (int)(n); i++) {
    for (int j = 0; j < (int)(m); j++) {
      if (a[i][j] && !u[i][j]) {
        signed long long xs, ys;
        int s;
        bfs(i, j, xs, ys, s);
        int t = bfs2(xs / s, ys / s, cnt + 1);
        int z = 0;
        for (int k = 0; k < (int)(t); k++) {
          int x = q[k][0];
          int y = q[k][1];
          bool bb = false;
          for (int l = 0; l < (int)(4); l++) {
            int xn = x + dx[l];
            int yn = y + dy[l];
            if (xn < 0 || xn >= n || yn < 0 || yn >= m || !a[xn][yn]) continue;
            if (d[xn][yn] > d[x][y]) bb = true;
          }
          if (!u3[x][y] && bb) {
            int w = 0;
            for (int di = (-4); di <= (int)(4); di++) {
              for (int dj = (-4); dj <= (int)(4); dj++) {
                int xn = x + di;
                int yn = y + dj;
                if (xn < 0 || xn >= n || yn < 0 || yn >= m || !a[xn][yn] ||
                    u2[xn][yn] != cnt + 1)
                  continue;
                w++;
              }
            }
            if (w < 27) {
              z++;
              for (int di = (-4); di <= (int)(4); di++) {
                for (int dj = (-4); dj <= (int)(4); dj++) {
                  int xn = x + di;
                  int yn = y + dj;
                  if (xn < 0 || xn >= n || yn < 0 || yn >= m || !a[xn][yn] ||
                      u3[xn][yn])
                    continue;
                  u3[xn][yn] = true;
                }
              }
            }
          }
        }
        e[cnt] = z;
        cnt++;
      }
    }
  }
  printf("%d\n", cnt);
  sort(e, e + cnt);
  for (int i = 0; i < (int)(cnt); i++) {
    if (i) putchar(' ');
    printf("%d", e[i]);
  }
  puts("");
  return 0;
}
