#include <bits/stdc++.h>
using namespace std;
int h, w;
int v[1645][1645];
int was[1645][1645];
int was1[1645][1645];
vector<pair<int, int> > cur;
void dfs(int i, int j) {
  if (!was[i][j] && v[i][j]) {
    was[i][j] = 1;
    cur.push_back(pair<int, int>(i, j));
    for (int i1 = -1; i1 < 2; ++i1)
      for (int j1 = -1; j1 < 2; ++j1)
        if (i1 == 0 || j1 == 0) dfs(i + i1, j + j1);
  }
}
void dfs1(int i, int j) {
  if (!was1[i][j] && v[i][j]) {
    was1[i][j] = 1;
    for (int i1 = -1; i1 < 2; ++i1)
      for (int j1 = -1; j1 < 2; ++j1) dfs1(i + i1, j + j1);
  }
}
int f(int i, int j) {
  cur.resize(0);
  dfs(i, j);
  for (int it = 0; it < cur.size(); ++it) {
    int x = cur[it].first, y = cur[it].second;
    bool in = 1;
    for (int i = -3; i <= 3; ++i)
      for (int j = -3; j <= 3; ++j)
        if (!v[x + i][y + j]) in = 0;
    if (in) {
      for (int i = -5; i <= 5; ++i)
        for (int j = -5; j <= 5; ++j) was1[x + i][y + j] = 1;
    }
  }
  int res = 0;
  for (int i = 0; i < cur.size(); ++i)
    if (!was1[cur[i].first][cur[i].second]) {
      ++res;
      dfs1(cur[i].first, cur[i].second);
    }
  for (int i = 0; i < cur.size(); ++i) v[cur[i].first][cur[i].second] = 0;
  return res;
}
int main() {
  memset(v, 0, sizeof(v));
  memset(was, 0, sizeof(was));
  memset(was1, 0, sizeof(was1));
  cin >> h >> w;
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j) scanf("%d", &v[i + 5][j + 5]);
  h += 20;
  w += 20;
  vector<int> res;
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      if (v[i][j]) {
        res.push_back(f(i, j));
      }
  sort(res.begin(), res.end());
  cout << res.size() << endl;
  for (int i = 0; i < res.size(); ++i) cout << res[i] << ' ';
  cout << endl;
  return 0;
}
