read while read h w N do echo ${_[h*h+w%2>N*N&w*w+h%2>N*N]-S} done