1504.cpp (745B)
1 class Solution { 2 public: 3 int numSubmat(vector<vector<int>> &mat) const { 4 const int n = size(mat), m = size(mat[0]); 5 6 for (int j = 0; j < m; j++) { 7 for (int i = 0, acc = 0; i < n; i++) { 8 if (mat[i][j]) 9 mat[i][j] = acc += 1; 10 else 11 acc = 0; 12 } 13 } 14 15 int res = 0; 16 for (int i = 0; i < n; i++) { 17 for (int j = 0; j < m; j++) { 18 int mini = mat[i][j]; 19 for (int k = j; k >= 0; k--) { 20 mini = min(mini, mat[i][k]); 21 if (!mini) break; 22 res += mini; 23 } 24 } 25 } 26 return res; 27 } 28 };