Probably the simplest approach once you have the image array is identifying the leftmost, rightmost, topmost and bottommost '1', perform edge-detection and then traverse the edge pixels between those 4 points. This should sort of work for an angled rectangle as is, but for one lined up with the array you'd need to also look for the NW, NE, SW, SE-most pixels as well, and see which works best.
If you have more processing power to throw at the problem you could try cross-correlation with suitable kernels, such as a rectangular corner, at all angles and positions.