Perimeter

View as PDF

Submit solution

Points: 100
Time limit: 1.0s
Python 2.0s
Memory limit: 256M

Author:
Problem type

Problem Statement

You have a square grid with length n. Each cell in the grid can be either 1 or 0.

Return the length of the perimeter required to surround all the 1 cells in the grid.

Input

Your first line will contain n.

Your next n lines will each contain n grids of 1's and 0s.

Output

A single integer representing the unit perimeter required to surround all the 1 cells in the grid.

Constraints

For all test cases:

  • 1 \le n \le 10^3
  • All cells will be either 1 or 0.

Example 1

Input
3
010
111
010
Output
12

Example 2

Input
4
0110
1110
0000
0110
Output
16

Example 3

Input
2
01
10
Output
8

Python Template

n = int(input())
grid = [list(map(int, input())) for _ in range(n)]

Comments

There are no comments at the moment.