
function f=backward_solve(s);  %defining function to solve matrix for solving lights out problem 
m=rows(s);  %assigns number of rows to m
n=columns(s);   %assigns number of columns to n
a=zeros(m*n,m*n);	%makes a zero matrix of size mn x mn
for c=1:m;
	for d=1:n;
		i=c; j=d;
		for i= max(i-1,1):min(i+1,m);		%used min and max to take care of boundary conditions
			a(j+((i-1)*n),((c-1)*n)+d)=1;	%assigns the given element of 'a' to 1
		endfor
		i=c; j=d;
		for j= max(j-1,1):min(j+1,n);		%used min and max to take care of boundary conditions
			a(j+((i-1)*n),((c-1)*n)+d)=1;
		endfor

	endfor
endfor
p=s';    %assigning adjoint of s to p
b=p(:)  ;  % b column matrix made to be made to add with Ax  
 w=det(a);     %modulus of a
 l=mod((w*(a\b)),2);
                             % r=@(y) mod((a*y +b),2) ;
                             % c=ones(m*n,1);
                             % l=fsolve(r,c)
v=round(l);	%rounding off the decimals to integers
mod(v,2);
r=zeros(m,n);
for j=1:m*n
	r(j)=v(j);
endfor
mod(r',2)

endfunction
