intsurfaceArea(vector<vector<int>> A){ int res = 0, n = A.size(), m = A[0].size(); int dy[4]{-1,0,1,0},dx[4]{0,1,0,-1}; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { res += 2 + 4 * A[i][j]; for(int k = 0; k < 4; k++) { int ny = i + dy[k], nx = j + dx[k]; if(0 <= ny and ny < n and0 <= nx and nx < m) { res -= min(A[i][j], A[ny][nx]); } } } } return res; }