int up[MAXN][MAXN]; //consecutive 1 above (i,j) (included) int pr[MAXN][MAXN]; //pre sum 1 in the left of (i,j) (included) char str[MAXN]; int top; ll ans;
for (int i = 1; i <= n; ++i) { cin >> (str + 1); for (int j = 1; j <= m; ++j) { up[i][j] = (str[j] == '1') ? (up[i - 1][j] + 1) : 0; pr[i][j] = pr[i][j - 1] + (str[j] - '0'); } }
for (int i = 1; i <= n; ++i) { top = 0; for (int j = 1, k; j <= m + 1; ++j) { k = j; while (top && stk[top].up >= up[i][j]) { k = stk[top].ls; if (stk[top].up > up[i][j]) { int l = stk[top].ls; int r = j - 1; if (pr[i + 1][r] - pr[i + 1][l - 1] != r - l + 1) { ans++; } } top--; } if (up[i][j])stk[++top] = {up[i][j], k}; } // while (top) { // int l = stk[top].ls; // int r = m; // if (pr[i + 1][r] - pr[i + 1][l - 1] != r - l + 1) { // ans++; // } // top--; // } } cout << ans << endl; return0; }