First of all, please use 3x` encapsulation to make your code copyable, Ref
Please also make sure that the complete code is added (including definitions of your image).
And finally if you scale your mesh with l
, then this has to be taken into account when computing i and j