#include <bits/stdc++.h>
using namespace std;
const int S = 1600 + 11;
int n, m, vis[S][S], cnt, ans[S * S];
bool used[S][S], g[S][S], gg[S][S], t[S][S];
inline void dfs1(int x, int y) {
  vis[x][y] = cnt;
  if (g[x - 1][y] && !vis[x - 1][y]) dfs1(x - 1, y);
  if (g[x + 1][y] && !vis[x + 1][y]) dfs1(x + 1, y);
  if (g[x][y - 1] && !vis[x][y - 1]) dfs1(x, y - 1);
  if (g[x][y + 1] && !vis[x][y + 1]) dfs1(x, y + 1);
}
inline void dfs2(int x, int y) {
  used[x][y] = 1;
  if (g[x - 1][y - 1] && !used[x - 1][y - 1]) dfs2(x - 1, y - 1);
  if (g[x - 1][y] && !used[x - 1][y]) dfs2(x - 1, y);
  if (g[x - 1][y + 1] && !used[x - 1][y + 1]) dfs2(x - 1, y + 1);
  if (g[x][y - 1] && !used[x][y - 1]) dfs2(x, y - 1);
  if (g[x][y + 1] && !used[x][y + 1]) dfs2(x, y + 1);
  if (g[x + 1][y - 1] && !used[x + 1][y - 1]) dfs2(x + 1, y - 1);
  if (g[x + 1][y] && !used[x + 1][y]) dfs2(x + 1, y);
  if (g[x + 1][y + 1] && !used[x + 1][y + 1]) dfs2(x + 1, y + 1);
}
inline void color(bool tag) {
  memcpy(t, gg, sizeof gg);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      gg[i][j] =
          ((i > 1 && t[i - 1][j] == tag) || (i < n && t[i + 1][j] == tag) ||
           (j > 1 && t[i][j - 1] == tag) || (j < m && t[i][j + 1] == tag))
              ? tag
              : !tag;
}
int main() {
  char ch;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) scanf(" %c", &ch), g[i][j] = ch - '0';
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (g[i][j] && !vis[i][j]) ++cnt, dfs1(i, j);
  memcpy(gg, g, sizeof g);
  color(0);
  color(0);
  color(0);
  color(1);
  color(1);
  color(1);
  color(1);
  color(1);
  color(1);
  color(1);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) g[i][j] = (!gg[i][j] && g[i][j]);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (g[i][j] && !used[i][j]) ++ans[vis[i][j]], dfs2(i, j);
  sort(ans + 1, ans + cnt + 1);
  printf("%d\n", cnt);
  for (int i = 1; i <= cnt; ++i) printf("%d%c", ans[i], " \n"[i == cnt]);
  return 0;
}
