Without knowing where fim comes from, I don't know. I'm going to guess that fim is another image imported the same way as the given image, or possibly derived from it. If that's the case, using double() instead of im2double() is probably going to cause you problems if you're looking at the image. Using double() will make the image floating-point, but it will still be in the range [0 255]. Tools like imshow() will display the image as solid white, because it expects floating point images to be normalized. Using im2double does both.
If that's not part of the problem, then again, i'll have to assume that there's some other issue with the missing array, since the following test has no such issues. Here's some simplification that might help:
[III fim] = meshgrid(1:10,1:5)
bfim = [fim(2,:); fim; fim(end-1,:)];
bfim = [bfim(:,2) bfim bfim(:,end-1)];
bfim(r+2, k+2) = fim(r,k);
A = bfim(i-1:i+1,j-1:j+1).';
If this is just going to turn into an averaging filter, the transpose is irrelevant:
III = imread('cameraman.tif');
bfim = [fim(2,:); fim; fim(end-1,:)];
bfim = [bfim(:,2) bfim bfim(:,end-1)];
bfim(r+2, k+2) = fim(r,k);
outpict(i,j) = sum(bfim(i-1:i+1,j-1:j+1),'all');
outpict = im2uint8(outpict);
If it's for something else, then ignore this.