Read the entire line of input into a string, then parse that string. This way, you can check if the next item is a "$", and if not, you can re-try by trying to parse it as an integer to be stored in the current row of the matrix. You will also be able to determine how many items were provided for the current row of the matrix since you will eventually reach the end of the string: if at that point you still did not encounter a "$", then you know that you should continue to the next row.

You presumably also need to store the number or rows and columns of the matrix: it is alright to create a 2D array with plenty of space, but you might not use all of it. Then, you might want to check that each row was entered with the same number of items.