It looks like you are using elementwise multiply in a couple of places where you should be using dot product style matrix multiply. E.g. the formula for dw in mathematical terms is this:
dw = \displaystyle \frac {1}{m} X \cdot (A - Y)^T
Here’s a thread about the notational conventions that Prof Ng uses for matrix multiplication.