Finding the perimeter of a river


Suppose you're given a matrix of 1s and 0s that represents a map of rivers. You can assume that the grid cells in your map are only connected horizontally and vertically (e.g. no diagonal connections). You can assume that 1 represents water (your river) and 0 represents land/your river bank. Each cell has a length of 1 and is square in your map. Given this, write code to determine the perimeter of your river.


Input: [[1,0]] 
Output: 4

Input: [[1,0,1],
Output: 12


Access restricted

Subscribe to premium account to see the solution.

Get premium now