private int neighbors(boolean[][] cells, int x, int y) {
    int x1 = (x>0)?x-1:x;
    int x2 = (x<n-1)?x+1:x;
    int y1 = (y>0)?y-1:y;
    int y2 = (y<n-1)?y+1:y;
    int count = 0;
    for (int i = x1; i <= x2; i++) {
      for (int j = y1; j <= y2; j++) {
        count += (cells[i][j])?1:0;
      }
    }
    if (cells[x][y]) count--;
    return count;
  }