#include <bits/stdc++.h>
using namespace std;
char a[1600][1600];
char b[1600][1600];
int n, m;
int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};
inline bool ok(int first, int second) {
  return first >= 0 && first < n && second >= 0 && second < m;
}
bool cmp(pair<int, int> a, pair<int, int> b) {
  return a.first < b.first || a.first == b.first && a.second < b.second;
}
bool cw(pair<int, int> a, pair<int, int> b, pair<int, int> c) {
  return a.first * (b.second - c.second) + b.first * (c.second - a.second) +
             c.first * (a.second - b.second) <
         0;
}
bool ccw(pair<int, int> a, pair<int, int> b, pair<int, int> c) {
  return a.first * (b.second - c.second) + b.first * (c.second - a.second) +
             c.first * (a.second - b.second) >
         0;
}
int sh = 4;
inline int ngb(int first, int second) {
  int tot = 0;
  for (int i = first - sh; i <= first + sh; ++i)
    for (int j = second - sh; j <= second + sh; ++j)
      if (a[i][j] == 1) ++tot;
  return tot;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      scanf("%d", &a[i][j]);
      b[i][j] = 0;
    }
  }
  for (int i = sh; i < n - sh; ++i)
    for (int j = sh; j < m - sh; ++j)
      if (a[i][j] == 1 && ngb(i, j) == (2 * sh + 1) * (2 * sh + 1)) b[i][j] = 1;
  vector<int> ans(0);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (b[i][j] == 1) {
        queue<pair<int, int> > q;
        vector<pair<int, int> > allp;
        q.push(make_pair(i, j));
        allp.push_back(make_pair(i, j));
        a[i][j] = 2;
        while (!q.empty()) {
          pair<int, int> cur = q.front();
          q.pop();
          for (int w = 0; w < 8; ++w) {
            pair<int, int> nxt =
                make_pair(cur.first + dx[w], cur.second + dy[w]);
            if (!ok(nxt.first, nxt.second)) continue;
            if (a[nxt.first][nxt.second] != 1) continue;
            a[nxt.first][nxt.second] = 2;
            allp.push_back(nxt);
            q.push(nxt);
          }
        }
        vector<pair<int, int> > pts(0);
        q.push(make_pair(i, j));
        pts.push_back(make_pair(i, j));
        b[i][j] = 0;
        a[i][j] = 0;
        while (!q.empty()) {
          pair<int, int> cur = q.front();
          q.pop();
          for (int w = 0; w < 8; ++w) {
            pair<int, int> nxt =
                make_pair(cur.first + dx[w], cur.second + dy[w]);
            if (!ok(nxt.first, nxt.second)) continue;
            if (b[nxt.first][nxt.second] != 1) continue;
            b[nxt.first][nxt.second] = 0;
            a[nxt.first][nxt.second] = 0;
            pts.push_back(nxt);
            q.push(nxt);
          }
        }
        int si = 0, sj = 0;
        for (int k = 0; k < ((int)(pts).size()); ++k) {
          si += pts[k].first;
          sj += pts[k].second;
        }
        double cx = si / ((int)(pts).size());
        double cy = sj / ((int)(pts).size());
        double ddx = 0, ddy = 0;
        for (int k = 0; k < ((int)(pts).size()); ++k) {
          ddx = max(ddx, 1. * pts[k].first);
          ddy = max(ddy, 1. * pts[k].second);
        }
        ddx -= cx;
        ddy -= cy;
        double c = sqrt(abs(ddx * ddx - ddy * ddy));
        for (int k = 0; k < ((int)(pts).size()); ++k) {
          double vx = pts[k].first - cx;
          double vy = pts[k].second - cy;
          double vl = sqrt(vx * vx + vy * vy);
          vx /= vl;
          vy /= vl;
          for (int act = 1; act <= 7; ++act) {
            double px = pts[k].first + vx * act;
            double py = pts[k].second + vy * act;
            int sadd = 2;
            for (int sx = -sadd; sx <= sadd; ++sx)
              for (int sy = -sadd; sy <= sadd; ++sy)
                if (ok((int)px + sx, (int)py + sy) &&
                    a[(int)px + sx][(int)py + sy] == 2)
                  a[(int)px + sx][(int)py + sy] = 0;
          }
        }
        int rays = 0;
        for (int k = 0; k < ((int)(allp).size()); ++k) {
          if (a[allp[k].first][allp[k].second] == 2) {
            q.push(make_pair(allp[k].first, allp[k].second));
            a[allp[k].first][allp[k].second] = 0;
            ++rays;
            while (!q.empty()) {
              pair<int, int> cur = q.front();
              q.pop();
              for (int w = 0; w < 8; ++w) {
                pair<int, int> nxt =
                    make_pair(cur.first + dx[w], cur.second + dy[w]);
                if (!ok(nxt.first, nxt.second)) continue;
                if (a[nxt.first][nxt.second] != 2) continue;
                a[nxt.first][nxt.second] = 0;
                pts.push_back(nxt);
                q.push(nxt);
              }
            }
          }
        }
        ans.push_back(rays);
      }
    }
  }
  cout << ((int)(ans).size()) << endl;
  sort((ans).begin(), (ans).end());
  for (int i = 0; i < ((int)(ans).size()); ++i) {
    if (i) cout << " ";
    cout << ans[i];
  }
  cout << endl;
}
