#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:65000000")
using namespace std;
int tabl[3][1700][1700];
int n, m;
int c = 1;
void obx(int x, int y) {
  if (tabl[0][x][y] != 1) return;
  tabl[0][x][y] = c;
  obx(x + 1, y);
  obx(x - 1, y);
  obx(x, y - 1);
  obx(x, y + 1);
}
void dil(int x, int y) {
  bool b = 1;
  for (int i = -2; i <= 2; ++i)
    for (int j = -2; j <= 2; ++j)
      if (tabl[0][x + i][y + j] == 0) {
        b = 0;
        break;
      }
  tabl[1][x][y] = b * tabl[0][x][y];
}
void er(int x, int y) {
  int b = 0;
  for (int i = -2; i <= 2; ++i)
    for (int j = -2; j <= 2; ++j)
      if (tabl[1][x + i][y + j] != 0) {
        b = tabl[1][x + i][y + j];
        break;
      }
  tabl[2][x][y] = b;
  if (tabl[0][x][y] == tabl[2][x][y]) tabl[0][x][y] = 0;
}
int mas[1000000];
int cnt(int x, int y) {
  if (tabl[0][x][y] == 0) return 0;
  tabl[0][x][y] = 0;
  return 1 + cnt(x + 1, y) + cnt(x - 1, y) + cnt(x, y + 1) + cnt(x, y - 1);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) scanf("%d", &tabl[0][i + 50][j + 50]);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (tabl[0][i + 50][j + 50] == 1) {
        ++c;
        obx(i + 50, j + 50);
      }
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (tabl[0][i + 50][j + 50] != 0) dil(i + 50, j + 50);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) er(i + 50, j + 50);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (tabl[0][i + 50][j + 50] != 0) {
        int k = tabl[0][i + 50][j + 50];
        if (cnt(i + 50, j + 50) > 5) ++mas[k];
      }
  ++c;
  sort(mas + 2, mas + c);
  printf("%d\n", c - 2);
  for (int i = 2; i < c; ++i) printf("%d ", mas[i]);
  return 0;
}
