public int maxsq(int[][] arr, int x, int y) {
    if (maxs[x][y] != 0)
      return maxs[x][y];
    if (x >= arr.length - 1 || y >= arr[0].length - 1)
      return maxs[x][y] = 1;
    if (arr[x + 1][y] == 0 || arr[x + 1][y + 1] == 0 || arr[x][y + 1] == 0)
      return maxs[x][y] = 1;

    return maxs[x][y] = 1 + Math.min(Math.min(maxsq(arr, x, y + 1), maxsq(arr, x + 1, y + 1)),
        maxsq(arr, x + 1, y));
  }