#include <bits/stdc++.h>
using namespace std;
const int N = 1624;
int n, m;
int vst[N][N], calc[N][N];
int in[N][N];
int na, ans[N * N];
int fr, bk;
pair<int, int> que[N * N];
int px[4] = {0, 0, 1, -1};
int py[4] = {1, -1, 0, 0};
inline void BFS(int stx, int sty) {
  fr = bk = 0;
  que[bk++] = make_pair(stx, sty);
  vst[stx][sty] = ++na;
  while (fr < bk) {
    int x = que[fr].first;
    int y = que[fr].second;
    fr++;
    for (int i = 0; i < 4; i++) {
      int nx = x + px[i];
      int ny = y + py[i];
      if (!vst[nx][ny] && in[nx][ny]) {
        vst[nx][ny] = na;
        que[bk++] = make_pair(nx, ny);
      }
    }
  }
}
int main(int argc, char** argv) {
  scanf("%d %d", &n, &m);
  getchar();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      in[i][j] = getchar() - '0';
      getchar();
    }
  }
  na = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (!vst[i][j] && in[i][j]) {
        BFS(i, j);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (in[i][j] && in[i + 1][j] && in[i + 2][j] && !calc[i][j] &&
          !calc[i + 1][j] && !calc[i + 2][j]) {
        int cnt = 0;
        for (int k = -1; k < 2; k += 2) {
          cnt = in[i - 1][j] + in[i + 3][j];
          cnt += in[i][j + k] + in[i + 1][j + k] + in[i + 2][j + k];
          if (cnt) continue;
          cnt += in[i - 1][j - k] + in[i + 3][j - k];
          if (!cnt)
            ans[vst[i][j]]++;
          else {
            if (cnt == 2) continue;
            ans[vst[i][j]]++;
          }
          calc[i][j]++;
          calc[i + 1][j]++;
          calc[i + 2][j]++;
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (in[i][j] && in[i][j + 1] && in[i][j + 2] && !calc[i][j] &&
          !calc[i][j + 1] && !calc[i][j + 2]) {
        int cnt = 0;
        for (int k = -1; k < 2; k += 2) {
          cnt = in[i][j - 1] + in[i][j + 3];
          cnt += in[i + k][j] + in[i + k][j + 1] + in[i + k][j + 2];
          if (cnt) continue;
          cnt += in[i - k][j - 1] + in[i - k][j + 3];
          if (!cnt)
            ans[vst[i][j]]++;
          else {
            if (cnt == 2) continue;
            ans[vst[i][j]]++;
          }
          calc[i][j]++;
          calc[i][j + 1]++;
          calc[i][j + 2]++;
        }
      }
    }
  }
  printf("%d\n", na);
  sort(ans + 1, ans + 1 + na);
  for (int i = 1; i <= na; i++) printf("%d%c", ans[i], i == na ? '\n' : ' ');
  return 0;
}
