Extending @aes 's solution by simulation, my computations suggest you in fact always stay when within 6 of a perfect square, for a large enough square.

First, let's cast this as a dynamic programming problem:

$V\left(n\right)=\begin{cases}
0 & \hspace{1em}\exists k\in\mathbb{N} s.t. k^{2}=n\\
\max\left\{ n,\frac{1}{6}\sum\limits_{m=1}^{6}V\left(n+m\right)\right\} & \hspace{1em}\nexists k\in\mathbb{N} s.t. k^{2}=n
\end{cases}$

That is, your value conditional on having currently the cumulative sum $n$ is given by choosing to stay (taking $n$) or averaging over future values, unless $n$ is a perfect square.

To make this computable, consider a finite version of the problem, where we force
$V(n)=n$ for all $n$ past some threshold $N$; call this approximation $\tilde{V}\left(n;N\right)$.

$\tilde{V}\left(n;N\right)=\begin{cases}
0 & \hspace{1em}\exists k\in\mathbb{N} s.t k^{2}=n\\
\max\left\{ n,\frac{1}{6}\sum\limits_{m=1}^{6}\tilde{V}\left(n+m\right)\right\} & \hspace{1em}\nexists k\in\mathbb{N} s.t. k^{2}=n\cap n<N\\
n & \hspace{1em}\nexists k\in\mathbb{N} s.t. k^{2}=n\cap n\geq N
\end{cases}$

I approximated this with the following code in R (slow, I know):

```
value_functions<-c()
NNN<-10000
squares<-cumsum(seq(1,2*ceiling(sqrt(NNN+6))-1,by=2))
for (NN in ceiling(.8*NNN):NNN){
values<-0:(NNN+6)
values[squares+1]<-0
for (nn in (NN-1):0){
values[nn+1]<-ifelse(nn %in% squares,0,
max(nn,sum(values[(nn+2):(nn+7)])/6))
}
value_functions<-rbind(value_functions,values[0:NNN])
}
```

Here's a plot for $N=9990,\ldots,10000$, $n=1,\ldots,200$ (that we can't tell the plots for separate $N$ apart suggests convergence):

It may be hard to see, but basically what's going on is:

- at a perfect square, value is 0
- just after, we are secure for some time--and we'll roll for sure. because we know for sure that we'll roll many times, value doesn't increase (that money is guaranteed).
- at 6 before the next perfect square, or value plummets--now there's a risk of failure if we choose to continue.
- just prior to this, our value increases--we've eliminated some of the risk of failure by getting, say, 7 less than a square, which guarantees we'll be able to roll again risk-free.

Building on @aes 's finding, here are the counts at which we drop out, according to my simulation:

```
[1] 30 31 43 44 45 58 59 60 61 62 75 76 77 78 79 80 94 95 96 97 98 99 115 116 117 118 119 120 138 139 140 141 142 143 163 164 165 166
[39] 167 168 190 191 192 193 194 195 219 220 221 222 223 224 250 251 252 253 254 255 283 284 285 286 287 288 318 319 320 321 322 323 355 356 357 358 359 360
[77] 394 395 396 397 398 399 435 436 437 438 439 440 478 479 480 481 482 483 523 524 525 526 527 528 570 571 572 573 574 575 619 620 621 622 623 624 670 671
[115] 672 673 674 675 723 724 725 726 727 728 778 779 780 781 782 783 835 836 837 838 839 840 894 895 896 897 898 899 955 956 957 958 959 960 1018 1019 1020 1021
[153] 1022 1023 1083 1084 1085 1086 1087 1088 1150 1151 1152 1153 1154 1155 1219 1220 1221 1222 1223 1224 1290 1291 1292 1293 1294 1295 1363 1364 1365 1366 1367 1368 1438 1439 1440 1441 1442 1443
[191] 1515 1516 1517 1518 1519 1520 1594 1595 1596 1597 1598 1599 1675 1676 1677 1678 1679 1680 1758 1759 1760 1761 1762 1763 1843 1844 1845 1846 1847 1848 1930 1931 1932 1933 1934 1935 2019 2020
[229] 2021 2022 2023 2024 2110 2111 2112 2113 2114 2115 2203 2204 2205 2206 2207 2208 2298 2299 2300 2301 2302 2303 2395 2396 2397 2398 2399 2400 2494 2495 2496 2497 2498 2499 2595 2596 2597 2598
[267] 2599 2600 2698 2699 2700 2701 2702 2703 2803 2804 2805 2806 2807 2808 2910 2911 2912 2913 2914 2915 3019 3020 3021 3022 3023 3024 3130 3131 3132 3133 3134 3135 3243 3244 3245 3246 3247 3248
[305] 3358 3359 3360 3361 3362 3363 3475 3476 3477 3478 3479 3480 3594 3595 3596 3597 3598 3599 3715 3716 3717 3718 3719 3720 3838 3839 3840 3841 3842 3843 3963 3964 3965 3966 3967 3968 4090 4091
[343] 4092 4093 4094 4095 4219 4220 4221 4222 4223 4224 4350 4351 4352 4353 4354 4355 4483 4484 4485 4486 4487 4488 4618 4619 4620 4621 4622 4623 4755 4756 4757 4758 4759 4760 4894 4895 4896 4897
[381] 4898 4899 5035 5036 5037 5038 5039 5040 5178 5179 5180 5181 5182 5183 5323 5324 5325 5326 5327 5328 5470 5471 5472 5473 5474 5475 5619 5620 5621 5622 5623 5624 5770 5771 5772 5773 5774 5775
[419] 5923 5924 5925 5926 5927 5928 6078 6079 6080 6081 6082 6083 6235 6236 6237 6238 6239 6240 6394 6395 6396 6397 6398 6399 6555 6556 6557 6558 6559 6560 6718 6719 6720 6721 6722 6723 6883 6884
[457] 6885 6886 6887 6888 7050 7051 7052 7053 7054 7055 7219 7220 7221 7222 7223 7224 7390 7391 7392 7393 7394 7395 7563 7564 7565 7566 7567 7568 7738 7739 7740 7741 7742 7743 7915 7916 7917 7918
[495] 7919 7920 8094 8095 8096 8097 8098 8099 8275 8276 8277 8278 8279 8280 8458 8459 8460 8461 8462 8463 8643 8644 8645 8646 8647 8648 8830 8831 8832 8833 8834 8835 9019 9020 9021 9022 9023 9024
[533] 9210 9211 9212 9213 9214 9215 9403 9404 9405 9406 9407 9408 9598 9599 9600 9601 9602 9603 9795 9796 9797 9798 9799 9800 9994 9995 9996 9997 9998 9999
```

I'm still not fully satisfied that we've got a fully rigorous solution yet.

Basically, all that we've done so far ASSUMES a finite solution. My simulations are cruxed on that we can always find a stopping point; same for @aes. His upper bound assumes we're trading off $n^2-1$ for $(n+1)^2-1$, but, in the language of the problem as stated above, we're actually trading $n^2-1$ for $V\left(\left(n+1\right)^2-1\right)$. If this is itself infinite, the whole problem unravels.

That is, I'm still not convinced we've found an upper bound--once we prove an upper bound, the simulations by me, @aes and @Pakk all line up.