#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
using ll = long long int;

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define SZ(x) ((int)(x).size())

#define FASTIO() cin.tie(0); ios::sync_with_stdio(false)

// chmax/min: if update a then true
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define ceil(a,b) ((a) + ((b) - 1)) / (b)

template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }

//////////////////////////////////////
#define dp(arg) if(KANAILOCAL) { cout << #arg << ":"; dprint(arg); }
template <typename T> void dprint(T arg) { cout << arg << "\n"; }
template <typename T> void dprint(const vector<T>& arg) { for_each(begin(arg), end(arg), [](T value){ cout << value << " "; }   ); cout << "\n";  }
template <typename T> void dprint(const vector<vector<T>>& arg) { for_each(begin(arg), end(arg), [=](vector<T> arg2){ dprint(arg2); cout << "\n";} );  }
template <typename T> void dprint(const unordered_set<T>& arg) { for_each(begin(arg), end(arg), [](T value){ cout << value << " "; }   ); cout << "\n";  }
template <typename T1, typename T2> void dprint(const unordered_map<T1,T2>& arg) { for_each(begin(arg), end(arg), [](auto & x){ cout << x.first << "_" << x.second << " "; }   ); cout << "\n";  }
//////////////////////////////////////
#define Yes std::cout << "Yes" << std::endl;
#define No std::cout << "No" << std::endl;
#define YES std::cout << "YES" << std::endl;
#define NO std::cout << "NO" << std::endl;
//////////////////////////////////////

void inputSample(){
#if 0
    string s; cin.ignore();
    getline(cin, s);
    int n,c;
    cin >> n >> c;
    int x; vector<int> dat(n); REP(i, n){cin << x; dat.at(i) = x;}
    if('a' <= s[i] && s[i] <= 'z'){}
    if('A' <= s[i] && s[i] <= 'Z'){}
#endif
}
template<typename T>
class SparseTable {
    vector<vector<T>> table;
    vector<int> lookup;
    function<T(T, T)> operation;

public:
    SparseTable(const vector<T> &v, function<T(T, T)> ope): operation(ope) {
        int log_len = 0;
        while((1 << log_len) <= (int)v.size()) ++log_len;
        table = vector<vector<T>>(log_len, vector<int>(1 << log_len));
        lookup = vector<int>((int)v.size() + 1);

        for(int i = 0; i < (int)v.size(); i++) {
            table[0][i] = v[i];
        }
        for(int i = 1; i < log_len; i++) {
            for(int j = 0; j + (1 << i) <= (1 << log_len); j++) {
                table[i][j] = operation(table[i-1][j], table[i - 1][j + (1 << (i - 1))]);
            }
        }
        for(int i = 2; i < (int)lookup.size(); i++) {
            lookup[i] = lookup[i >> 1] + 1;
        }
    }

    inline T query(int l, int r) {
        int b = lookup[r - l];
        return operation(table[b][l], table[b][r - (1 << b)]);
    }
};

using namespace std;
int main() {
    FASTIO();


    auto op = [](int a, int b) {return min(a,b);};

    int n; cin >> n;
    int x; vector<int> dat(n); REP(i, n){cin >> x; dat.at(i) = x;}
    int res = 0;

    SparseTable<int> st(dat, op);

    REP(i,n){
        FOR(j, i, n){
            res = max(res, st.query(i, j + 1) * (j-i+1));
        }

    }
    cout << res << "\n";

}

#include <bits/stdc++.h>
typedef long long       ll;
#define PI                acos(-1)
#define pb                push_back
#define mp                make_pair
#define mt                make_tuple
#define all(a)            (a).begin(), (a).end()
#define clr(a,h)          memset(a, (h), sizeof(a))
#define F first
#define S second
#define fore(i,b,e)		  for(int i=(int)b;i<(int)e;++i)
#define forr(i,b,e)		  for(int i=(int)b;i<(int)e;++i)
#define deb(x)			  cerr << "# " << (#x) << " = " << (x) << endl;
#define sz(x)             (int)x.size()
int faster_in(){int r=0,c;for(c=getchar();c<=32;c=getchar());if(c=='-') return -faster_in();for(;c>32;r=(r<<1)+(r<<3)+c-'0',c=getchar());return r;}

using namespace std;

typedef pair<int, int>  ii;
typedef vector<int>     vi;
typedef vector<ii>      vii;
typedef vector<ll>      vll;
const int INF = 1234567890;

const int tam = 2010;

char M[2010][2010];
int dist[2010][2010];

vii tele[26];
bool vis[26];

int main()
{
    std::ios::sync_with_stdio(false); cin.tie(0);
    //freopen("","r",stdin);
    //freopen("","w",stdout);
    int h, w;
    cin >> h >> w;
    fore(i, 0, h) fore(j, 0, w) cin >> M[i][j];
    int stR, stC;
    int enR, enC;
    fore(i, 0, h) fore(j, 0, w) {
        if (M[i][j] == 'S') stR = i, stC = j;
        if (M[i][j] == 'G') enR = i, enC = j;
        char c = M[i][j];
        if ('a' <= c && c <= 'z') {
            tele[c-'a'].pb({i, j});
        }
        dist[i][j] = INF;
    }
    deque<ii> Q;
    dist[stR][stC] = 0;
    Q.push_back({stR, stC});
    int dy[4] = {1,-1,0,0};
    int dx[4] = {0,0,1,-1};
    while (!Q.empty()) {
        int y = Q.front().F;
        int x = Q.front().S;
        Q.pop_front();
        char c = M[y][x];
        if ('a' <= c && c <= 'z' && !vis[c-'a']) {
            vis[c-'a'] = true;
            for (ii u : tele[c-'a']) {
                int a = u.F;
                int b = u.S;
                if (dist[a][b] > dist[y][x] + 1) {
                    dist[a][b] = dist[y][x] + 1;
                    Q.push_back({a, b});
                }
            }
        }
        fore(i, 0, 4) {
            int a = y + dy[i];
            int b = x + dx[i];
            if (a < 0 || b < 0 || a >= h || b >= w) continue;
            if (M[a][b] == '#') continue;
            if (dist[a][b] <= dist[y][x] + 1) continue;
            dist[a][b] = dist[y][x] + 1;
            Q.push_back({a,b});
        }
    }
    cout << (dist[enR][enC] == INF ? -1 : dist[enR][enC]) << '\n';
    return 0;
}
// Dinosaurs are cool!