Replace your for loop by:
tol = 10^-6; % Or whatever is appropriate
err = 1;
while err>tol
Told = T;
for i=2:nx-1
for j=2:ny-1
T(j,i)=((T(j-1,i)+T(j+1,i)+T(j,i-1)+T(j,i+1))/4);
end
end
err = max(max(abs(T - Told)));
end