316_F1. Suns and Rays_28625
Smart Beaver became interested in drawing. He draws suns. However, at some point, Smart Beaver realized that simply drawing suns is boring. So he decided to design a program that will process his drawings. You are given a picture drawn by the beaver. It will have two colors: one for the background and one for the suns in the image. Your task will be to count the number of suns in the image and for each of them to count the number of rays.

Sun is arbitrarily rotated ellipse with rays. Ray is a segment which connects point on boundary of the ellipse with some point outside ellipse.

<image> An image where all suns are circles.  <image> An image where all suns are ellipses, their axes are parallel to the coordinate axes.  <image> An image where all suns are rotated ellipses. 

It is guaranteed that: 

  * No two suns have common points. 
  * The rays’ width is 3 pixels. 
  * The lengths of the ellipsis suns’ axes will lie between 40 and 200 pixels. 
  * No two rays intersect. 
  * The lengths of all rays will lie between 10 and 30 pixels. 

Input

The first line contains two integers h and w — the height and width of the image (1 ≤ h, w ≤ 1600). Next h lines will contain w space-separated integers each. They describe Smart Beaver’s picture. Each number equals either a 0 (the image background), or a 1 (the sun color).

The input limits for scoring 30 points are (subproblem F1): 

  * All suns on the image are circles. 



The input limits for scoring 70 points are (subproblems F1+F2): 

  * All suns on the image are ellipses with axes parallel to the coordinate axes. 



The input limits for scoring 100 points are (subproblems F1+F2+F3):

  * All suns on the image are ellipses, they can be arbitrarily rotated. 

Output

The first line must contain a single number k — the number of suns on the beaver’s image. The second line must contain exactly k space-separated integers, corresponding to the number of rays on each sun. The numbers of the second line must be sorted in the increasing order.

Examples

Note

For each complexity level you are suggested a sample in the initial data. You can download the samples at http://www.abbyy.ru/sun.zip.
#include <bits/stdc++.h>
using namespace std;
long long rdtsc() {
  long long tmp;
  asm("rdtsc" : "=A"(tmp));
  return tmp;
}
inline int myrand() { return abs((rand() << 15) ^ rand()); }
inline int rnd(int x) { return myrand() % x; }
const int maxn = (int)1600 + 10;
int a[maxn][maxn];
int used[maxn][maxn];
int maxu;
const int maxg = 4;
const int go[maxg][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int cnt;
int st[maxn * maxn][2];
int h, w;
inline bool ok(int y, int x) { return 0 <= y && 0 <= x && y < h && x < w; }
void dfs(int y, int x) {
  used[y][x] = maxu;
  st[cnt][0] = y, st[cnt][1] = x, ++cnt;
  for (int g = 0; g < maxg; ++g) {
    int ny = y + go[g][0], nx = x + go[g][1];
    if (!ok(ny, nx) || used[ny][nx] || !a[ny][nx]) continue;
    dfs(ny, nx);
  }
}
const int K = 3;
bool check(int y, int x) {
  int col = used[y][x];
  for (int i1 = -K; i1 <= K; ++i1) {
    for (int i2 = -K; i2 <= K; ++i2) {
      if ((i1 * i1) + (i2 * i2) > K * K) continue;
      int ry = y, rx = x;
      ry += i1 * go[0][0], rx += i1 * go[0][1];
      ry += i2 * go[2][0], rx += i2 * go[2][1];
      if (!ok(ry, rx)) return 0;
      if (used[ry][rx] != col) return 0;
    }
  }
  return 1;
}
struct Point {
  double x, y;
  Point() {}
  Point(double _x, double _y) : x(_x), y(_y) {}
  inline Point operator-(const Point &p) const {
    return Point(x - p.x, y - p.y);
  }
  inline Point operator*(const double k) const { return Point(x * k, y * k); }
  inline double operator^(const Point &p) const { return x * p.y - y * p.x; }
  inline double operator*(const Point &p) const { return x * p.x + y * p.y; }
  inline double abs() const { return sqrt(x * x + y * y); }
};
double value[maxn][maxn];
const int K2 = 3;
bool solve() {
  if (scanf("%d%d", &h, &w) < 2) return 0;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      scanf("%d", &a[i][j]);
    }
  }
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      used[i][j] = 0;
    }
  }
  maxu = 0;
  vector<int> ans;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (used[i][j] || !a[i][j]) continue;
      ++maxu;
      cnt = 0;
      dfs(i, j);
      assert(cnt >= 500);
      int cool = 0;
      long long sumy = 0, sumx = 0;
      for (int i = 0; i < cnt; ++i) {
        if (!check(st[i][0], st[i][1])) continue;
        ++cool;
        sumy += st[i][0], sumx += st[i][1];
      }
      Point c(sumx, sumy);
      c = c * (1.0 / cool);
      int y = -1, x = -1;
      double maxval = -1;
      for (int i = 0; i < cnt; ++i) {
        if (!check(st[i][0], st[i][1])) continue;
        Point cur(st[i][1], st[i][0]);
        cur = cur - c;
        if (maxval < cur.abs()) {
          maxval = cur.abs(), y = st[i][0], x = st[i][1];
        }
      }
      double a = maxval;
      Point dir(x, y);
      dir = dir - c;
      dir = dir * (1.0 / a);
      maxval = -1;
      for (int i = 0; i < cnt; ++i) {
        if (!check(st[i][0], st[i][1])) continue;
        Point cur(st[i][1], st[i][0]);
        cur = cur - c;
        maxval = max(maxval, abs(dir ^ cur));
      }
      double b = maxval;
      a += K * 2, b += K * 2;
      fprintf(stderr, "a = %.3f, b = %.3f\n", a, b);
      fprintf(stderr, "center : (%.3f %.3f)\n", c.y, c.x);
      fprintf(stderr, "dir : (%.3f %.3f)\n", dir.y, dir.x);
      for (int i = 0; i < cnt; ++i) {
        Point cur(st[i][1], st[i][0]);
        cur = cur - c;
        Point real((cur * dir) / dir.abs(), (cur ^ dir) / dir.abs());
        double coef =
            ((real.x / a) * (real.x / a)) + ((real.y / b) * (real.y / b));
        double curValue = (real * max(0.0, (sqrt(coef) - 1.0))).abs();
        value[st[i][0]][st[i][1]] = curValue;
      }
      int res = 0;
      for (int i = 0; i < cnt; ++i) {
        int y = st[i][0], x = st[i][1];
        if (value[y][x] <= (1e-9)) continue;
        int col = used[y][x];
        bool fail = 0;
        for (int dy = -K2; !fail && dy <= K2; ++dy) {
          for (int dx = -K2; dx <= K2; ++dx) {
            int ny = y + dy, nx = x + dx;
            if (!ok(ny, nx) || used[ny][nx] != col) continue;
            if (value[ny][nx] > value[y][x] + (1e-9)) {
              fail = 1;
              break;
            }
          }
        }
        if (!fail) {
          fprintf(stderr, "%d %d %.3f\n", y, x, value[y][x]);
          value[y][x] = 1e18;
          ++res;
        }
      }
      ans.push_back(res);
    }
  }
  sort(ans.begin(), ans.end());
  printf("%d\n", ((int)(ans).size()));
  for (int i = 0; i < ((int)(ans).size()); ++i)
    printf("%d%c", ans[i], " \n"[i == ((int)(ans).size()) - 1]);
  return 1;
}
int main() {
  srand(rdtsc());
  while (1) {
    if (!solve()) break;
  }
  return 0;
}
