[Community Puzzle] Rectangle Partition

I am really stuck . I can`t for the life in me make this algorithm more optimal . this is my code :

string[] inputs;
            inputs = Console.ReadLine().Split(' ');
            int w = int.Parse(inputs[0]);
            int h = int.Parse(inputs[1]);
            int countX = int.Parse(inputs[2]);
            int countY = int.Parse(inputs[3]);
            List<int> listX = new List<int>();
            List<int> listY = new List<int>();
            int[] inputsX = new int[countX + 1];
            int[] inputsY = new int[countY + 1];
            inputs = Console.ReadLine().Split(' ');
            for (int i = 0; i < countX; i++)
            {
                inputsX[i] = int.Parse(inputs[i]);
            }
            inputs = Console.ReadLine().Split(' ');
            for (int i = 0; i < countY; i++)
            {
                inputsY[i] = int.Parse(inputs[i]);
            }
            inputsX[inputsX.Length - 1] = w;
            inputsY[inputsY.Length - 1] = h;

            for (int i = inputsX.Length - 1; i >= 0; i--)
            {
                int x = i;
                listX.Add(inputsX[i]);
                while (x > 0)
                {
                    
                    listX.Add(inputsX[i] - inputsX[x - 1]);
                    x--; ;
                }

            }
            for (int i = inputsY.Length - 1; i >= 0; i--)
            {
                int x = i;
                listY.Add(inputsY[i]);
                while (x > 0)
                {
                   
                    listY.Add(inputsY[i] - inputsY[x - 1]);
                    x--; ;
                }

            }
            int contor = 0;
            var grouped = listX.GroupBy(x=> x);
            foreach( var i in grouped){
                contor = contor + listY.Count(c => c == i.Key) * i.Count();
            }
            

            Console.WriteLine(contor);