function solution = backward_solve(input)
	
	% number of rows, columns in input
	[rows, cols] = size(input);
	% no_buttons = number of buttons
	no_buttons = rows*cols;
	A = zeros(no_buttons, no_buttons);


	% numbering the buttons
	a = zeros(rows, cols);	
	for i = 1 : rows
		for j = 1 : cols
			a(i, j) = cols * (i - 1) + j;
		endfor
	endfor


	% A(i,j) = 1 if ith bulb is toggled with jth switch is pressed
	

	% initializing A

	for i = 1 : rows
		for j = 1 : cols
			A(a(i,j), a(i,j)) = 1;
			if (i + 1 <= rows)
				A(a(i,j), a(i + 1, j)) = 1;
			end
			if (i - 1 >= 1)
				A(a(i,j), a(i - 1, j)) = 1;
			end
			if (j + 1 <= cols)
				A(a(i,j), a(i, j + 1)) = 1;
			end
			if (j - 1 >= 1)
				A(a(i,j), a(i, j - 1)) = 1;
			end
		endfor
	endfor

	% vectorization of input as a column vector
	input_col_vec = reshape(input', no_buttons, 1);
	
	% ith index of row_of_x contains row_number of A which has (i - 1) zeros before the first one
	row_for_x = zeros(1,no_buttons);

	% Gaussian elimination of A
	for i = 1 : no_buttons
		target_rows = find_rows(A, i - 1);
		no_of_rows_to_be_added = size(target_rows)(2);
		if (no_of_rows_to_be_added >= 2)
			for j = 2 : no_of_rows_to_be_added
				A(target_rows(j), :) = mod((A(target_rows(j), :) + A(target_rows(1), :)), 2);
				input_col_vec(target_rows(j)) = input_col_vec(target_rows(j)) + input_col_vec(target_rows(1));
			endfor
		end
		if (no_of_rows_to_be_added >= 1)
			row_for_x(i) = target_rows(1);
		end
	endfor

	
	% x - vector form of solution
	x = zeros(no_buttons, 1);
	input_col_vec = mod(input_col_vec, 2);
	for i = no_buttons : -1 : 1
		if (row_for_x(i) != 0)
			x(i) = mod((input_col_vec(row_for_x(i)) + A(row_for_x(i),:)*x), 2);
		end
	endfor
	
	% solution	
	x = reshape(x, cols, rows)';
	solution = x;
endfunction
