This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const int N = 2005;
int n, m, h[N][N], le[N], ri[N];
long long dp[N][N];
char a[N][N];
vector < int > st;
int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            cin >> a[i][j];
        }
    }
    for(int i = 1; i <= m; i++){
        h[1][i] = (a[1][i] == '.');
    }
    for(int i = 2; i <= n; i++){
        for(int j = 1; j <= m; j++){
            h[i][j] = (a[i][j] == '#' ? 0 : h[i - 1][j] + 1);
        }
    }
    for(int i = 1; i <= n; i++){
        st.clear();
        for(int j = 1; j <= m; j++){
            while(!st.empty() && h[i][st.back()] > h[i][j]){
                st.pop_back();
            }
            if(st.empty()){
                le[j] = 0;
            }
            else{
                le[j] = st.back();
            }
            st.push_back(j);
        }
        st.clear();
        for(int j = m; j >= 1; j--){
            while(!st.empty() && h[i][st.back()] >= h[i][j]){
                st.pop_back();
            }
            if(st.empty()){
                ri[j] = m + 1;
            }
            else{
                ri[j] = st.back();
            }
            st.push_back(j);
        }
        for(int j = 1; j <= m; j++){
            dp[h[i][j]][ri[j] - j] += 1;
            dp[h[i][j]][j - le[j]] += 1;
            dp[h[i][j]][ri[j] - le[j]] -= 1;
            dp[h[i][j]][0] -= 1;
        }
    }
    for(int i = 1; i <= n + 1; i++){
        for(int j = 1; j <= m + 1; j++){
            dp[i][j] += dp[i][j - 1];
        }
    }
    for(int i = n; i >= 1; i--){
        for(int j = m; j >= 1; j--){
            dp[i][j] += dp[i + 1][j] + dp[i][j + 1] - dp[i + 1][j + 1];
        }
    }
    long long ans = 0;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            ans += 1LL * dp[i][j] * i * j;
        }
    }
    cout << ans;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |