When the county jail or any building contiguous thereto is on fire and there is reason to apprehend that the prisoners may be thereby injured or endangered, the sheriff or jailer may remove them to a safe and convenient place and confine them there so long as may be necessary to avoid the danger.