#include <bits/stdc++.h>
long map[1100][1100], en[1100][1100], bb[1100000][2], mark[1100][1100], n, m,
    b[1100000][3], c[1100], cc[1100];
int main() {
  long i, j, k, odd, num, ii, jj, u, l, r;
  scanf("%ld%ld", &n, &m);
  for (i = 0; i <= (n + 1); i++) {
    for (j = 0; j <= (m + 1); j++) {
      map[i][j] = 0;
      en[i][j] = 0;
      mark[i][j] = 0;
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      scanf("%ld", &map[i][j]);
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (map[i][j]) {
        break;
      }
    }
    if (map[i][j]) {
      break;
    }
  }
  l = 0;
  r = 0;
  bb[l][0] = i;
  bb[l][1] = j;
  mark[i][j] = 1;
  while (l <= r) {
    i = bb[l][0];
    j = bb[l][1];
    if (map[i + 1][j] && !mark[i + 1][j]) {
      r++;
      bb[r][0] = i + 1;
      bb[r][1] = j;
      mark[i + 1][j] = 1;
    }
    if (map[i - 1][j] && !mark[i - 1][j]) {
      r++;
      bb[r][0] = i - 1;
      bb[r][1] = j;
      mark[i - 1][j] = 1;
    }
    if (map[i][j + 1] && !mark[i][j + 1]) {
      r++;
      bb[r][0] = i;
      bb[r][1] = j + 1;
      mark[i][j + 1] = 1;
    }
    if (map[i][j - 1] && !mark[i][j - 1]) {
      r++;
      bb[r][0] = i;
      bb[r][1] = j - 1;
      mark[i][j - 1] = 1;
    }
    l++;
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      if (map[i][j] && !mark[i][j]) {
        printf("-1\n");
        return 0;
      }
    }
  }
  odd = 0;
  num = 0;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (map[i][j]) {
        if (map[i][j + 1] && map[i + 1][j] && map[i + 1][j + 1]) {
          printf("-1\n");
          return 0;
        } else {
          k = map[i - 1][j] + map[i][j - 1] + map[i][j + 1] + map[i + 1][j];
          if (k == 0) {
            printf("-1\n");
            return 0;
          }
          if ((k % 2) == 1) {
            odd += 1;
          }
          if (((map[i - 1][j] || map[i + 1][j]) &&
               (map[i][j - 1] || map[i][j + 1])) ||
              (k != 2)) {
            b[num][0] = i;
            b[num][1] = j;
            num++;
            en[i][j] = 1;
          }
        }
      }
    }
  }
  if (odd > 2) {
    printf("-1\n");
    return 0;
  }
  for (i = 0; i < 1100; i++) {
    c[i] = 0;
  }
  for (k = 0; k < num; k++) {
    i = b[k][0];
    j = b[k][1];
    if (map[i + 1][j]) {
      for (ii = i + 1, jj = 1; en[ii][j] == 0; ii++) {
        jj++;
      }
      c[jj] = 1;
    }
    if (map[i][j + 1]) {
      for (jj = j + 1, ii = 1; en[i][jj] == 0; jj++) {
        ii++;
      }
      c[ii] = 1;
    }
  }
  for (u = 0, i = 0; i < 1100; i++) {
    if (c[i]) {
      cc[u] = i;
      u++;
    }
  }
  for (i = 2, k = 0; i < 1100; i++) {
    ii = 0;
    for (j = 0; j < u; j++) {
      if ((cc[j] % i) != 0) {
        ii = 1;
        break;
      }
    }
    if (ii == 0) {
      if (k == 0) {
        k = 1;
      } else {
        printf(" ");
      }
      printf("%ld", i);
    }
  }
  if (k == 0) {
    printf("-1\n");
  } else {
    printf("\n");
  }
  return 0;
}
