#include <bits/stdc++.h>
using namespace std;
const int N = 1700;
int n, m, tim, res[N * N], num[N * N], a[N][N], s[N][N], id[N][N];
bool ray[N][N];
bool check(int x, int y) {
  return s[x + 3][y + 3] - s[x - 4][y + 3] - s[x + 3][y - 4] +
             s[x - 4][y - 4] <=
         15;
}
void dfs1(int x, int y) {
  if (id[x][y] || !a[x][y]) return;
  id[x][y] = *res;
  dfs1(x - 1, y);
  dfs1(x + 1, y);
  dfs1(x, y - 1);
  dfs1(x, y + 1);
}
void dfs2(int x, int y) {
  if (!a[x][y] || ray[x][y]) return;
  if (!check(x, y)) {
    if (num[id[x][y]] != tim) {
      num[id[x][y]] = tim;
      res[id[x][y]]++;
    }
    return;
  }
  ray[x][y] = true;
  dfs2(x - 1, y);
  dfs2(x + 1, y);
  dfs2(x, y - 1);
  dfs2(x, y + 1);
}
int main() {
  scanf("%d%d", &n, &m);
  n += 5;
  m += 5;
  for (int i = 6; i <= n; i++)
    for (int j = 6; j <= m; j++) scanf("%d", &a[i][j]);
  n += 5;
  m += 5;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (a[i][j] && !id[i][j]) {
        ++*res;
        dfs1(i, j);
      }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (a[i][j] && !ray[i][j] && check(i, j)) {
        ++tim;
        dfs2(i, j);
      }
  sort(res + 1, res + *res + 1);
  printf("%d\n", *res);
  for (int i = 1; i <= *res; i++) printf("%d ", res[i]);
}
