# Life is Hard

• I thought we had a square lake in the first place, my solution was:

``````    int islandPerimeter(vector<vector<int>>& grid) {
int ret = 0, side_len = grid.size();
for(int i=0; i<side_len; ++i) {
int a = 0, b = 0;
for(int j=0; j<side_len; ++j) {
if(grid[i][j] != a)  ++ret;
a = grid[i][j];
if(grid[j][i] != b)  ++ret;
b = grid[j][i];
}
if(a)   ++ret;
if(b)   ++ret;
}
return ret;
}
``````

Of course it did not passed the test case, then i noticed the sad word "rectangular". So here is my second solution:

``````    int islandPerimeter(vector<vector<int>>& grid) {
int ret = 0, side1 = grid.size(), side2;
if(!side1)  return 0;
side2 = grid[0].size();
for(int i=0, b = 0, my_sum = 0; i<side1; ++i) {
int a = 0;
for(int j=0; j<side2; ++j) {
if(grid[i][j] != a) ++ret;
a = grid[i][j];
int ni = (my_sum + j)%side1, nj = (my_sum + j)/side1;
if(grid[ni][nj] != b)   ++ret;
if(ni == side1 - 1) {
if(grid[ni][nj] == 1)   ++ret;
b = 0;
} else {
b = grid[ni][nj];
}
}
if(a)   ++ret;
my_sum += side2;
}
return ret;
}
``````

Well, it is a pity that the lake is not square because I really like my first solution.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.