In this task, you will implement a function calculate_brightness(img) that calculates the average brightness of a grayscale image. The image is represented as a 2D matrix, where each element represents a pixel value between 0 (black) and 255 (white).
Implement the function calculate_brightness(img) to:
For any of these edge cases, the function should return -1.