true_points = np.argwhere(dat)
top_left = true_points.min(axis=0)
bottom_right = true_points.max(axis=0)
out = dat[top_left[0]:bottom_right[0] + 1, top_left[1]:bottom_right[1] + 1]
How to crop zero edges of a numpy array?