Problem 1

Try block approach first

n=3; B=4*diag(ones(1,n))-diag(ones(1,n-1),1)-diag(ones(1,n-1),-1)
O=zeros(n,n)
mI=-eye(n,n)
It is easy to build the matrix for a small n. In our case n=3:
>> [B mI O;mI B O;O mI B]

ans =

     4    -1     0    -1     0     0     0     0     0
    -1     4    -1     0    -1     0     0     0     0
     0    -1     4     0     0    -1     0     0     0
    -1     0     0     4    -1     0     0     0     0
     0    -1     0    -1     4    -1     0     0     0
     0     0    -1     0    -1     4     0     0     0
     0     0     0    -1     0     0     4    -1     0
     0     0     0     0    -1     0    -1     4    -1
     0     0     0     0     0    -1     0    -1     4

Let's return to this approach later in the general case. Actually, just see here

Construction by diagonals

Let m be the nr. of subintervals
n=m-1 nr. of interior grid point in each axis
N=N^2 nr. of inner nodes on the 2-dim. grid = nr. of unknowns.

Matlab-session

m=4;n=m-1;N=n^2
maind=4*ones(N,1)
neardiag=-ones(N-1,1);
fardiag=-ones(N-n,1)

See the matrices separately

diag(maind)
diag(neardiag,1)
diag(neardiag,-1)
diag(fardiag,n)
diag(fardiag,-n)
Now just include +... to add the above matrices.
diag(maind)+...
diag(neardiag,1) +...
diag(neardiag,-1) + ...
diag(fardiag,n) +...
diag(fardiag,-n)
It's good, except you need to have 0 at each nth position of neardiag.

An elegant way is to form the vector [1,2,3, ...,N-1] ie. 1:N-1
Then form the remainder in division by n. Each nth gives a zero.

rem(1:N-1,n)
neardiag=rem(1:N-1,n)~=0  % Hence this is the right idiom, isn't it!
After this adjustment just repeat the above commands.
A=...
diag(maind)+...
diag(neardiag,1) +...
diag(neardiag,-1) + ...
diag(fardiag,n) +...
diag(fardiag,-n)
Note: The above idiom is my no means the only (elegant) solution, another idea would be like this:
 [ones(n-1,n);zeros(1,n)]  % 
 ans(:)            % columnwise "ravel" into a long vector
This was a bit more than was asked in exercise 1.