Explanation: I am using four matrix to keep track of max height in all directions...and if height of current point is less than min of all direction matrices..then I am adding the difference in total amount of water.

public int TrapRainWater(int[,] heightMap)

{

int r = heightMap.GetLength(0);

int c = heightMap.GetLength(1);

int[,] leftMap = new int[r, c];

int[,] rightMap = new int[r, c];

int[,] topMap = new int[r, c];

int[,] bottomMap = new int[r, c];

```
for (int i = 0; i < r; i++)
{
leftMap[i, 0] = heightMap[i, 0];
for (int j = 1; j < c; j++)
{
if (heightMap[i, j] < leftMap[i, j - 1]) leftMap[i, j] = leftMap[i, j - 1];
else leftMap[i, j] = heightMap[i, j];
}
}
for (int i = 0; i < r; i++)
{
rightMap[i, c - 1] = heightMap[i, c - 1];
for (int j = c - 2; j >= 0; j--)
{
if (heightMap[i, j] < rightMap[i, j + 1]) rightMap[i, j] = rightMap[i, j + 1];
else rightMap[i, j] = heightMap[i, j];
}
}
for (int j = 0; j < c; j++)
{
topMap[0, j] = heightMap[0, j];
for (int i = 1; i < r; i++)
{
if (heightMap[i, j] < topMap[i - 1, j]) topMap[i, j] = topMap[i - 1, j];
else topMap[i, j] = heightMap[i, j];
}
}
for (int j = 0; j < c; j++)
{
bottomMap[r - 1, j] = heightMap[r - 1, j];
for (int i = r - 2; i >= 0; i--)
{
if (heightMap[i, j] < bottomMap[i + 1, j]) bottomMap[i, j] = bottomMap[i + 1, j];
else bottomMap[i, j] = heightMap[i, j];
}
}
int totalAmount = 0;
for (int i = 1; i < r - 1; i++)
{
for (int j = 1; j < c - 1; j++)
{
int min = Math.Min(Math.Min(leftMap[i, j], rightMap[i, j]), Math.Min(topMap[i, j], bottomMap[i, j]));
if (min > heightMap[i, j])
totalAmount += min - heightMap[i, j];
}
}
return totalAmount;
}
```

Inputs: [[12,13,1,12],[13,4,13,12],[13,8,10,12],[12,13,12,12],[13,13,13,13]]

My output = 15;

Expected output =14;