For the purpose of promoting the health, safety, convenience, morals or welfare of its inhabitants, any town may, by a zoning ordinance or bylaw, regulate and restrict the height, number of stories, and size of buildings and structures, the size and width of lots, the percentages of lot that may be occupied, the size of yards, courts and other open spaces, the density of population, and the location and use of buildings, structures and land for trade, industry, agriculture, residence or other purposes; provided, however, that no ordinance or bylaw which prohibits or limits the use of land for any church or other religious purpose or for any educational purpose which is religious, sectarian, denominational or public shall be valid; and provided, further, that in regulating or restricting the size of such buildings or structures, no provision of any ordinance or bylaw shall be valid which requires the floor area of the living space of a single-family residential building to be greater than 768 square feet.