I think you could use nonzero() on the image and grab the min. and max. values as see here:
x = torch.zeros(24, 24)
x[3:7, 5:9] = 1.

idx = x.nonzero()
x_min = idx[:, 0].min()
x_max = idx[:, 0].max()
y_min = idx[:, 1].min()
y_max = idx[:, 1].max()