Translating a 3D grid into array index

Suppose I want to translate the following C routine into a CUDA kernel.

And, I want to use all the dimensions in the grid to run the kernel.

How can I calculate the indices of the row and column of the matrix?

void OuterProduct(float* A, float* B, float** C, int N)
{
    for(int r=0 ; r<N ; r++)
    {
        for(int c=0 ; c<N ; c++)
        {
            for(int cc=0 ; cc<N ; cc++)
            {
                (*C)[r * N + c] += A[r * N + cc] * B[cc * N + c];
            }
        }
    }
}

The following is my understanding:

__global__ void MultiplyMatKernel(I* A, I* B, I* C, int N)
{
    int dimx = N;
    int dimy = N;
    int dimz = N;

    int r = blockIdx.x * blockDim.x + threadIdx.x;
    int c = blockIdx.y * blockDim.y + threadIdx.y;
    int d = blockIdx.z * blockDim.z + threadIdx.z;

    if (r < N && c < N && d < N) 
	{
        int loc_c = d * dimx * dimy + c * dimx + r;
 
        for (int cc=0; cc<N; cc++) 
		{
		    int loc_a = (cc * dimx * dimy) + (c * dimx) + r;
		    int loc_b = (d * dimx * dimy) + (cc * dimx) + r;
                    C[loc_c] += A[loc_a]*B[loc_b];
        }
    }
}

I this correct? I think not.

Can you give me the correct rationale for calculating loc_a, loc_b, and loc_c?