With our getting into the correctness of the computation itself (I didn't check that) there is an obvious syntax error
You forgot using a Module or a Block
So the code should look like
gridresistor[p_, q_, ai_, aj_, bi_, bj_] :=
Module [{A, B, k, c, V}, A = ConstantArray[0, {p*q, p*q}];
...
]