#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:36777216")
const double Pi = acos(-1.0);
using namespace std;
const int maxn = 2000;
int numused;
int nulls = 0;
bool used[maxn][maxn];
int a[maxn][maxn], b[maxn][maxn], c[maxn][maxn];
int sum[maxn][maxn];
int ans[100000];
int h, w;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
void dfs(int i, int j, int cnt) {
  if (i < 0 || j < 0 || i >= h || j >= w) return;
  if (used[i][j]) return;
  if (!b[i][j]) return;
  used[i][j] = 1;
  c[i][j] = cnt;
  for (int k = 0; k < 4; k++) {
    dfs(i + dx[k], j + dy[k], cnt);
  }
}
bool added = 0;
void dfs2(int i, int j) {
  if (i < 0 || j < 0 || i >= h || j >= w) return;
  if (used[i][j]) return;
  if (!a[i][j]) return;
  if (c[i][j] && !added) {
    added = 1;
    ans[c[i][j]]++;
  }
  if (c[i][j]) return;
  used[i][j] = 1;
  for (int k = 0; k < 4; k++) {
    dfs2(i + dx[k], j + dy[k]);
  }
}
int gets(int x, int y) {
  if (x < 0 || y < 0) return 0;
  return sum[x][y];
}
int getsum(int x, int y, int xx, int yy) {
  if (x < 0) x = 0;
  if (y < 0) y = 0;
  if (xx >= h) xx = h - 1;
  if (yy >= w) yy = w - 1;
  return gets(xx, yy) - gets(x - 1, yy) - gets(xx, y - 1) + gets(x - 1, y - 1);
}
int getsq(int x, int y, int xx, int yy) {
  if (x < 0) x = 0;
  if (y < 0) y = 0;
  if (xx >= h) xx = h - 1;
  if (yy >= w) yy = w - 1;
  return (xx - x + 1) * (yy - y + 1);
}
int main() {
  scanf("%d %d", &h, &w);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char x;
      scanf(" %c", &x);
      b[i][j] = a[i][j] = (x == '1');
    }
  }
  sum[0][0] = a[0][0];
  for (int i = 1; i < h; i++) {
    sum[i][0] = sum[i - 1][0] + a[i][0];
  }
  for (int i = 1; i < w; i++) {
    sum[0][i] = sum[0][i - 1] + a[0][i];
  }
  for (int i = 1; i < h; i++) {
    for (int j = 1; j < w; j++) {
      sum[i][j] = a[i][j] + sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
    }
  }
  int ww = 2;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      b[i][j] = getsum(i - ww, j - ww, i + ww, j + ww) ==
                getsq(i - ww, j - ww, i + ww, j + ww);
    }
  }
  sum[0][0] = b[0][0];
  for (int i = 1; i < h; i++) {
    sum[i][0] = sum[i - 1][0] + b[i][0];
  }
  for (int i = 1; i < w; i++) {
    sum[0][i] = sum[0][i - 1] + b[0][i];
  }
  for (int i = 1; i < h; i++) {
    for (int j = 1; j < w; j++) {
      sum[i][j] = b[i][j] + sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
    }
  }
  ww++;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      b[i][j] = getsum(i - ww, j - ww, i + ww, j + ww) > 0;
    }
  }
  int cnt = 1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (b[i][j] && !used[i][j]) dfs(i, j, cnt++);
    }
  }
  int ss = 0;
  memset(used, 0, sizeof(used));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] && !c[i][j] && !used[i][j]) {
        ss++;
        added = 0;
        dfs2(i, j);
      } else {
      }
    }
  }
  sort(ans, ans + cnt);
  printf("%d\n", cnt - 1);
  for (int i = 1; i < cnt; i++) {
    printf("%d ", ans[i]);
  }
  printf("\n");
  return 0;
}
