I already tried that and it worked. The problem is that I don't know how to detect how many squares an object needs and whether it fits when there's an edge or not enough space; in other words, how to calculate the surrounding squares.
You don't need to detect how many squares an object needs, you should define the width and height ahead of time for the object, and use those values to check available space.
For your inventory array, cells outside the array bounds are always 0. So for empty spaces inside your array, use something like -1 while filled spaces could be set to 1. Then when checking array coordinates based on the width and height of whatever you're trying to fit, you'll be able to see if each target space an empty space (-1), filled space (1), or out of bounds (0). Only allow placement/confirmation if all checked spaces are empty.