#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<set>
#include<map>
using namespace std;
int n, i, j, bl;
long long d,w, mnd, mnw, mxw, mxd, tmxd, tmnd, tmxw, tmnw, ans;
string s;
main(){
    cin >> n;
    for(i = 0; i<n; ++i){
        cin >> s;
        d=0;
        w=0;
        mnd=0;
        mxd=0;
        mnw=0;
        mxw=0;
        for(j = 0; j<s.size(); ++j){
            if (s[j] == 'D') ++d;
            else if(s[j] == 'A') --d;
            else if (s[j] == 'W') ++w;
            else if (s[j] == 'S') --w;
            mnd= min(mnd, d);
            mxd= max(mxd, d);

            mnw= min(mnw, w);
            mxw= max(mxw, w);
        }
        ans = (mxd-mnd+1)*(mxw-mnw+1);

        d = 0;
        w = 0;
        tmxd = 0;
        tmnd = 0;
        tmxw = 0;
        tmnw = 0;
        bl = 0;
        for(j = 0; j<s.size(); ++j){
            if (s[j] == 'D') ++d;
            else if(s[j] == 'A') --d;
            else if (s[j] == 'W') ++w;
            else if (s[j] == 'S') --w;
            if (bl ==0 && d == mxd){
                bl = 1;
                d-=2;
                tmnd= min(tmnd, d);
                ++d;
            }
            tmnd= min(tmnd, d);
            tmxd= max(tmxd, d);

            tmnw= min(tmnw, w);
            tmxw= max(tmxw, w);
        }
        ans = min(ans, (tmxd-tmnd+1)*(tmxw-tmnw+1));

        d = 0;
        w = 0;
        tmxd = 0;
        tmnd = 0;
        tmxw = 0;
        tmnw = 0;
        bl = 0;
        for(j = 0; j<s.size(); ++j){
            if (s[j] == 'D') ++d;
            else if(s[j] == 'A') --d;
            else if (s[j] == 'W') ++w;
            else if (s[j] == 'S') --w;
            if (bl ==0 && d == mnd){
                bl = 1;
                d+=2;
                tmxd= max(tmxd, d);
                --d;
            }
            tmnd= min(tmnd, d);
            tmxd= max(tmxd, d);

            tmnw= min(tmnw, w);
            tmxw= max(tmxw, w);
        }
        ans = min(ans, (tmxd-tmnd+1)*(tmxw-tmnw+1));

        d = 0;
        w = 0;
        tmxd = 0;
        tmnd = 0;
        tmxw = 0;
        tmnw = 0;
        bl = 0;
        for(j = 0; j<s.size(); ++j){
            if (s[j] == 'D') ++d;
            else if(s[j] == 'A') --d;
            else if (s[j] == 'W') ++w;
            else if (s[j] == 'S') --w;
            if (bl ==0 && w == mxw){
                bl = 1;
                w-=2;
                tmnw= min(tmnw, w);
                ++w;
            }
            tmnd= min(tmnd, d);
            tmxd= max(tmxd, d);

            tmnw= min(tmnw, w);
            tmxw= max(tmxw, w);
        }
        ans = min(ans, (tmxd-tmnd+1)*(tmxw-tmnw+1));

        d = 0;
        w = 0;
        tmxd = 0;
        tmnd = 0;
        tmxw = 0;
        tmnw = 0;
        bl = 0;
        for(j = 0; j<s.size(); ++j){
            if (s[j] == 'D') ++d;
            else if(s[j] == 'A') --d;
            else if (s[j] == 'W') ++w;
            else if (s[j] == 'S') --w;
            if (bl ==0 && w == mnw){
                bl = 1;
                w+=2;
                tmxw= min(tmxw, w);
                --w;
            }
            tmnd= min(tmnd, d);
            tmxd= max(tmxd, d);

            tmnw= min(tmnw, w);
            tmxw= max(tmxw, w);
        }
         ans = min(ans, (tmxd-tmnd+1)*(tmxw-tmnw+1));
         cout << ans<<"\n";
    }
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<set>
#include<map>
using namespace std;
int n, i, j, bl;
long long d,w, mnd, mnw, mxw, mxd, tmxd, tmnd, tmxw, tmnw, ans;
string s;
main(){
    cin >> n;
    for(i = 0; i<n; ++i){
        cin >> s;
        d=0;
        w=0;
        mnd=0;
        mxd=0;
        mnw=0;
        mxw=0;
        for(j = 0; j<s.size(); ++j){
            if (s[j] == 'D') ++d;
            else if(s[j] == 'A') --d;
            else if (s[j] == 'W') ++w;
            else if (s[j] == 'S') --w;
            mnd= min(mnd, d);
            mxd= max(mxd, d);

            mnw= min(mnw, w);
            mxw= max(mxw, w);
        }
        ans = (mxd-mnd+1)*(mxw-mnw+1);

        d = 0;
        w = 0;
        tmxd = 0;
        tmnd = 0;
        tmxw = 0;
        tmnw = 0;
        bl = 0;
        for(j = 0; j<s.size(); ++j){
            if (s[j] == 'D') ++d;
            else if(s[j] == 'A') --d;
            else if (s[j] == 'W') ++w;
            else if (s[j] == 'S') --w;
            if (bl ==0 && d == mxd){
                bl = 1;
                d-=2;
                tmnd= min(tmnd, d);
                ++d;
            }
            tmnd= min(tmnd, d);
            tmxd= max(tmxd, d);

            tmnw= min(tmnw, w);
            tmxw= max(tmxw, w);
        }
        ans = min(ans, (tmxd-tmnd+1)*(tmxw-tmnw+1));

        d = 0;
        w = 0;
        tmxd = 0;
        tmnd = 0;
        tmxw = 0;
        tmnw = 0;
        bl = 0;
        for(j = 0; j<s.size(); ++j){
            if (s[j] == 'D') ++d;
            else if(s[j] == 'A') --d;
            else if (s[j] == 'W') ++w;
            else if (s[j] == 'S') --w;
            if (bl ==0 && d == mnd){
                bl = 1;
                d+=2;
                tmxd= max(tmxd, d);
                --d;
            }
            tmnd= min(tmnd, d);
            tmxd= max(tmxd, d);

            tmnw= min(tmnw, w);
            tmxw= max(tmxw, w);
        }
        ans = min(ans, (tmxd-tmnd+1)*(tmxw-tmnw+1));

        d = 0;
        w = 0;
        tmxd = 0;
        tmnd = 0;
        tmxw = 0;
        tmnw = 0;
        bl = 0;
        for(j = 0; j<s.size(); ++j){
            if (s[j] == 'D') ++d;
            else if(s[j] == 'A') --d;
            else if (s[j] == 'W') ++w;
            else if (s[j] == 'S') --w;
            if (bl ==0 && w == mxw){
                bl = 1;
                w-=2;
                tmnw= min(tmnw, w);
                ++w;
            }
            tmnd= min(tmnd, d);
            tmxd= max(tmxd, d);

            tmnw= min(tmnw, w);
            tmxw= max(tmxw, w);
        }
        ans = min(ans, (tmxd-tmnd+1)*(tmxw-tmnw+1));

        d = 0;
        w = 0;
        tmxd = 0;
        tmnd = 0;
        tmxw = 0;
        tmnw = 0;
        bl = 0;
        for(j = 0; j<s.size(); ++j){
            if (s[j] == 'D') ++d;
            else if(s[j] == 'A') --d;
            else if (s[j] == 'W') ++w;
            else if (s[j] == 'S') --w;
            if (bl ==0 && w == mnw){
                bl = 1;
                w+=2;
                tmxw= max(tmxw, w);
                --w;
            }
            tmnd= min(tmnd, d);
            tmxd= max(tmxd, d);

            tmnw= min(tmnw, w);
            tmxw= max(tmxw, w);
        }
         ans = min(ans, (tmxd-tmnd+1)*(tmxw-tmnw+1));
         cout << ans<<"\n";
    }
}


#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

typedef long long ll;

int main() {

	//fstream cin("input.txt");
	//ofstream cout("output.txt");

	int t; cin >> t;
	while (t--) {
		string s; cin >> s;
		int n = s.size();
		vector<int> ymax(n), ymin(n), xmax(n), xmin(n);
		int cx = 0, cy = 0;
		for (int i = n; i>= 0; i--) {
			if (s[i] == 'W') {
				ymax[i] = i < n - 1 ? max(1, ymax[i + 1] + 1) : 1;
				ymin[i] = i < n - 1 ? ymin[i + 1] + 1 : 1;
				xmin[i] = i < n - 1 ? xmin[i + 1] : 0;
				xmax[i] = i < n - 1 ? xmax[i + 1] : 0;
			}
			if (s[i] == 'S') {
				ymin[i] = i < n - 1 ? min(-1, ymin[i + 1] - 1) : -1;
				ymax[i] = i < n - 1 ? ymax[i + 1] - 1 : -1;
				xmin[i] = i < n - 1 ? xmin[i + 1] : 0;
				xmax[i] = i < n - 1 ? xmax[i + 1] : 0;
			}
			if (s[i] == 'A') {
				ymin[i] = i < n - 1 ? ymin[i + 1] : 0;
				ymax[i] = i < n - 1 ? ymax[i + 1] : 0;
				xmin[i] = i < n - 1 ? xmax[i + 1] + 1 : 1;
				xmax[i] = i < n - 1 ? max(1, xmax[i + 1] + 1) : 1;
			}
			if (s[i] == 'D') {
				ymin[i] = i < n - 1 ? ymin[i + 1] : 0;
				ymax[i] = i < n - 1 ? ymax[i + 1] : 0;
				xmax[i] = i < n - 1 ? xmax[i + 1] - 1 : -1;
				xmin[i] = i < n - 1 ? min(-1, xmin[i + 1] - 1) : -1;
			}
		}
		int ans = (max(0, xmax[0]) - min(0, xmin[0]) + 1)*(max(0, ymax[0]) - min(0, ymin[0]) + 1), xb = 0, xs = 0, yb = 0, ys = 0;
		for (int i = 0; i < n; i++) {
			int tans1 = (max(cy + 1, max(yb, cy + 1 + ymax[i])) - min(ys, cy + 1 + ymin[i]) + 1)* (max(xb, cx + xmax[i]) - min(xs, cx + xmin[i]) + 1);
			ans = min(ans, tans1);
			tans1 = (max(yb, cy - 1 + ymax[i]) - min(cy - 1, min(ys, cy - 1  + ymin[i]) )+ 1)* (max(xb, cx + xmax[i]) - min(xs, cx + xmin[i]) + 1);
			ans = min(ans, tans1);
			tans1 = (max(yb, cy + ymax[i]) - min(ys, cy + ymin[i]) + 1)* (max(cx + 1, max(xb, cx + 1 + xmax[i])) - min(xs, cx + 1 + xmin[i]) + 1);
			ans = min(ans, tans1);
			tans1 = (max(yb, cy + ymax[i]) - min(ys, cy + ymin[i]) + 1)* (max(xb, cx - 1 + xmax[i]) - min(cx - 1, min(xs, cx - 1  + xmin[i])) + 1);
			ans = min(ans, tans1);
			if (s[i] == 'W') {
				cy++;
				yb = max(yb, cy);
			}
			if (s[i] == 'S') {
				cy--;
				ys = min(ys, cy);
			}
			if (s[i] == 'A') {
				cx++;
				xb = max(xb, cx);
			}
			if (s[i] == 'D') {
				cx--;
				xs = min(xs, cx);
			}
		}
		cout << ans << endl;

	}
	//vector<vector<ll>> answ(10, vector<ll>(10, 1e9));

}


#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

typedef long long ll;

int main() {

    //fstream cin("input.txt");
    //ofstream cout("output.txt");

    int t; cin >> t;
    while (t--) {
        string s; cin >> s;
        int n = s.size();
        vector<int> ymax(n), ymin(n), xmax(n), xmin(n);
        int cx = 0, cy = 0;
        for (int i = n; i >= 0; i--) {
            if (s[i] == 'W') {
                ymax[i] = i < n - 1 ? max(1, ymax[i + 1] + 1) : 1;
                ymin[i] = i < n - 1 ? ymin[i + 1] + 1 : 0;
                xmin[i] = i < n - 1 ? xmin[i + 1] : 0;
                xmax[i] = i < n - 1 ? xmax[i + 1] : 0;
            }
            if (s[i] == 'S') {
                ymin[i] = i < n - 1 ? min(-1, ymin[i + 1] - 1) : -1;
                ymax[i] = i < n - 1 ? ymax[i + 1] - 1 : 0;
                xmin[i] = i < n - 1 ? xmin[i + 1] : 0;
                xmax[i] = i < n - 1 ? xmax[i + 1] : 0;
            }
            if (s[i] == 'A') {
                ymin[i] = i < n - 1 ? ymin[i + 1] : 0;
                ymax[i] = i < n - 1 ? ymax[i + 1] : 0;
                xmin[i] = i < n - 1 ? xmin[i + 1] + 1 : 0;
                xmax[i] = i < n - 1 ? max(1, xmax[i + 1] + 1) : 1;
            }
            if (s[i] == 'D') {
                ymin[i] = i < n - 1 ? ymin[i + 1] : 0;
                ymax[i] = i < n - 1 ? ymax[i + 1] : 0;
                xmax[i] = i < n - 1 ? xmax[i + 1] - 1 : 0;
                xmin[i] = i < n - 1 ? min(-1, xmin[i + 1] - 1) : -1;
            }
        }
        int ans = (max(0, xmax[0]) - min(0, xmin[0]) + 1)*(max(0, ymax[0]) - min(0, ymin[0]) + 1), xb = 0, xs = 0, yb = 0, ys = 0;
        for (int i = 0; i < n; i++) {
            int tans1 = (max(cy + 1, max(yb, cy + 1 + ymax[i])) - min(ys, cy + 1 + ymin[i]) + 1)* (max(xb, cx + xmax[i]) - min(xs, cx + xmin[i]) + 1);
            ans = min(ans, tans1);
            tans1 = (max(yb, cy - 1 + ymax[i]) - min(cy - 1, min(ys, cy - 1 + ymin[i])) + 1)* (max(xb, cx + xmax[i]) - min(xs, cx + xmin[i]) + 1);
            ans = min(ans, tans1);
            tans1 = (max(yb, cy + ymax[i]) - min(ys, cy + ymin[i]) + 1)* (max(cx + 1, max(xb, cx + 1 + xmax[i])) - min(xs, cx + 1 + xmin[i]) + 1);
            ans = min(ans, tans1);
            tans1 = (max(yb, cy + ymax[i]) - min(ys, cy + ymin[i]) + 1)* (max(xb, cx - 1 + xmax[i]) - min(cx - 1, min(xs, cx - 1 + xmin[i])) + 1);
            ans = min(ans, tans1);
            if (s[i] == 'W') {
                cy++;
                yb = max(yb, cy);
            }
            if (s[i] == 'S') {
                cy--;
                ys = min(ys, cy);
            }
            if (s[i] == 'A') {
                cx++;
                xb = max(xb, cx);
            }
            if (s[i] == 'D') {
                cx--;
                xs = min(xs, cx);
            }
        }
        cout << ans << endl;

    }
    //vector<vector<ll>> answ(10, vector<ll>(10, 1e9));

}


#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

typedef long long ll;

vector <vector <ll>> g;
vector <bool> used;
ll a[10000], b[10000];
ll n;

int main() {

	//fstream cin("input.txt");
	//ofstream cout("output.txt");
	int t; cin >> t;
	while (t--) {
		string s; cin >> s;
		int cx = 0, cy = 0, ymax = 0, cntx = 0, ymin = 0, cntsx = 0,  xmax = 0, cnty = 0, xmin = 0, cntsy = 0;
		for (int i = 0; i < s.size(); i++) {
			if (s[i] == 'W') {
				cy++;
				ymax = max(cy, ymax);
				cnty++;
			}
			if (s[i] == 'S') {
				cy--;
				ymin = min(cy, ymin);
				cntsy++;
			}
			if (s[i] == 'A') {
				cx--;
				xmin = min(cx, xmin);
				cntx++;
			}
			if (s[i] == 'D') {
				cx++;
				xmax = max(xmax, cx);
				cntx++;
			}
		}
		int ans = (ymax - ymin + 1)*(xmax - xmin + 1);
		if (cnty - cntsy > 1) {
			ans = min(ans, (ymax - ymin)*(xmax - xmin + 1));
		}
		if (cntx - cntsx > 1) {
			ans = min(ans, (ymax - ymin + 1)*(xmax - xmin));
		}
		cout << ans << endl;
	}
	//vector<vector<ll>> answ(10, vector<ll>(10, 1e9));

}


#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

typedef long long ll;

int main() {

    //fstream cin("input.txt");
    //ofstream cout("output.txt");

    int t; cin >> t;
    while (t--) {
        string s; cin >> s;
        int n = s.size();
        vector<int> ymax(n), ymin(n), xmax(n), xmin(n);
        int cx = 0, cy = 0;
        for (int i = n; i >= 0; i--) {
            if (s[i] == 'W') {
                ymax[i] = i < n - 1 ? max(1, ymax[i + 1] + 1) : 1;
                ymin[i] = i < n - 1 ? ymin[i + 1] + 1 : 1;
                xmin[i] = i < n - 1 ? xmin[i + 1] : 0;
                xmax[i] = i < n - 1 ? xmax[i + 1] : 0;
            }
            if (s[i] == 'A') {
                ymin[i] = i < n - 1 ? min(-1, ymin[i + 1] - 1) : -1;
                ymax[i] = i < n - 1 ? ymax[i + 1] - 1 : -1;
                xmin[i] = i < n - 1 ? xmin[i + 1] : 0;
                xmax[i] = i < n - 1 ? xmax[i + 1] : 0;
            }
            if (s[i] == 'S') {
                ymin[i] = i < n - 1 ? ymin[i + 1] : 0;
                ymax[i] = i < n - 1 ? ymax[i + 1] : 0;
                xmin[i] = i < n - 1 ? xmax[i + 1] + 1 : 1;
                xmax[i] = i < n - 1 ? max(1, xmax[i + 1] + 1) : 1;
            }
            if (s[i] == 'D') {
                ymin[i] = i < n - 1 ? ymin[i + 1] : 0;
                ymax[i] = i < n - 1 ? ymax[i + 1] : 0;
                xmax[i] = i < n - 1 ? xmax[i + 1] - 1 : -1;
                xmin[i] = i < n - 1 ? min(-1, xmin[i + 1] - 1) : -1;
            }
        }
        int ans = (max(0, xmax[0]) - min(0, xmin[0]) + 1)*(max(0, ymax[0]) - min(0, ymin[0]) + 1), xb = 0, xs = 0, yb = 0, ys = 0;
        for (int i = 0; i < n; i++) {
            int tans1 = (max(cy + 1, max(yb, cy + 1 + ymax[i])) - min(ys, cy + 1 + ymin[i]) + 1)* (max(xb, cx + xmax[i]) - min(xs, cx + xmin[i]) + 1);
            ans = min(ans, tans1);
            tans1 = (max(yb, cy - 1 + ymax[i]) - min(cy - 1, min(ys, cy - 1 + ymin[i])) + 1)* (max(xb, cx + xmax[i]) - min(xs, cx + xmin[i]) + 1);
            ans = min(ans, tans1);
            tans1 = (max(yb, cy + ymax[i]) - min(ys, cy + ymin[i]) + 1)* (max(cx + 1, max(xb, cx + 1 + xmax[i])) - min(xs, cx + 1 + xmin[i]) + 1);
            ans = min(ans, tans1);
            tans1 = (max(yb, cy + ymax[i]) - min(ys, cy + ymin[i]) + 1)* (max(xb, cx - 1 + xmax[i]) - min(cx - 1, min(xs, cx - 1 + xmin[i])) + 1);
            ans = min(ans, tans1);
            if (s[i] == 'W') {
                cy++;
                yb = max(yb, cy);
            }
            if (s[i] == 'A') {
                cy--;
                ys = min(ys, cy);
            }
            if (s[i] == 'S') {
                cx++;
                xb = max(xb, cx);
            }
            if (s[i] == 'D') {
                cx--;
                xs = min(xs, cx);
            }
        }
        cout << ans << endl;

    }
    //vector<vector<ll>> answ(10, vector<ll>(10, 1e9));

}


#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

typedef long long ll;

int main() {

	fstream cin("input.txt");
	ofstream cout("output.txt");

	int t; cin >> t;
	while (t--) {
		string s; cin >> s;
		int n = s.size();
		vector<int> ymax(n), ymin(n), xmax(n), xmin(n);
		int cx = 0, cy = 0;
		for (int i = n; i >= 0; i--) {
			if (s[i] == 'W') {
				ymax[i] = i < n - 1 ? max(1, ymax[i + 1] + 1) : 1;
				ymin[i] = i < n - 1 ? min(1, ymin[i + 1] + 1) : 1;
				xmin[i] = i < n - 1 ? xmin[i + 1] : 0;
				xmax[i] = i < n - 1 ? xmax[i + 1] : 0;
			}
			if (s[i] == 'S') {
				ymin[i] = i < n - 1 ? min(-1, ymin[i + 1] - 1) : -1;
				ymax[i] = i < n - 1 ? max(-1, ymax[i + 1] - 1) : -1;
				xmin[i] = i < n - 1 ? xmin[i + 1] : 0;
				xmax[i] = i < n - 1 ? xmax[i + 1] : 0;
			}
			if (s[i] == 'A') {
				ymin[i] = i < n - 1 ? ymin[i + 1] : 0;
				ymax[i] = i < n - 1 ? ymax[i + 1] : 0;
				xmin[i] = i < n - 1 ? min(1, xmax[i + 1] + 1) : 1;
				xmax[i] = i < n - 1 ? max(1, xmax[i + 1] + 1) : 1;
			}
			if (s[i] == 'D') {
				ymin[i] = i < n - 1 ? ymin[i + 1] : 0;
				ymax[i] = i < n - 1 ? ymax[i + 1] : 0;
				xmax[i] = i < n - 1 ? max(-1, xmax[i + 1] - 1) : -1;
				xmin[i] = i < n - 1 ? min(-1, xmin[i + 1] - 1) : -1;
			}
		}
		int ans = (max(0, xmax[0]) - min(0, xmin[0]) + 1)*(max(0, ymax[0]) - min(0, ymin[0]) + 1), xb = 0, xs = 0, yb = 0, ys = 0;
		for (int i = 0; i < n; i++) {
			int tans1 = (max(cy + 1, max(yb, cy + 1 + ymax[i])) - min(ys, cy + 1 + ymin[i]) + 1)* (max(xb, cx + xmax[i]) - min(xs, cx + xmin[i]) + 1);
			ans = min(ans, tans1);
			tans1 = (max(yb, cy - 1 + ymax[i]) - min(cy - 1, min(ys, cy - 1 + ymin[i])) + 1)* (max(xb, cx + xmax[i]) - min(xs, cx + xmin[i]) + 1);
			ans = min(ans, tans1);
			tans1 = (max(yb, cy + ymax[i]) - min(ys, cy + ymin[i]) + 1)* (max(cx + 1, max(xb, cx + 1 + xmax[i])) - min(xs, cx + 1 + xmin[i]) + 1);
			ans = min(ans, tans1);
			tans1 = (max(yb, cy + ymax[i]) - min(ys, cy + ymin[i]) + 1)* (max(xb, cx - 1 + xmax[i]) - min(cx - 1, min(xs, cx - 1 + xmin[i])) + 1);
			ans = min(ans, tans1);
			if (s[i] == 'W') {
				cy++;
				yb = max(yb, cy);
			}
			if (s[i] == 'S') {
				cy--;
				ys = min(ys, cy);
			}
			if (s[i] == 'A') {
				cx++;
				xb = max(xb, cx);
			}
			if (s[i] == 'D') {
				cx--;
				xs = min(xs, cx);
			}
		}
		cout << ans << endl;

	}
	//vector<vector<ll>> answ(10, vector<ll>(10, 1e9));

}


#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>

using namespace std;

typedef long long ll;

int main() {

    //fstream cin("input.txt");
    //ofstream cout("output.txt");

    int t; cin >> t;
    while (t--) {
        string s; cin >> s;
        ll n = s.size();
        vector<ll> ymax(n), ymin(n), xmax(n), xmin(n);
        ll cx = 0, cy = 0;
        for (int i = n; i >= 0; i--) {
            if (s[i] == 'W') {
                ymax[i] = i < n - 1 ? max((ll)1, ymax[i + 1] + 1L) : 1;
                ymin[i] = i < n - 1 ? ymin[i + 1] + 1 : 0;
                xmin[i] = i < n - 1 ? xmin[i + 1] : 0;
                xmax[i] = i < n - 1 ? xmax[i + 1] : 0;
            }
            if (s[i] == 'S') {
                ymin[i] = i < n - 1 ? min((ll)-1, ymin[i + 1] - 1) : -1;
                ymax[i] = i < n - 1 ? ymax[i + 1] - 1 : 0;
                xmin[i] = i < n - 1 ? xmin[i + 1] : 0;
                xmax[i] = i < n - 1 ? xmax[i + 1] : 0;
            }
            if (s[i] == 'A') {
                ymin[i] = i < n - 1 ? ymin[i + 1] : 0;
                ymax[i] = i < n - 1 ? ymax[i + 1] : 0;
                xmin[i] = i < n - 1 ? xmin[i + 1] + 1 : 0;
                xmax[i] = i < n - 1 ? max((ll)1, xmax[i + 1] + 1) : 1;
            }
            if (s[i] == 'D') {
                ymin[i] = i < n - 1 ? ymin[i + 1] : 0;
                ymax[i] = i < n - 1 ? ymax[i + 1] : 0;
                xmax[i] = i < n - 1 ? xmax[i + 1] - 1 : 0;
                xmin[i] = i < n - 1 ? min((ll)-1, xmin[i + 1] - 1) : -1;
            }
        }
        ll ans = (max((ll)0, xmax[0]) - min((ll)0, xmin[0]) + 1)*(max((ll)0, ymax[0]) - min((ll)0, ymin[0]) + 1), xb = 0, xs = 0, yb = 0, ys = 0;
        for (int i = 0; i < n; i++) {
            ll tans1 = (max(cy + 1, max(yb, cy + 1 + ymax[i])) - min(ys, cy + 1 + ymin[i]) + 1)* (max(xb, cx + xmax[i]) - min(xs, cx + xmin[i]) + 1);
            ans = min(ans, tans1);
            tans1 = (max(yb, cy - 1 + ymax[i]) - min(cy - 1, min(ys, cy - 1 + ymin[i])) + 1)* (max(xb, cx + xmax[i]) - min(xs, cx + xmin[i]) + 1);
            ans = min(ans, tans1);
            tans1 = (max(yb, cy + ymax[i]) - min(ys, cy + ymin[i]) + 1)* (max(cx + 1, max(xb, cx + 1 + xmax[i])) - min(xs, cx + 1 + xmin[i]) + 1);
            ans = min(ans, tans1);
            tans1 = (max(yb, cy + ymax[i]) - min(ys, cy + ymin[i]) + 1)* (max(xb, cx - 1 + xmax[i]) - min(cx - 1, min(xs, cx - 1 + xmin[i])) + 1);
            ans = min(ans, tans1);
            if (s[i] == 'W') {
                cy++;
                yb = max(yb, cy);
            }
            if (s[i] == 'S') {
                cy--;
                ys = min(ys, cy);
            }
            if (s[i] == 'A') {
                cx++;
                xb = max(xb, cx);
            }
            if (s[i] == 'D') {
                cx--;
                xs = min(xs, cx);
            }
        }
        cout << ans << endl;

    }
    //vector<vector<ll>> answ(10, vector<ll>(10, 1e9));

}


#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

typedef long long ll;

int main() {

	fstream cin("input.txt");
	ofstream cout("output.txt");

	int t; cin >> t;
	while (t--) {
		string s; cin >> s;
		int n = s.size();
		vector<int> ymax(n), ymin(n), xmax(n), xmin(n);
		int cx = 0, cy = 0, ymax = 0, ymin = 0, xmax = 0, xmin = 0;
		for (int i = n; i>= 0; i--) {
			if (s[i] == 'W') {
				ymin[i] = i < n - 1 ? min(-1, ymin[i + 1] - 1) : -1;
				ymax[i] = i < n - 1 ? ymax[i + 1] - 1 : -1;
				xmin[i] = i < n - 1 ? xmin[i + 1] : 0;
				xmax[i] = i < n - 1 ? xmax[i + 1] : 0;
			}
			if (s[i] == 'S') {
				ymax[i] = i < n - 1 ? max(1, ymax[i + 1] + 1) : 1;
				ymin[i] = i < n - 1 ? ymin[i + 1] + 1 : 1;
				xmin[i] = i < n - 1 ? xmin[i + 1] : 0;
				xmax[i] = i < n - 1 ? xmax[i + 1] : 0;
			}
			if (s[i] == 'A') {
				ymin[i] = i < n - 1 ? ymin[i + 1] : 0;
				ymax[i] = i < n - 1 ? ymax[i + 1] : 0;
				xmax[i] = i < n - 1 ? xmax[i + 1] - 1 : -1;
				xmin[i] = i < n - 1 ? min(-1, xmin[i + 1] - 1) : -1;
			}
			if (s[i] == 'D') {
				ymin[i] = i < n - 1 ? ymin[i + 1] : 0;
				ymax[i] = i < n - 1 ? ymax[i + 1] : 0;
				xmin[i] = i < n - 1 ? xmax[i + 1] + 1 : 1;
				xmax[i] = i < n - 1 ? max(1, xmax[i + 1] + 1) : 1;
			}
		}
		int ans = 0, xb, xs, yb, ys;
		for (int i = 0; i < n; i++) {
			if (s[i] == 'W') {
				cy++;
			}
			if (s[i] == 'S') {
				cy--;
			}
			if (s[i] == 'A') {
				cx++;
			}
			if (s[i] == 'D') {
				cy++;
			}
		}
	}
	//vector<vector<ll>> answ(10, vector<ll>(10, 1e9));

}


#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

typedef long long ll;

vector <vector <ll>> g;
vector <bool> used;
ll a[10000], b[10000];
ll n;

int main() {

	//fstream cin("input.txt");
	//ofstream cout("output.txt");
	int t; cin >> t;
	while (t--) {
		string s; cin >> s;
		int cx = 0, cy = 0, ymax = 0, ymin = 0, xmax = 0, xmin = 0;
		for (int i = 0; i < s.size(); i++) {
			if (s[i] == 'W') {
				cy++;
				ymax = max(cy, ymax);
			}
			if (s[i] == 'S') {
				cy--;
				ymin = min(cy, ymin);
			}
			if (s[i] == 'A') {
				cx--;
				xmin = min(cx, xmin);
			}
			if (s[i] == 'D') {
				cx++;
				xmax = max(xmax, cx);
			}
		}
		int ans = (ymax - ymin + 1)*(xmax - xmin + 1);
		if (ymax - ymin + 1 > 2) {
			ans = min(ans, (ymax - ymin)*(xmax - xmin + 1));
		}
		if(xmax - xmin + 1 > 2){
			ans = min(ans, (ymax - ymin + 1)*(xmax - xmin));
		}
		cout << ans << endl;
	}
	//vector<vector<ll>> answ(10, vector<ll>(10, 1e9));

}


#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

typedef long long ll;

int main() {

	//fstream cin("input.txt");
	//ofstream cout("output.txt");

	int t; cin >> t;
	while (t--) {
		string s; cin >> s;
		int n = s.size();
		vector<int> ymax(n), ymin(n), xmax(n), xmin(n);
		int cx = 0, cy = 0;
		for (int i = n; i >= 0; i--) {
			if (s[i] == 'W') {
				ymax[i] = i < n - 1 ? max(1, ymax[i + 1] + 1) : 1;
				ymin[i] = i < n - 1 ? ymin[i + 1] + 1 : 1;
				xmin[i] = i < n - 1 ? xmin[i + 1] : 0;
				xmax[i] = i < n - 1 ? xmax[i + 1] : 0;
			}
			if (s[i] == 'S') {
				ymin[i] = i < n - 1 ? min(-1, ymin[i + 1] - 1) : -1;
				ymax[i] = i < n - 1 ? ymax[i + 1] - 1 : -1;
				xmin[i] = i < n - 1 ? xmin[i + 1] : 0;
				xmax[i] = i < n - 1 ? xmax[i + 1] : 0;
			}
			if (s[i] == 'A') {
				ymin[i] = i < n - 1 ? ymin[i + 1] : 0;
				ymax[i] = i < n - 1 ? ymax[i + 1] : 0;
				xmin[i] = i < n - 1 ? xmax[i + 1] + 1 : 1;
				xmax[i] = i < n - 1 ? max(1, xmax[i + 1] + 1) : 1;
			}
			if (s[i] == 'D') {
				ymin[i] = i < n - 1 ? ymin[i + 1] : 0;
				ymax[i] = i < n - 1 ? ymax[i + 1] : 0;
				xmax[i] = i < n - 1 ? xmax[i + 1] - 1 : -1;
				xmin[i] = i < n - 1 ? min(-1, xmin[i + 1] - 1) : -1;
			}
		}
		int ans = (max(0, xmax[0]) - min(0, xmin[0]) + 1)*(max(0, ymax[0]) - min(0, ymin[0]) + 1), xb = 0, xs = 0, yb = 0, ys = 0;
		for (int i = 0; i < n; i++) {
			int tans1 = (max(cy + 1, max(yb, cy + 1 + ymax[i])) - min(ys, cy + 1 + ymin[i]) + 1)* (max(xb, cx + xmax[i]) - min(xs, cx + xmin[i]) + 1);
			ans = min(ans, tans1);
			tans1 = (max(yb, cy - 1 + ymax[i]) - min(cy - 1, min(ys, cy - 1 + ymin[i])) + 1)* (max(xb, cx + xmax[i]) - min(xs, cx + xmin[i]) + 1);
			ans = min(ans, tans1);
			tans1 = (max(yb, cy + ymax[i]) - min(ys, cy + ymin[i]) + 1)* (max(cx + 1, max(xb, cx + 1 + xmax[i])) - min(xs, cx + 1 + xmin[i]) + 1);
			ans = min(ans, tans1);
			tans1 = (max(yb, cy + ymax[i]) - min(ys, cy + ymin[i]) + 1)* (max(xb, cx - 1 + xmax[i]) - min(cx - 1, min(xs, cx - 1 + xmin[i])) + 1);
			ans = min(ans, tans1);
			if (s[i] == 'W') {
				cy++;
				yb = max(yb, cy);
			}
			if (s[i] == 'S') {
				cy--;
				ys = min(ys, cy);
			}
			if (s[i] == 'A') {
				cx++;
				xb = max(xb, cx);
			}
			if (s[i] == 'D') {
				cx--;
				xs = min(xs, cx);
			}
		}
		cout << ans << endl;

	}
	//vector<vector<ll>> answ(10, vector<ll>(10, 1e9));

}


#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

typedef long long ll;

vector <vector <ll>> g;
vector <bool> used;
ll a[10000], b[10000];
ll n;

int main() {

	//fstream cin("input.txt");
	//ofstream cout("output.txt");
	int t; cin >> t;
	while (t--) {
		string s; cin >> s;
		int cx = 0, cy = 0, ymax = 0, ymin = 0, xmax = 0, xmin = 0;
		for (int i = 0; i < s.size(); i++) {
			if (s[i] == 'W') {
				cy++;
				ymax = max(cy, ymax);
			}
			if (s[i] == 'S') {
				cy--;
				ymin = min(cy, ymin);
			}
			if (s[i] == 'A') {
				cx--;
				xmin = min(cx, xmin);
			}
			if (s[i] == 'D') {
				cx++;
				xmax = max(xmax, cx);
			}
		}
		int ans = (ymax - ymin + 1)*(xmax - xmin + 1);
		if (ymax + ymin > 1) {
			ans = min(ans, (ymax - ymin)*(xmax - xmin + 1));
		}
		if(xmax + xmin > 1){
			ans = min(ans, (ymax - ymin + 1)*(xmax - xmin));
		}
		cout << ans << endl;
	}
	//vector<vector<ll>> answ(10, vector<ll>(10, 1e9));

}


#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

typedef long long ll;

int main() {

	//fstream cin("input.txt");
	//ofstream cout("output.txt");

	int t; cin >> t;
	while (t--) {
		string s; cin >> s;
		int n = s.size();
		vector<int> ymax(n), ymin(n), xmax(n), xmin(n);
		int cx = 0, cy = 0;
		for (int i = n; i >= 0; i--) {
			if (s[i] == 'W') {
				ymax[i] = i < n - 1 ? max(1, ymax[i + 1] + 1) : 1;
				ymin[i] = i < n - 1 ? min(1, ymin[i + 1] + 1) : 1;
				xmin[i] = i < n - 1 ? xmin[i + 1] : 0;
				xmax[i] = i < n - 1 ? xmax[i + 1] : 0;
			}
			if (s[i] == 'S') {
				ymin[i] = i < n - 1 ? min(-1, ymin[i + 1] - 1) : -1;
				ymax[i] = i < n - 1 ? max(-1, ymax[i + 1] - 1) : -1;
				xmin[i] = i < n - 1 ? xmin[i + 1] : 0;
				xmax[i] = i < n - 1 ? xmax[i + 1] : 0;
			}
			if (s[i] == 'A') {
				ymin[i] = i < n - 1 ? ymin[i + 1] : 0;
				ymax[i] = i < n - 1 ? ymax[i + 1] : 0;
				xmin[i] = i < n - 1 ? min(1, xmax[i + 1] + 1) : 1;
				xmax[i] = i < n - 1 ? max(1, xmax[i + 1] + 1) : 1;
			}
			if (s[i] == 'D') {
				ymin[i] = i < n - 1 ? ymin[i + 1] : 0;
				ymax[i] = i < n - 1 ? ymax[i + 1] : 0;
				xmax[i] = i < n - 1 ? max(-1, xmax[i + 1] - 1) : -1;
				xmin[i] = i < n - 1 ? min(-1, xmin[i + 1] - 1) : -1;
			}
		}
		int ans = (max(0, xmax[0]) - min(0, xmin[0]) + 1)*(max(0, ymax[0]) - min(0, ymin[0]) + 1), xb = 0, xs = 0, yb = 0, ys = 0;
		for (int i = 0; i < n; i++) {
			int tans1 = (max(cy + 1, max(yb, cy + 1 + ymax[i])) - min(ys, cy + 1 + ymin[i]) + 1)* (max(xb, cx + xmax[i]) - min(xs, cx + xmin[i]) + 1);
			ans = min(ans, tans1);
			tans1 = (max(yb, cy - 1 + ymax[i]) - min(cy - 1, min(ys, cy - 1 + ymin[i])) + 1)* (max(xb, cx + xmax[i]) - min(xs, cx + xmin[i]) + 1);
			ans = min(ans, tans1);
			tans1 = (max(yb, cy + ymax[i]) - min(ys, cy + ymin[i]) + 1)* (max(cx + 1, max(xb, cx + 1 + xmax[i])) - min(xs, cx + 1 + xmin[i]) + 1);
			ans = min(ans, tans1);
			tans1 = (max(yb, cy + ymax[i]) - min(ys, cy + ymin[i]) + 1)* (max(xb, cx - 1 + xmax[i]) - min(cx - 1, min(xs, cx - 1 + xmin[i])) + 1);
			ans = min(ans, tans1);
			if (s[i] == 'W') {
				cy++;
				yb = max(yb, cy);
			}
			if (s[i] == 'S') {
				cy--;
				ys = min(ys, cy);
			}
			if (s[i] == 'A') {
				cx++;
				xb = max(xb, cx);
			}
			if (s[i] == 'D') {
				cx--;
				xs = min(xs, cx);
			}
		}
		cout << ans << endl;

	}
	//vector<vector<ll>> answ(10, vector<ll>(10, 1e9));

}


#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define st first
#define nd second
#define all(x) x.begin(),x.end()
using namespace std;
const int MX = 1e5;
const int INF = 1e9;
int t,n;
int main(){
    cin >> t;
    while(t--){
        string s;
        cin >> s;
        n = s.length();
        vector<ll> h,v;
        h.push_back(0);
        v.push_back(0);
        for(int i=0 ; i<n ; ++i){
            if(s[i] == 'W')
                v.push_back(1);
            if(s[i] == 'S')
                v.push_back(-1);
            if(s[i] == 'A')
                h.push_back(-1);
            if(s[i] == 'D')
                h.push_back(1);
        }
        ll mxH=0,mnH=0,mnV=0,mxV=0;
        for(int i=1 ; i<h.size() ; ++i){
            h[i] += h[i-1];
            mxH = max(mxH,h[i]);
            mnH = min(mnH,h[i]);
        }
        for(int i=1 ; i<v.size() ; ++i){
            v[i] += v[i-1];
            mxV = max(mxV,v[i]);
            mnV = min(mnV,v[i]);
        }
        // for(auto el : h)
        //     cout << el << " ";
        // puts("");
        // for(auto el : v)
        //     cout << el << " ";
        // puts("");
        ll p = (mxH-mnH+1);
        ll q = (mxV-mnV+1);
        ll ans = (mxH-mnH+1) * (mxV-mnV+1);
        if(p <= 2 && q <= 2){
            cout << ans << "\n";
            continue;
        }
        int l=INF,r=-INF;
        for(int i=0 ; i<h.size() ; ++i){
            if(h[i] == mxH)
                l = i;
            if(h[n-i-1] == mnH)
                r = n-i-1;
        }
        if(l<r)
            ans = min(ans,(mxH-mnH)*(mxV-mnV+1));
        l=INF;r=-INF;
        for(int i=0 ; i<h.size() ; ++i){
            if(h[n-i-1] == mxH)
                r = n-i-1;
            if(h[i] == mnH)
                l = i;
        }
        if(l<r)
            ans = min(ans,(mxH-mnH)*(mxV-mnV+1));
        l=INF;r=-INF;
        for(int i=0 ; i<v.size() ; ++i){
            if(v[i] == mxV)
                l = i;
            if(h[n-i-1] == mnV)
                r = n-i-1;
        }
        if(l<r)
            ans = min(ans,(mxH-mnH+1)*(mxV-mnV));
        l=INF;r=-INF;
        for(int i=0 ; i<v.size() ; ++i){
            if(v[n-i-1] == mxV)
                r = n-i-1;
            if(v[i] == mnV)
                l = i;
        }
        if(l<r)
            ans = min(ans,(mxH-mnH+1)*(mxV-mnV));
        cout << ans << "\n";
    }
}


#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define st first
#define nd second
#define all(x) x.begin(),x.end()
using namespace std;
const int MX = 1e5;
const int INF = 1e9;
int t,n;
int main(){
    cin >> t;
    while(t--){
        string s;
        cin >> s;
        n = s.length();
        vector<ll> h,v;
        h.push_back(0);
        v.push_back(0);
        for(int i=0 ; i<n ; ++i){
            if(s[i] == 'W')
                v.push_back(1);
            if(s[i] == 'S')
                v.push_back(-1);
            if(s[i] == 'A')
                h.push_back(-1);
            if(s[i] == 'D')
                h.push_back(1);
        }
        ll mxH=0,mnH=0,mnV=0,mxV=0;
        for(int i=1 ; i<h.size() ; ++i){
            h[i] += h[i-1];
            mxH = max(mxH,h[i]);
            mnH = min(mnH,h[i]);
        }
        for(int i=1 ; i<v.size() ; ++i){
            v[i] += v[i-1];
            mxV = max(mxV,v[i]);
            mnV = min(mnV,v[i]);
        }
        // for(auto el : h)
        //     cout << el << " ";
        // puts("");
        // for(auto el : v)
        //     cout << el << " ";
        // puts("");
        ll p = (mxH-mnH+1);
        ll q = (mxV-mnV+1);
        ll ans = (mxH-mnH+1) * (mxV-mnV+1);
        if(p <= 2 && q <= 2){
            cout << ans << "\n";
            continue;
        }
        int l=INF,r=-INF;
        for(int i=0 ; i<h.size() ; ++i){
            if(h[i] == mxH)
                l = i;
            if(h[h.size()-i-1] == mnH)
                r = h.size()-i-1;
        }
        if(l<r)
            ans = min(ans,(mxH-mnH)*(mxV-mnV+1));
        l=INF;r=-INF;
        for(int i=0 ; i<h.size() ; ++i){
            if(h[h.size()-i-1] == mxH)
                r = h.size()-i-1;
            if(h[i] == mnH)
                l = i;
        }
        if(l<r)
            ans = min(ans,(mxH-mnH)*(mxV-mnV+1));
        l=INF;r=-INF;
        for(int i=0 ; i<v.size() ; ++i){
            if(v[i] == mxV)
                l = i;
            if(h[v.size()-i-1] == mnV)
                r = v.size()-i-1;
        }
        if(l<r)
            ans = min(ans,(mxH-mnH+1)*(mxV-mnV));
        l=INF;r=-INF;
        for(int i=0 ; i<v.size() ; ++i){
            if(v[v.size()-i-1] == mxV)
                r = v.size()-i-1;
            if(v[i] == mnV)
                l = i;
        }
        if(l<r)
            ans = min(ans,(mxH-mnH+1)*(mxV-mnV));
        cout << ans << "\n";
    }
}


#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define st first
#define nd second
#define all(x) x.begin(),x.end()
using namespace std;
const int MX = 1e5;
int t,n;
int main(){
    cin >> t;
    while(t--){
        string s;
        cin >> s;
        n = s.length();
        vector<int> h,v;
        for(int i=0 ; i<n ; ++i){
            if(s[i] == 'W')
                v.push_back(1);
            if(s[i] == 'S')
                v.push_back(-1);
            if(s[i] == 'A')
                h.push_back(-1);
            if(s[i] == 'D')
                h.push_back(1);
        }
        int mn=0,mx=0,x=0;
        for(auto el : v){
            x += el;
            mn = min(mn,x);
            mx = max(mx,x);
        }
        ll a = mx - mn;
        mn=0;mx=0,x=0;
        for(auto el : h){
            x += el;
            mn = min(mn,x);
            mx = max(mx,x);
        }
        ll b = mx - mn;
        if(b <= 1 && a <= 1)
            cout << ((a+1)*(b+1)) << "\n";
        else if (b <= 1)
            cout << (a*(b+1)) << "\n";
        else if (a <= 1)
            cout << ((a+1)*(b)) << "\n";
        else
            cout << min(a*(b+1),(a+1)*b) << "\n";
    }
}


#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define st first
#define nd second
#define all(x) x.begin(),x.end()
using namespace std;
const int MX = 1e5;
int t,n;
int main(){
    cin >> t;
    while(t--){
        string s;
        cin >> s;
        n = s.length();
        vector<int> h,v;
        for(int i=0 ; i<n ; ++i){
            if(s[i] == 'W')
                v.push_back(1);
            if(s[i] == 's')
                v.push_back(-1);
            if(s[i] == 'A')
                h.push_back(-1);
            if(s[i] == 'D')
                h.push_back(1);
        }
        int mn=0,mx=0,x=0;
        for(auto el : v){
            x += el;
            mn = min(mn,x);
            mx = max(mx,x);
        }
        ll a = mx - mn;
        mn=0;mx=0,x=0;
        for(auto el : h){
            x += el;
            mn = min(mn,x);
            mx = max(mx,x);
        }
        ll b = mx - mn;
        if(b <= 1 && a <= 1)
            cout << ((a+1)*(b+1)) << "\n";
        else if (b <= 1)
            cout << (a*(b+1)) << "\n";
        else if (a <= 1)
            cout << ((a+1)*(b)) << "\n";
        else
            cout << min(a*(b+1),(a+1)*b) << "\n";
    }
}


#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define st first
#define nd second
#define all(x) x.begin(),x.end()
using namespace std;
const int MX = 1e5;
const int INF = 1e9;
int t,n;
int main(){
    cin >> t;
    while(t--){
        string s;
        cin >> s;
        n = s.length();
        vector<ll> h,v;
        h.push_back(0);
        v.push_back(0);
        for(int i=0 ; i<n ; ++i){
            if(s[i] == 'W')
                v.push_back(1);
            if(s[i] == 'S')
                v.push_back(-1);
            if(s[i] == 'A')
                h.push_back(-1);
            if(s[i] == 'D')
                h.push_back(1);
        }
        ll mxH=0,mnH=0,mnV=0,mxV=0;
        for(int i=1 ; i<h.size() ; ++i){
            h[i] += h[i-1];
            mxH = max(mxH,h[i]);
            mnH = min(mnH,h[i]);
        }
        for(int i=1 ; i<v.size() ; ++i){
            v[i] += v[i-1];
            mxV = max(mxV,v[i]);
            mnV = min(mnV,v[i]);
        }
        // for(auto el : h)
        //     cout << el << " ";
        // puts("");
        // for(auto el : v)
        //     cout << el << " ";
        // puts("");
        ll p = (mxH-mnH+1);
        ll q = (mxV-mnV+1);
        ll ans = (mxH-mnH+1) * (mxV-mnV+1);
        if(p <= 2 && q <= 2){
            cout << ans << "\n";
            continue;
        }
        int l=INF,r=-INF;
        for(int i=0 ; i<h.size() ; ++i){
            if(h[i] == mxH)
                l = i;
            if(h[h.size()-i-1] == mnH)
                r = h.size()-i-1;
        }
        if(l<r && h.size() > 2)
            ans = min(ans,(mxH-mnH)*(mxV-mnV+1));
        l=INF;r=-INF;
        for(int i=0 ; i<h.size() ; ++i){
            if(h[h.size()-i-1] == mxH)
                r = h.size()-i-1;
            if(h[i] == mnH)
                l = i;
        }
        if(l<r && h.size() > 2)
            ans = min(ans,(mxH-mnH)*(mxV-mnV+1));
        l=INF;r=-INF;
        for(int i=0 ; i<v.size() ; ++i){
            if(v[i] == mxV)
                l = i;
            if(v[v.size()-i-1] == mnV)
                r = v.size()-i-1;
        }
        if(l<r && v.size() > 2)
            ans = min(ans,(mxH-mnH+1)*(mxV-mnV));
        l=INF;r=-INF;
        for(int i=0 ; i<v.size() ; ++i){
            if(v[v.size()-i-1] == mxV)
                r = v.size()-i-1;
            if(v[i] == mnV)
                l = i;
        }
        if(l<r && v.size() > 2)
            ans = min(ans,(mxH-mnH+1)*(mxV-mnV));
        cout << ans << "\n";
    }
}


#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define st first
#define nd second
#define all(x) x.begin(),x.end()
using namespace std;
const int MX = 1e5;
const int INF = 1e9;
int t,n;
int main(){
    cin >> t;
    while(t--){
        string s;
        cin >> s;
        n = s.length();
        vector<ll> h,v;
        h.push_back(0);
        v.push_back(0);
        for(int i=0 ; i<n ; ++i){
            if(s[i] == 'W')
                v.push_back(1);
            if(s[i] == 'S')
                v.push_back(-1);
            if(s[i] == 'A')
                h.push_back(-1);
            if(s[i] == 'D')
                h.push_back(1);
        }
        ll mxH=0,mnH=0,mnV=0,mxV=0;
        for(int i=1 ; i<h.size() ; ++i){
            h[i] += h[i-1];
            mxH = max(mxH,h[i]);
            mnH = min(mnH,h[i]);
        }
        for(int i=1 ; i<v.size() ; ++i){
            v[i] += v[i-1];
            mxV = max(mxV,v[i]);
            mnV = min(mnV,v[i]);
        }
        // for(auto el : h)
        //     cout << el << " ";
        // puts("");
        // for(auto el : v)
        //     cout << el << " ";
        // puts("");
        ll p = (mxH-mnH+1);
        ll q = (mxV-mnV+1);
        ll ans = (mxH-mnH+1) * (mxV-mnV+1);
        if(p <= 2 && q <= 2){
            cout << ans << "\n";
            continue;
        }
        int l=INF,r=-INF;
        for(int i=0 ; i<h.size() ; ++i){
            if(h[i] == mxH)
                l = i;
            if(h[h.size()-i-1] == mnH)
                r = h.size()-i-1;
        }
        if(l<r)
            ans = min(ans,(mxH-mnH)*(mxV-mnV+1));
        l=INF;r=-INF;
        for(int i=0 ; i<h.size() ; ++i){
            if(h[h.size()-i-1] == mxH)
                r = h.size()-i-1;
            if(h[i] == mnH)
                l = i;
        }
        if(l<r)
            ans = min(ans,(mxH-mnH)*(mxV-mnV+1));
        l=INF;r=-INF;
        for(int i=0 ; i<v.size() ; ++i){
            if(v[i] == mxV)
                l = i;
            if(v[v.size()-i-1] == mnV)
                r = v.size()-i-1;
        }
        if(l<r)
            ans = min(ans,(mxH-mnH+1)*(mxV-mnV));
        l=INF;r=-INF;
        for(int i=0 ; i<v.size() ; ++i){
            if(v[v.size()-i-1] == mxV)
                r = v.size()-i-1;
            if(v[i] == mnV)
                l = i;
        }
        if(l<r)
            ans = min(ans,(mxH-mnH+1)*(mxV-mnV));
        cout << ans << "\n";
    }
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma comment(linker, "/STACK:2700000000")
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<deque>
#include<bitset>
#include<sstream>
#include<numeric>
#include<random>
#include<functional>
#include<unordered_set>
#include<unordered_map>
#include<stack>
using namespace std;
typedef unsigned int ull;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef pair<ld, ld> pdd;
typedef pair<int, char> pic;
typedef pair<pii, ll> piii;
#define xx first
#define yy second
#define zz second.second
#define mp make_pair
#define y1 oshfkfsldhs
#define union jfghjdghdjhgjhdjgh
#define all(x) (x).begin(), (x).end()
const ll MOD = 998244353;
const ll MOD1 = 1e9 + 7;
const ll BAS = 257;
//const ll dy[] = { -1, 0, 1, 0 };
//const ll dx[] = { 0, 1, 0, -1 };
const ll N = 1.5e8;
const ll H = 22;
const double EPS = 1e-6;
const long double PI = 3.14159265358979323846;
const ll SZ = 300;
const ll INF = 1e15 + 213;
const ll MAXN = 1.5e6 + 10;
const ull D = 2100;

int n;
string s;

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cout.precision(26);
	srand(89398);
#ifdef _DEBUG
	auto _ = freopen("input.txt", "r", stdin);
	_ = freopen("output.txt", "w", stdout);
#else
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	//freopen("game.in", "r", stdin);
	//freopen("game.out", "w", stdout);
#endif
	cin >> n;
	while (n--)
	{
		cin >> s;
		int m = s.size();
		ll mnx = 0, mxx = 0, mny = 0, mxy = 0, curx = 0, cury = 0;
		ll lmnx = 0, lmxx = 0, lmny = 0, lmxy = 0;
		ll fmnx = m, fmxx = m, fmny = m, fmxy = m;
		for (auto c : s)
		{
			if (c == 'W')
			{
				cury++;
				mxy = max(mxy, cury);
				mny = min(mny, cury);
				mxx = max(mxx, curx);
				mnx = min(mnx, curx);
			}

			if (c == 'S')
			{
				cury--;
				mxy = max(mxy, cury);
				mny = min(mny, cury);
				mxx = max(mxx, curx);
				mnx = min(mnx, curx);
			}

			if (c == 'D')
			{
				curx++;
				mxy = max(mxy, cury);
				mny = min(mny, cury);
				mxx = max(mxx, curx);
				mnx = min(mnx, curx);
			}

			if (c == 'A')
			{
				curx--;
				mxy = max(mxy, cury);
				mny = min(mny, cury);
				mxx = max(mxx, curx);
				mnx = min(mnx, curx);
			}
		}
		curx = 0, cury = 0;
		if (mnx == 0)
			fmnx = 0;
		if (mny == 0)
			fmny = 0;
		if (mxx == 0)
			fmxx = 0;
		if (mxy == 0)
			fmxy = 0;
		for (ll i = 0; i < m; i++)
		{
			char c = s[i];
			if (c == 'W')
			{
				cury++;
				if (cury == mxy)
					lmxy = i, fmxy = min(fmxy, i);
				if (cury == mny)
					lmny = i, fmny = min(fmny, i);
				if (curx == mxx)
					lmxx = i, fmxx = min(fmxx, i);
				if (curx == mnx)
					lmnx = i, fmnx = min(fmnx, i);
			}

			if (c == 'S')
			{
				cury--;
				if (cury == mxy)
					lmxy = i, fmxy = min(fmxy, i);
				if (cury == mny)
					lmny = i, fmny = min(fmny, i);
				if (curx == mxx)
					lmxx = i, fmxx = min(fmxx, i);
				if (curx == mnx)
					lmnx = i, fmnx = min(fmnx, i);
			}

			if (c == 'D')
			{
				curx++;
				if (cury == mxy)
					lmxy = i, fmxy = min(fmxy, i);
				if (cury == mny)
					lmny = i, fmny = min(fmny, i);
				if (curx == mxx)
					lmxx = i, fmxx = min(fmxx, i);
				if (curx == mnx)
					lmnx = i, fmnx = min(fmnx, i);
			}

			if (c == 'A')
			{
				curx--;
				if (cury == mxy)
					lmxy = i, fmxy = min(fmxy, i);
				if (cury == mny)
					lmny = i, fmny = min(fmny, i);
				if (curx == mxx)
					lmxx = i, fmxx = min(fmxx, i);
				if (curx == mnx)
					lmnx = i, fmnx = min(fmnx, i);
			}
		}
		ll ans = (mxx - mnx + 1) * (mxy - mny + 1);
		if (abs(mnx - mxx) > 1 && (fmxx > lmnx || fmnx > lmxx))
			ans = min(ans, (mxx - mnx) * (mxy - mny + 1));
		if (abs(mny - mxy) > 1 && (fmxy > lmny || fmny > lmxy))
			ans = min(ans, (mxx - mnx + 1) * (mxy - mny));
		cout << ans << "\n";
	}
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma comment(linker, "/STACK:2700000000")
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<deque>
#include<bitset>
#include<sstream>
#include<numeric>
#include<random>
#include<functional>
#include<unordered_set>
#include<unordered_map>
#include<stack>
using namespace std;
typedef unsigned int ull;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef pair<ld, ld> pdd;
typedef pair<int, char> pic;
typedef pair<pii, ll> piii;
#define xx first
#define yy second
#define zz second.second
#define mp make_pair
#define y1 oshfkfsldhs
#define union jfghjdghdjhgjhdjgh
#define all(x) (x).begin(), (x).end()
const ll MOD = 998244353;
const ll MOD1 = 1e9 + 7;
const ll BAS = 257;
//const ll dy[] = { -1, 0, 1, 0 };
//const ll dx[] = { 0, 1, 0, -1 };
const ll N = 1.5e8;
const ll H = 22;
const double EPS = 1e-6;
const long double PI = 3.14159265358979323846;
const ll SZ = 300;
const ll INF = 1e15 + 213;
const ll MAXN = 1.5e6 + 10;
const ull D = 2100;

int n;
string s;

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cout.precision(26);
	srand(89398);
#ifdef _DEBUG
	auto _ = freopen("input.txt", "r", stdin);
	_ = freopen("output.txt", "w", stdout);
#else
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	//freopen("game.in", "r", stdin);
	//freopen("game.out", "w", stdout);
#endif
	cin >> n;
	while (n--)
	{
		cin >> s;
		int m = s.size();
		ll mnx = 0, mxx = 0, mny = 0, mxy = 0, curx = 0, cury = 0;
		ll lmnx = 0, lmxx = 0, lmny = 0, lmxy = 0;
		ll fmnx = m, fmxx = m, fmny = m, fmxy = m;
		for (auto c : s)
		{
			if (c == 'W')
			{
				cury++;
				mxy = max(mxy, cury);
			}

			if (c == 'S')
			{
				cury--;
				mny = min(mny, cury);
			}

			if (c == 'D')
			{
				curx++;
				mxx = max(mxx, curx);
			}

			if (c == 'A')
			{
				curx--;
				mnx = min(mnx, curx);
			}
		}
		for (ll i = 0; i < m; i++)
		{
			char c = s[i];
			if (c == 'W')
			{
				cury++;
				if (cury == mxy)
					lmxy = i, fmxy = min(fmxy, i);
				if (cury == mny)
					lmny = i, fmny = min(fmny, i);
				if (curx == mxx)
					lmxx = i, fmxx = min(fmxx, i);
				if (curx == mnx)
					lmnx = i, fmnx = min(fmnx, i);
			}

			if (c == 'S')
			{
				cury--;
				if (cury == mxy)
					lmxy = i, fmxy = min(fmxy, i);
				if (cury == mny)
					lmny = i, fmny = min(fmny, i);
				if (curx == mxx)
					lmxx = i, fmxx = min(fmxx, i);
				if (curx == mnx)
					lmnx = i, fmnx = min(fmnx, i);
			}

			if (c == 'D')
			{
				curx++;
				if (cury == mxy)
					lmxy = i, fmxy = min(fmxy, i);
				if (cury == mny)
					lmny = i, fmny = min(fmny, i);
				if (curx == mxx)
					lmxx = i, fmxx = min(fmxx, i);
				if (curx == mnx)
					lmnx = i, fmnx = min(fmnx, i);
			}

			if (c == 'A')
			{
				curx--;
				if (cury == mxy)
					lmxy = i, fmxy = min(fmxy, i);
				if (cury == mny)
					lmny = i, fmny = min(fmny, i);
				if (curx == mxx)
					lmxx = i, fmxx = min(fmxx, i);
				if (curx == mnx)
					lmnx = i, fmnx = min(fmnx, i);
			}
		}
		ll ans = (mxx - mnx + 1) * (mxy - mny + 1);
		if (abs(mnx - mxx) > 1 && (fmxx > lmnx || fmnx > lmxx))
			ans = min(ans, (mxx - mnx) * (mxy - mny + 1));
		if (abs(mny - mxy) > 1 && (fmxy > lmny || fmny > lmxy))
			ans = min(ans, (mxx - mnx + 1) * (mxy - mny));
		cout << ans  << "\n";
	}
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma comment(linker, "/STACK:2700000000")
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<deque>
#include<bitset>
#include<sstream>
#include<numeric>
#include<random>
#include<functional>
#include<unordered_set>
#include<unordered_map>
#include<stack>
using namespace std;
typedef unsigned int ull;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef pair<ld, ld> pdd;
typedef pair<int, char> pic;
typedef pair<pii, ll> piii;
#define xx first
#define yy second
#define zz second.second
#define mp make_pair
#define y1 oshfkfsldhs
#define union jfghjdghdjhgjhdjgh
#define all(x) (x).begin(), (x).end()
const ll MOD = 998244353;
const ll MOD1 = 1e9 + 7;
const ll BAS = 257;
//const ll dy[] = { -1, 0, 1, 0 };
//const ll dx[] = { 0, 1, 0, -1 };
const ll N = 1.5e8;
const ll H = 22;
const double EPS = 1e-6;
const long double PI = 3.14159265358979323846;
const ll SZ = 300;
const ll INF = 1e15 + 213;
const ll MAXN = 1.5e6 + 10;
const ull D = 2100;

int n;
string s;

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cout.precision(26);
	srand(89398);
#ifdef _DEBUG
	auto _ = freopen("input.txt", "r", stdin);
	_ = freopen("output.txt", "w", stdout);
#else
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	//freopen("game.in", "r", stdin);
	//freopen("game.out", "w", stdout);
#endif
	cin >> n;
	while (n--)
	{
		cin >> s;
		int m = s.size();
		ll mnx = 0, mxx = 0, mny = 0, mxy = 0, curx = 0, cury = 0;
		ll lmnx = 0, lmxx = 0, lmny = 0, lmxy = 0;
		for (auto c : s)
		{
			if (c == 'W')
			{
				cury++;
				mxy = max(mxy, cury);
			}

			if (c == 'S')
			{
				cury--;
				mny = min(mny, cury);
			}

			if (c == 'D')
			{
				curx++;
				mxx = max(mxx, curx);
			}

			if (c == 'A')
			{
				curx--;
				mnx = min(mnx, curx);
			}
		}
		ll ans = (mxx - mnx + 1) * (mxy - mny + 1);
		if (abs(mnx - mxx) > 1)
			ans = min(ans, (mxx - mnx) * (mxy - mny + 1));
		if (abs(mny - mxy) > 1)
			ans = min(ans, (mxx - mnx + 1) * (mxy - mny));
		cout << ans  << "\n";
	}
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma comment(linker, "/STACK:2700000000")
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<deque>
#include<bitset>
#include<sstream>
#include<numeric>
#include<random>
#include<functional>
#include<unordered_set>
#include<unordered_map>
#include<stack>
using namespace std;
typedef unsigned int ull;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef pair<ld, ld> pdd;
typedef pair<int, char> pic;
typedef pair<pii, ll> piii;
#define xx first
#define yy second
#define zz second.second
#define mp make_pair
#define y1 oshfkfsldhs
#define union jfghjdghdjhgjhdjgh
#define all(x) (x).begin(), (x).end()
const ll MOD = 998244353;
const ll MOD1 = 1e9 + 7;
const ll BAS = 257;
//const ll dy[] = { -1, 0, 1, 0 };
//const ll dx[] = { 0, 1, 0, -1 };
const ll N = 1.5e8;
const ll H = 22;
const double EPS = 1e-6;
const long double PI = 3.14159265358979323846;
const ll SZ = 300;
const ll INF = 1e15 + 213;
const ll MAXN = 1.5e6 + 10;
const ull D = 2100;

int n;
string s;

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cout.precision(26);
	srand(89398);
#ifdef _DEBUG
	auto _ = freopen("input.txt", "r", stdin);
	_ = freopen("output.txt", "w", stdout);
#else
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	//freopen("game.in", "r", stdin);
	//freopen("game.out", "w", stdout);
#endif
	cin >> n;
	while (n--)
	{
		cin >> s;
		int m = s.size();
		ll mnx = 0, mxx = 0, mny = 0, mxy = 0, curx = 0, cury = 0;
		ll lmnx = 0, lmxx = 0, lmny = 0, lmxy = 0;
		ll fmnx = m, fmxx = m, fmny = m, fmxy = m;
		for (auto c : s)
		{
			if (c == 'W')
			{
				cury++;
				mxy = max(mxy, cury);
				mny = min(mny, cury);
				mxx = max(mxx, curx);
				mnx = min(mnx, curx);
			}

			if (c == 'S')
			{
				cury--;
				mxy = max(mxy, cury);
				mny = min(mny, cury);
				mxx = max(mxx, curx);
				mnx = min(mnx, curx);
			}

			if (c == 'D')
			{
				curx++;
				mxy = max(mxy, cury);
				mny = min(mny, cury);
				mxx = max(mxx, curx);
				mnx = min(mnx, curx);
			}

			if (c == 'A')
			{
				curx--;
				mxy = max(mxy, cury);
				mny = min(mny, cury);
				mxx = max(mxx, curx);
				mnx = min(mnx, curx);
			}
		}
		curx = 0, cury = 0;
		for (ll i = 0; i < m; i++)
		{
			char c = s[i];
			if (c == 'W')
			{
				cury++;
				if (cury == mxy)
					lmxy = i, fmxy = min(fmxy, i);
				if (cury == mny)
					lmny = i, fmny = min(fmny, i);
				if (curx == mxx)
					lmxx = i, fmxx = min(fmxx, i);
				if (curx == mnx)
					lmnx = i, fmnx = min(fmnx, i);
			}

			if (c == 'S')
			{
				cury--;
				if (cury == mxy)
					lmxy = i, fmxy = min(fmxy, i);
				if (cury == mny)
					lmny = i, fmny = min(fmny, i);
				if (curx == mxx)
					lmxx = i, fmxx = min(fmxx, i);
				if (curx == mnx)
					lmnx = i, fmnx = min(fmnx, i);
			}

			if (c == 'D')
			{
				curx++;
				if (cury == mxy)
					lmxy = i, fmxy = min(fmxy, i);
				if (cury == mny)
					lmny = i, fmny = min(fmny, i);
				if (curx == mxx)
					lmxx = i, fmxx = min(fmxx, i);
				if (curx == mnx)
					lmnx = i, fmnx = min(fmnx, i);
			}

			if (c == 'A')
			{
				curx--;
				if (cury == mxy)
					lmxy = i, fmxy = min(fmxy, i);
				if (cury == mny)
					lmny = i, fmny = min(fmny, i);
				if (curx == mxx)
					lmxx = i, fmxx = min(fmxx, i);
				if (curx == mnx)
					lmnx = i, fmnx = min(fmnx, i);
			}
		}
		ll ans = (mxx - mnx + 1) * (mxy - mny + 1);
		if (abs(mnx - mxx) > 1 && (fmxx > lmnx || fmnx > lmxx))
			ans = min(ans, (mxx - mnx) * (mxy - mny + 1));
		if (abs(mny - mxy) > 1 && (fmxy > lmny || fmny > lmxy))
			ans = min(ans, (mxx - mnx + 1) * (mxy - mny));
		cout << ans  << "\n";
	}
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma comment(linker, "/STACK:2700000000")
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<deque>
#include<bitset>
#include<sstream>
#include<numeric>
#include<random>
#include<functional>
#include<unordered_set>
#include<unordered_map>
#include<stack>
using namespace std;
typedef unsigned int ull;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef pair<ld, ld> pdd;
typedef pair<int, char> pic;
typedef pair<pii, ll> piii;
#define xx first
#define yy second
#define zz second.second
#define mp make_pair
#define y1 oshfkfsldhs
#define union jfghjdghdjhgjhdjgh
#define all(x) (x).begin(), (x).end()
const ll MOD = 998244353;
const ll MOD1 = 1e9 + 7;
const ll BAS = 257;
//const ll dy[] = { -1, 0, 1, 0 };
//const ll dx[] = { 0, 1, 0, -1 };
const ll N = 1.5e8;
const ll H = 22;
const double EPS = 1e-6;
const long double PI = 3.14159265358979323846;
const ll SZ = 300;
const ll INF = 1e15 + 213;
const ll MAXN = 1.5e6 + 10;
const ull D = 2100;

int n;
string s;

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cout.precision(26);
	srand(89398);
#ifdef _DEBUG
	auto _ = freopen("input.txt", "r", stdin);
	_ = freopen("output.txt", "w", stdout);
#else
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	//freopen("game.in", "r", stdin);
	//freopen("game.out", "w", stdout);
#endif
	cin >> n;
	while (n--)
	{
		cin >> s;
		int m = s.size();
		ll mnx = 0, mxx = 0, mny = 0, mxy = 0, curx = 0, cury = 0;
		ll lmnx = 0, lmxx = 0, lmny = 0, lmxy = 0;
		ll fmnx = m, fmxx = m, fmny = m, fmxy = m;
		for (auto c : s)
		{
			if (c == 'W')
			{
				cury++;
				mxy = max(mxy, cury);
				mny = min(mny, cury);
				mxx = max(mxx, curx);
				mnx = min(mnx, curx);
			}

			if (c == 'S')
			{
				cury--;
				mxy = max(mxy, cury);
				mny = min(mny, cury);
				mxx = max(mxx, curx);
				mnx = min(mnx, curx);
			}

			if (c == 'D')
			{
				curx++;
				mxy = max(mxy, cury);
				mny = min(mny, cury);
				mxx = max(mxx, curx);
				mnx = min(mnx, curx);
			}

			if (c == 'A')
			{
				curx--;
				mxy = max(mxy, cury);
				mny = min(mny, cury);
				mxx = max(mxx, curx);
				mnx = min(mnx, curx);
			}
		}
		curx = 0, cury = 0;
		if (mnx == 0)
			fmnx = 0;
		if (mny == 0)
			fmny = 0;
		for (ll i = 0; i < m; i++)
		{
			char c = s[i];
			if (c == 'W')
			{
				cury++;
				if (cury == mxy)
					lmxy = i, fmxy = min(fmxy, i);
				if (cury == mny)
					lmny = i, fmny = min(fmny, i);
				if (curx == mxx)
					lmxx = i, fmxx = min(fmxx, i);
				if (curx == mnx)
					lmnx = i, fmnx = min(fmnx, i);
			}

			if (c == 'S')
			{
				cury--;
				if (cury == mxy)
					lmxy = i, fmxy = min(fmxy, i);
				if (cury == mny)
					lmny = i, fmny = min(fmny, i);
				if (curx == mxx)
					lmxx = i, fmxx = min(fmxx, i);
				if (curx == mnx)
					lmnx = i, fmnx = min(fmnx, i);
			}

			if (c == 'D')
			{
				curx++;
				if (cury == mxy)
					lmxy = i, fmxy = min(fmxy, i);
				if (cury == mny)
					lmny = i, fmny = min(fmny, i);
				if (curx == mxx)
					lmxx = i, fmxx = min(fmxx, i);
				if (curx == mnx)
					lmnx = i, fmnx = min(fmnx, i);
			}

			if (c == 'A')
			{
				curx--;
				if (cury == mxy)
					lmxy = i, fmxy = min(fmxy, i);
				if (cury == mny)
					lmny = i, fmny = min(fmny, i);
				if (curx == mxx)
					lmxx = i, fmxx = min(fmxx, i);
				if (curx == mnx)
					lmnx = i, fmnx = min(fmnx, i);
			}
		}
		ll ans = (mxx - mnx + 1) * (mxy - mny + 1);
		if (abs(mnx - mxx) > 1 && (fmxx > lmnx || fmnx > lmxx))
			ans = min(ans, (mxx - mnx) * (mxy - mny + 1));
		if (abs(mny - mxy) > 1 && (fmxy > lmny || fmny > lmxy))
			ans = min(ans, (mxx - mnx + 1) * (mxy - mny));
		cout << ans  << "\n";
	}
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma comment(linker, "/STACK:2700000000")
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<deque>
#include<bitset>
#include<sstream>
#include<numeric>
#include<random>
#include<functional>
#include<unordered_set>
#include<unordered_map>
#include<stack>
using namespace std;
typedef unsigned int ull;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef pair<ld, ld> pdd;
typedef pair<int, char> pic;
typedef pair<pii, ll> piii;
#define xx first
#define yy second
#define zz second.second
#define mp make_pair
#define y1 oshfkfsldhs
#define union jfghjdghdjhgjhdjgh
#define all(x) (x).begin(), (x).end()
const ll MOD = 998244353;
const ll MOD1 = 1e9 + 7;
const ll BAS = 257;
//const ll dy[] = { -1, 0, 1, 0 };
//const ll dx[] = { 0, 1, 0, -1 };
const ll N = 1.5e8;
const ll H = 22;
const double EPS = 1e-6;
const long double PI = 3.14159265358979323846;
const ll SZ = 300;
const ll INF = 1e15 + 213;
const ll MAXN = 1.5e6 + 10;
const ull D = 2100;

int n;
string s;

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cout.precision(26);
	srand(89398);
#ifdef _DEBUG
	auto _ = freopen("input.txt", "r", stdin);
	_ = freopen("output.txt", "w", stdout);
#else
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	//freopen("game.in", "r", stdin);
	//freopen("game.out", "w", stdout);
#endif
	cin >> n;
	while (n--)
	{
		cin >> s;
		int m = s.size();
		ll mnx = 0, mxx = 0, mny = 0, mxy = 0, curx = 0, cury = 0;
		ll lmnx = 0, lmxx = 0, lmny = 0, lmxy = 0;
		ll fmnx = m, fmxx = m, fmny = m, fmxy = m;
		for (auto c : s)
		{
			if (c == 'W')
			{
				cury++;
				mxy = max(mxy, cury);
			}

			if (c == 'S')
			{
				cury--;
				mny = min(mny, cury);
			}

			if (c == 'D')
			{
				curx++;
				mxx = max(mxx, curx);
			}

			if (c == 'A')
			{
				curx--;
				mnx = min(mnx, curx);
			}
		}
		curx = 0, cury = 0;
		for (ll i = 0; i < m; i++)
		{
			char c = s[i];
			if (c == 'W')
			{
				cury++;
				if (cury == mxy)
					lmxy = i, fmxy = min(fmxy, i);
				if (cury == mny)
					lmny = i, fmny = min(fmny, i);
				if (curx == mxx)
					lmxx = i, fmxx = min(fmxx, i);
				if (curx == mnx)
					lmnx = i, fmnx = min(fmnx, i);
			}

			if (c == 'S')
			{
				cury--;
				if (cury == mxy)
					lmxy = i, fmxy = min(fmxy, i);
				if (cury == mny)
					lmny = i, fmny = min(fmny, i);
				if (curx == mxx)
					lmxx = i, fmxx = min(fmxx, i);
				if (curx == mnx)
					lmnx = i, fmnx = min(fmnx, i);
			}

			if (c == 'D')
			{
				curx++;
				if (cury == mxy)
					lmxy = i, fmxy = min(fmxy, i);
				if (cury == mny)
					lmny = i, fmny = min(fmny, i);
				if (curx == mxx)
					lmxx = i, fmxx = min(fmxx, i);
				if (curx == mnx)
					lmnx = i, fmnx = min(fmnx, i);
			}

			if (c == 'A')
			{
				curx--;
				if (cury == mxy)
					lmxy = i, fmxy = min(fmxy, i);
				if (cury == mny)
					lmny = i, fmny = min(fmny, i);
				if (curx == mxx)
					lmxx = i, fmxx = min(fmxx, i);
				if (curx == mnx)
					lmnx = i, fmnx = min(fmnx, i);
			}
		}
		ll ans = (mxx - mnx + 1) * (mxy - mny + 1);
		if (abs(mnx - mxx) > 1 && (fmxx > lmnx || fmnx > lmxx))
			ans = min(ans, (mxx - mnx) * (mxy - mny + 1));
		if (abs(mny - mxy) > 1 && (fmxy > lmny || fmny > lmxy))
			ans = min(ans, (mxx - mnx + 1) * (mxy - mny));
		cout << ans  << "\n";
	}
}


#include<bits/stdc++.h>
using namespace std;

#define int long long
#define sp ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define cps CLOCKS_PER_SEC
#define f first
#define s second
#define debug1(x) cout<<x<<"\n"
#define debug2(x,y) cout<<x<<" "<<y<<"\n"
#define debug3(x,y,z) cout<<x<<" "<<y<<" "<<z<<"\n"
#define nl cout<<"\n";
#define pq priority_queue
#define inf 99999999999999999
#define test cout<<"abcd\n";
#define pi pair<int,int>
#define pii pair<int,pi>
#define pb push_back
#define mxn 200005
#define mxnn 500
#define mod (ll)998244353
int func2(vector<int> v)
{

    int n = v.size();
    for(int i = 1; i<n; i++) v[i] += v[i-1];
    int mx = 0;
    int mn = 0;
    for(int i = 0; i<n; i++)
    {
        mx = max(mx, v[i]);
        mn = min(mn, v[i]);
    }
    return mx-mn+1;
}
int func(vector<int> v)
{
    int n = v.size();
    for(int i = 1; i<n; i++) v[i] += v[i-1];
    int leftmn = 0,rightmx=0;
    int mx = 0;
    int mn = 0;
    for(int i = 0; i<n; i++)
    {
        mx = max(mx, v[i]);
        mn = min(mn, v[i]);
    }
    for(int i = 0; i<n; i++)
    {
        if(v[i] == mx) rightmx = i;
    }
    for(int i = 0; i<n; i++)
    {
        if(v[i] == mn)
        {
            leftmn = i;
            break;
        }
    }
 //   cout<<mx<<" "<<mn<<" "<<leftmn<<" "<<rightmx<<"\n";
    if(leftmn > rightmx) return mx-mn;
    return mx-mn+1;
}
int32_t main(){
int t;
cin >>t;
while(t--)
{
    string s;
    cin >>s;
    vector<int>v;
    int hori = 1, ver = 1;
    for(int i = 0; i<s.size(); i++)
    {
        if(s[i] == 'W') v.pb(1);
        else if(s[i] == 'S') v.pb(-1);
    }
    int ans1, ans2;
    ver = func2(v);
    ans1 = max(2LL, func(v));
    if(v.size()) ver = max(2LL, ver);
    else ver = 1;
    for(int i = 0; i<v.size(); i++)
    {
        if(v[i] == 1) v[i] = -1;
        else v[i] = 1;
    }
    ans1 = max(2LL, min(ans1, func(v)));
    v.clear();
    for(int i = 0; i<s.size(); i++)
    {
        if(s[i] == 'A') v.pb(-1);
        else if(s[i] == 'D') v.pb(1);
    }
    hori = func2(v);
    if(v.size()) hori = max(2LL, hori);
    else hori = 1;
    ans2 = max(2LL, func(v));
    for(int i = 0; i<v.size(); i++)
    {
        if(v[i] == -1) v[i] = 1;
        else v[i] = -1;
    }
    ans2 = max(2LL, min(ans2, func(v)));
    int ans = max(2LL, min(ans1*hori, ans2*ver));
    cout<<ans<<"\n";
}
return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define int long long
#define sp ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define cps CLOCKS_PER_SEC
#define f first
#define s second
#define debug1(x) cout<<x<<"\n"
#define debug2(x,y) cout<<x<<" "<<y<<"\n"
#define debug3(x,y,z) cout<<x<<" "<<y<<" "<<z<<"\n"
#define nl cout<<"\n";
#define pq priority_queue
#define inf 99999999999999999
#define test cout<<"abcd\n";
#define pi pair<int,int>
#define pii pair<int,pi>
#define pb push_back
#define mxn 200005
#define mxnn 500
#define mod (ll)998244353
int func2(vector<int> v)
{

    int n = v.size();
    for(int i = 1; i<n; i++) v[i] += v[i-1];
    int mx = -9999999;
    int mn = 99999999;
    for(int i = 0; i<n; i++)
    {
        mx = max(mx, v[i]);
        mn = min(mn, v[i]);
    }
    return mx-mn+1;
}
int func(vector<int> v)
{
    int n = v.size();
    for(int i = 1; i<n; i++) v[i] += v[i-1];
    int leftmn = 0,rightmx=0;
    int mx = -9999999;
    int mn = 99999999;
    for(int i = 0; i<n; i++)
    {
        mx = max(mx, v[i]);
        mn = min(mn, v[i]);
    }
    for(int i = 0; i<n; i++)
    {
        if(v[i] == mx) rightmx = i;
    }
    for(int i = 0; i<n; i++)
    {
        if(v[i] == mn)
        {
            leftmn = i;
            break;
        }
    }
 //   cout<<mx<<" "<<mn<<" "<<leftmn<<" "<<rightmx<<"\n";
    if(leftmn > rightmx) return mx-mn;
    return mx-mn+1;
}
int32_t main(){
int t;
cin >>t;
while(t--)
{
    string s;
    cin >>s;
    vector<int>v;
    int hori = 1, ver = 1;
    for(int i = 0; i<s.size(); i++)
    {
        if(s[i] == 'W') v.pb(1);
        else if(s[i] == 'S') v.pb(-1);
    }
    int ans1, ans2;
    ver = func2(v);
    ans1 = max(2LL, func(v));
    if(v.size()) ver = max(2LL, ver);
    else ver = 1;
    for(int i = 0; i<v.size(); i++)
    {
        if(v[i] == 1) v[i] = -1;
        else v[i] = 1;
    }
    ans1 = max(2LL, min(ans1, func(v)));
    v.clear();
    for(int i = 0; i<s.size(); i++)
    {
        if(s[i] == 'A') v.pb(-1);
        else if(s[i] == 'D') v.pb(1);
    }
    hori = func2(v);
    if(v.size()) hori = max(2LL, hori);
    else hori = 1;
    ans2 = max(2LL, func(v));
    for(int i = 0; i<v.size(); i++)
    {
        if(v[i] == -1) v[i] = 1;
        else v[i] = -1;
    }
    ans2 = max(2LL, min(ans2, func(v)));
    int ans = max(2LL, min(ans1*hori, ans2*ver));
    cout<<ans<<"\n";
}
return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define int long long
#define sp ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define cps CLOCKS_PER_SEC
#define f first
#define s second
#define debug1(x) cout<<x<<"\n"
#define debug2(x,y) cout<<x<<" "<<y<<"\n"
#define debug3(x,y,z) cout<<x<<" "<<y<<" "<<z<<"\n"
#define nl cout<<"\n";
#define pq priority_queue
#define inf 99999999999999999
#define test cout<<"abcd\n";
#define pi pair<int,int>
#define pii pair<int,pi>
#define pb push_back
#define mxn 200005
#define mxnn 500
#define mod (ll)998244353
int func2(vector<int> v)
{

    int n = v.size();
    for(int i = 1; i<n; i++) v[i] += v[i-1];
    int mx = -99999999;
    int mn = 0;
    for(int i = 0; i<n; i++)
    {
        mx = max(mx, v[i]);
        mn = min(mn, v[i]);
    }
    return mx-mn+1;
}
int func(vector<int> v)
{
    int n = v.size();
    for(int i = 1; i<n; i++) v[i] += v[i-1];
    int leftmn = 0,rightmx;
    int mx = -99999999;
    int mn = 0;
    for(int i = 0; i<n; i++)
    {
        mx = max(mx, v[i]);
        mn = min(mn, v[i]);
    }
    for(int i = 0; i<n; i++)
    {
        if(v[i] == mx) rightmx = i;
    }
    for(int i = 0; i<n; i++)
    {
        if(v[i] == mn)
        {
            leftmn = i;
            break;
        }
    }
 //   cout<<mx<<" "<<mn<<" "<<leftmn<<" "<<rightmx<<"\n";
    if(leftmn > rightmx) return mx-mn;
    return mx-mn+1;
}
int32_t main(){
int t;
cin >>t;
while(t--)
{
    string s;
    cin >>s;
    vector<int>v;
    int hori = 1, ver = 1;
    for(int i = 0; i<s.size(); i++)
    {
        if(s[i] == 'W') v.pb(1);
        else if(s[i] == 'S') v.pb(-1);
    }
    int ans1, ans2;
    ver = func2(v);
    ans1 = max(2LL, func(v));
    if(v.size()) ver = max(2LL, ver);
    else ver = 1;
    for(int i = 0; i<v.size(); i++)
    {
        if(v[i] == 1) v[i] = -1;
        else v[i] = 1;
    }
    ans1 = max(2LL, min(ans1, func(v)));
    v.clear();
    for(int i = 0; i<s.size(); i++)
    {
        if(s[i] == 'A') v.pb(-1);
        else if(s[i] == 'D') v.pb(1);
    }
    hori = func2(v);
    if(v.size()) hori = max(2LL, hori);
    else hori = 1;
    ans2 = max(2LL, func(v));
    for(int i = 0; i<v.size(); i++)
    {
        if(v[i] == -1) v[i] = 1;
        else v[i] = -1;
    }
    ans2 = max(2LL, min(ans2, func(v)));
    int ans = max(2LL, min(ans1*hori, ans2*ver));
    cout<<ans<<"\n";
}
return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define int long long
#define sp ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define cps CLOCKS_PER_SEC
#define f first
#define s second
#define debug1(x) cout<<x<<"\n"
#define debug2(x,y) cout<<x<<" "<<y<<"\n"
#define debug3(x,y,z) cout<<x<<" "<<y<<" "<<z<<"\n"
#define nl cout<<"\n";
#define pq priority_queue
#define inf 99999999999999999
#define test cout<<"abcd\n";
#define pi pair<int,int>
#define pii pair<int,pi>
#define pb push_back
#define mxn 200005
#define mxnn 500
#define mod (ll)998244353
int func2(vector<int> v)
{

    reverse(v.begin(), v.end());
    v.push_back(0);
    reverse(v.begin(), v.end());
    int n = v.size();
    for(int i = 1; i<n; i++) v[i] += v[i-1];
    int mx = -9999999;
    int mn = 99999999;
    for(int i = 0; i<n; i++)
    {
        mx = max(mx, v[i]);
        mn = min(mn, v[i]);
    }
    return mx-mn+1;
}
int func(vector<int> v)
{
    reverse(v.begin(), v.end());
    v.push_back(0);
    reverse(v.begin(), v.end());
    int n = v.size();
    for(int i = 1; i<n; i++) v[i] += v[i-1];
    int leftmn = 0,rightmx=0;
    int mx = -9999999;
    int mn = 99999999;
    for(int i = 0; i<n; i++)
    {
        mx = max(mx, v[i]);
        mn = min(mn, v[i]);
    }
    for(int i = 0; i<n; i++)
    {
        if(v[i] == mx) rightmx = i;
    }
    for(int i = 0; i<n; i++)
    {
        if(v[i] == mn)
        {
            leftmn = i;
            break;
        }
    }
 //   cout<<mx<<" "<<mn<<" "<<leftmn<<" "<<rightmx<<"\n";
    if(leftmn > rightmx) return mx-mn;
    return mx-mn+1;
}
int32_t main(){
int t;
cin >>t;
while(t--)
{
    string s;
    cin >>s;
    vector<int>v;
    int hori = 1, ver = 1;
    for(int i = 0; i<s.size(); i++)
    {
        if(s[i] == 'W') v.pb(1);
        else if(s[i] == 'S') v.pb(-1);
    }
    int ans1, ans2;
    ver = func2(v);
    ans1 = max(2LL, func(v));
    if(v.size()) ver = max(2LL, ver);
    else ver = 1;
    for(int i = 0; i<v.size(); i++)
    {
        if(v[i] == 1) v[i] = -1;
        else v[i] = 1;
    }
    ans1 = max(2LL, min(ans1, func(v)));
    v.clear();
    for(int i = 0; i<s.size(); i++)
    {
        if(s[i] == 'A') v.pb(-1);
        else if(s[i] == 'D') v.pb(1);
    }
    hori = func2(v);
    if(v.size()) hori = max(2LL, hori);
    else hori = 1;
    ans2 = max(2LL, func(v));
    for(int i = 0; i<v.size(); i++)
    {
        if(v[i] == -1) v[i] = 1;
        else v[i] = -1;
    }
    ans2 = max(2LL, min(ans2, func(v)));
    int ans = max(2LL, min(ans1*hori, ans2*ver));
    cout<<ans<<"\n";
}
return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef pair<int,int> pii;
typedef pair<lli,lli> pll;
int main(){
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--){
    	string s;
    	cin>>s;
    	int minto=0;
    	int maxbo=0;
    	int minle=0;
    	int maxri=0;
    	int x=0;
    	int y=0;
    	for(int i=0;i<s.size();i++){
    		if(s[i]=='W'){
    			x-=1;
    			minto=min(minto,x);
    		}
		    if(s[i]=='S'){
			    x+=1;
			    maxbo=max(maxbo,x);
		    }
		    if(s[i]=='A'){
			    y-=1;
			    minle=min(minle,y);
		    }
		    if(s[i]=='D'){
			    y+=1;
			    maxri=max(maxri,y);
		    }
    	}
    	bool botable=true;
    	bool reach=false;
    	x=0;
    	y=0;
    	for(int i=0;i<s.size();i++){
		    if(s[i]=='W'){
			    x-=1;
		    }
		    if(s[i]=='S'){
			    x+=1;
		    }
		    if(s[i]=='A'){
			    y-=1;
		    }
		    if(s[i]=='D'){
			    y+=1;
		    }
		    if(x==maxbo)reach=true;
		    if(reach&&x==minto)botable=false;
    	}
    	if(x==minto)botable=false;
	    x=0;
	    x=0;
	    y=0;
	    bool topable=true;
	    reach=false;
	    for(int i=0;i<s.size();i++){
		    if(s[i]=='W'){
			    x-=1;
		    }
		    if(s[i]=='S'){
			    x+=1;
		    }
		    if(s[i]=='A'){
			    y-=1;
		    }
		    if(s[i]=='D'){
			    y+=1;
		    }
		    if(x==minto)reach=true;
		    if(reach&&x==maxbo)topable=false;
		    x=x;
	    }
	    if(reach&&x==maxbo)topable=false;
	    bool leable=true;
	    reach=false;
	    x=0;
	    y=0;
	    for(int i=0;i<s.size();i++){
		    if(s[i]=='W'){
			    x-=1;
		    }
		    if(s[i]=='S'){
			    x+=1;
		    }
		    if(s[i]=='A'){
			    y-=1;
		    }
		    if(s[i]=='D'){
			    y+=1;
		    }
		    if(y==minle)reach=true;
		    if(reach&&y==maxri)leable=false;
		    y=y;
	    }
	    if(reach&&y==maxri)leable=false;
	    bool riable=true;
	    reach=false;
	    y=0;
	    x=0;
	    y=0;
	    for(int i=0;i<s.size();i++){
		    if(s[i]=='W'){
			    x-=1;
		    }
		    if(s[i]=='S'){
			    x+=1;
		    }
		    if(s[i]=='A'){
			    y-=1;
		    }
		    if(s[i]=='D'){
			    y+=1;
		    }
		    if(y==maxri)reach=true;
		    if(reach&&y==minle)riable=false;
		    y=y;
	    }
	    if(reach&&y==minle)riable=false;
	    lli h=(lli)(maxbo-minto+1);
	    lli w=(lli)(maxri-minle+1);
	    lli ans=h*w;
	    if(w>2&&(leable||riable)) {
		    ans=min(ans,(w-1)*h);
	    }
	    if(h>2&&(topable||botable)) {
		    ans=min(ans,w*(h-1));
	    }
	    printf("%lli\n",ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef pair<int,int> pii;
typedef pair<lli,lli> pll;
int main(){
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--){
    	string s;
    	cin>>s;
    	int minto=0;
    	int maxbo=0;
    	int minle=0;
    	int maxri=0;
    	int x=0;
    	int y=0;
    	for(int i=0;i<s.size();i++){
    		if(s[i]=='W'){
    			x-=1;
    			minto=min(minto,x);
    		}
		    if(s[i]=='S'){
			    x+=1;
			    maxbo=max(maxbo,x);
		    }
		    if(s[i]=='A'){
			    y-=1;
			    minle=min(minle,y);
		    }
		    if(s[i]=='D'){
			    y+=1;
			    maxri=max(maxri,y);
		    }
    	}
    	bool botable=true;
    	bool reach=false;
    	int prevx=0;
    	x=0;
    	y=0;
    	for(int i=0;i<s.size();i++){
		    if(s[i]=='W'){
			    x-=1;
		    }
		    if(s[i]=='S'){
			    x+=1;
		    }
		    if(s[i]=='A'){
			    y-=1;
		    }
		    if(s[i]=='D'){
			    y+=1;
		    }
		    if(x==maxbo-1)reach=true;
		    if(reach&&prevx==minto)botable=false;
		    prevx=x;
    	}
    	if(prevx==minto)botable=false;
	    prevx=0;
	    x=0;
	    y=0;
	    bool topable=true;
	    reach=false;
	    for(int i=0;i<s.size();i++){
		    if(s[i]=='W'){
			    x-=1;
		    }
		    if(s[i]=='S'){
			    x+=1;
		    }
		    if(s[i]=='A'){
			    y-=1;
		    }
		    if(s[i]=='D'){
			    y+=1;
		    }
		    if(x==minto+1)reach=true;
		    if(reach&&prevx==maxbo)topable=false;
		    prevx=x;
	    }
	    if(reach&&prevx==maxbo)topable=false;
	    bool leable=true;
	    reach=false;
	    int prevy=0;
	    x=0;
	    y=0;
	    for(int i=0;i<s.size();i++){
		    if(s[i]=='W'){
			    x-=1;
		    }
		    if(s[i]=='S'){
			    x+=1;
		    }
		    if(s[i]=='A'){
			    y-=1;
		    }
		    if(s[i]=='D'){
			    y+=1;
		    }
		    if(y==minle+1)reach=true;
		    if(reach&&prevy==maxri)leable=false;
		    prevy=y;
	    }
	    if(reach&&prevy==maxri)leable=false;
	    bool riable=true;
	    reach=false;
	    prevy=0;
	    x=0;
	    y=0;
	    for(int i=0;i<s.size();i++){
		    if(s[i]=='W'){
			    x-=1;
		    }
		    if(s[i]=='S'){
			    x+=1;
		    }
		    if(s[i]=='A'){
			    y-=1;
		    }
		    if(s[i]=='D'){
			    y+=1;
		    }
		    if(y==maxri-1)reach=true;
		    if(reach&&prevy==minle)riable=false;
		    prevy=y;
	    }
	    if(reach&&prevy==minle)riable=false;
	    lli h=(lli)(maxbo-minto+1);
	    lli w=(lli)(maxri-minle+1);
	    lli ans=h*w;
	    if(w>2) {
		    ans=min(ans,(w-1)*h);
	    }
	    if(h>2) {
		    ans=min(ans,w*(h-1));
	    }
	    printf("%lli\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
#define FRU freopen("out.txt","w",stdout)
#define FRO freopen("in.txt","r",stdin)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mem(ara,n) memset(ara,n,sizeof ara)
#define loop(i,j,n) for(i=j;i<n;i++)
#define rloop(i,j,n) for(i=n;i>=j;i--)
#define INF 2147483647
#define ll long long
#define pii pair<int,int>
#define eps 1e-9
#define mii map<int,int>
#define vi vector<int>
#define all(n) n.begin(),n.end()
#define inf INF
#define INFLL 10000000000000000LL

//const int row[]={-1, -1, -1,  0,  0,  1,  1,  1};  // Kings Move
//const int col[]={-1,  0,  1, -1,  1, -1,  0,  1};  // Kings Move
//const int row[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int col[]={-1,  1, -2,  2, -2,  2, -1,  1};  // Knights Move
//const int row[]={-1,0,0,1,0};
//const int col[]={0,-1,1,0,0};
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
int lcm(int a,int b)
{
    return ((a*b)/gcd(a,b));
}

/*bool bitcheck(int n,int pos)
{
    return (bool)(n & (1<<pos));
}

int biton(int n,int pos)
{
    return n=n or (1<<pos);
}
int bitoff(int n,int pos)
{
    return n=n & ~(1<<pos);
}*/

using namespace std;
ll n,ara[3][10],pset[10];
double get_point(ll ac,ll ttl)
{
    if(ac*2>ttl)return 500;
    if(ac*4>ttl)return 1000;
    if(ac*8>ttl)return 1500;
    if(ac*16>ttl)return 2000;
    if(ac*32>ttl)return 2500;
    else return 3000;
}

bool ok(ll fake)
{
    ll p1=0,p2=0;
    for(int i=0;i<5;i++)
    {
        if(ara[0][i]==-1)
        {
            if(ara[1][i]>=0)p2+=(ll)(get_point(pset[i],n+fake)*(1.0-(double)ara[1][i]/250.0));
        }
        else if(ara[1][i]==-1)
        {
            if(ara[0][i]>=0)p1+=(ll)(get_point(pset[i],n+fake)*(1.0-(double)ara[0][i]/250.0));
        }
        else if(ara[0][i]<=ara[1][i])
        {
            p1+=(ll)(get_point(pset[i],n+fake)*(1.0-(double)ara[0][i]/250.0));
            p2+=(ll)(get_point(pset[i],n+fake)*(1.0-(double)ara[1][i]/250.0));
        }
        else
        {
            p1+=(ll)(get_point(pset[i]+fake,n+fake)*(1.0-(double)ara[0][i]/250.0));
            p2+=(ll)(get_point(pset[i]+fake,n+fake)*(1.0-(double)ara[1][i]/250.0));
        }
    }
    return p1>p2;
}

int main()
{
//FRO;
//FRU;
//std::ios_base::sync_with_stdio(false);
    int a,b,c,i,j,k,tc,t;
    cin>>n;
    a=0;
    for(i=0;i<n;i++)
    {
        int flg=0;
        for(j=0;j<5;j++)
        {
            cin>>ara[i][j];
            if(ara[i][j]>=0)pset[j]++,flg=1;
        }
        if(flg)a++;
    }
    n=(ll)a;
    for(i=0;i<100000;i++)
    {
        if(ok(i))
        {
            cout<<i<<endl;
            return 0;
        }
    }
    cout<<-1<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define FRU freopen("out.txt","w",stdout)
#define FRO freopen("in.txt","r",stdin)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mem(ara,n) memset(ara,n,sizeof ara)
#define loop(i,j,n) for(i=j;i<n;i++)
#define rloop(i,j,n) for(i=n;i>=j;i--)
#define INF 2147483647
#define ll long long
#define pii pair<int,int>
#define eps 1e-9
#define mii map<int,int>
#define vi vector<int>
#define all(n) n.begin(),n.end()
#define inf INF
#define INFLL 10000000000000000LL

//const int row[]={-1, -1, -1,  0,  0,  1,  1,  1};  // Kings Move
//const int col[]={-1,  0,  1, -1,  1, -1,  0,  1};  // Kings Move
//const int row[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int col[]={-1,  1, -2,  2, -2,  2, -1,  1};  // Knights Move
//const int row[]={-1,0,0,1,0};
//const int col[]={0,-1,1,0,0};
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
int lcm(int a,int b)
{
    return ((a*b)/gcd(a,b));
}

/*bool bitcheck(int n,int pos)
{
    return (bool)(n & (1<<pos));
}

int biton(int n,int pos)
{
    return n=n or (1<<pos);
}
int bitoff(int n,int pos)
{
    return n=n & ~(1<<pos);
}*/

using namespace std;
ll n,ara[3][10],pset[10];
double get_point(ll ac,ll ttl)
{
    if(ac*2>ttl)return 500;
    if(ac*4>ttl)return 1000;
    if(ac*8>ttl)return 1500;
    if(ac*16>ttl)return 2000;
    if(ac*32>ttl)return 2500;
    else return 3000;
}

bool ok(ll fake)
{
    ll p1=0,p2=0;
    for(int i=0;i<5;i++)
    {
        if(ara[0][i]==-1)
        {
            if(ara[1][i]>=0)p2+=(ll)(get_point(pset[i],n+fake)*((ara[1][i]==0)?1.0:(1.0-(double)ara[1][i]/250.0)));
        }
        else if(ara[1][i]==-1)
        {
            if(ara[0][i]>=0)p1+=(ll)(get_point(pset[i],n+fake)*((ara[0][i]==0)?1.0:(1.0-(double)ara[0][i]/250.0)));
        }
        else if(ara[0][i]<=ara[1][i])
        {
            p1+=(ll)(get_point(pset[i],n+fake)*((ara[0][i]==0)?1.0:(1.0-(double)ara[0][i]/250.0)));
            p2+=(ll)(get_point(pset[i],n+fake)*((ara[1][i]==0)?1.0:(1.0-(double)ara[1][i]/250.0)));
        }
        else
        {
            p1+=(ll)(get_point(pset[i]+fake,n+fake)*((ara[0][i]==0)?1.0:(1.0-(double)ara[0][i]/250.0)));
            p2+=(ll)(get_point(pset[i]+fake,n+fake)*((ara[1][i]==0)?1.0:(1.0-(double)ara[1][i]/250.0)));
        }
        //if(fake==27)cout<<p1<<" "<<p2<<endl;
    }
    //if(p1>p2)cout<<p1<<" "<<p2<<endl;
    return p1>p2;
}

int main()
{
//FRO;
//FRU;
//std::ios_base::sync_with_stdio(false);
    int b,c,i,j,k,tc,t;
    int m;
    cin>>m;
    ll a=0;
    mem(pset,0);
    for(i=0;i<m;i++)
    {
        int flg=0;
        for(j=0;j<5;j++)
        {
            cin>>ara[i][j];
            if(ara[i][j]>=0)pset[j]++,flg=1;
        }
        if(flg)a++;
    }
    //for(i=0;i<5;i++)cout<<pset[i]<<" ";cout<<endl;
    n=a;
    for(i=0;i<100000;i++)
    {
        if(ok((ll)i))
        {
            cout<<i<<endl;
            return 0;
        }
    }
    cout<<-1<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define mem(ara,n) memset(ara,n,sizeof ara)
#define INF 2147483647
#define ll long long
#define all(n) n.begin(),n.end()
#define inf INF

using namespace std;
ll n,ara[101][101],pset[101];
ll get_point(ll ac,ll ttl)
{
    if(ac*2>ttl)return 500;
    if(ac*4>ttl)return 1000;
    if(ac*8>ttl)return 1500;
    if(ac*16>ttl)return 2000;
    if(ac*32>ttl)return 2500;
    else return 3000;
}

int ok(ll fake)//This function is doing the job
{
    ll p1=0,p2=0;
    for(int i=0;i<5;i++)
    {
        if(ara[0][i]==-1)
        {
            if(ara[1][i]>=0)p2+=get_point(pset[i],n+fake)*(250-ara[1][i]);
        }
        else if(ara[1][i]==-1)
        {
            if(ara[0][i]>=0)p1+=get_point(pset[i],n+fake)*(250-ara[0][i]);
        }
        else if(ara[0][i]<=ara[1][i])
        {
            p1+=get_point(pset[i],n+fake)*(250-ara[0][i]);
            p2+=get_point(pset[i],n+fake)*(250-ara[1][i]);
        }
        else
        {
            p1+=get_point(pset[i]+fake,n+fake)*(250-ara[0][i]);
            p2+=get_point(pset[i]+fake,n+fake)*(250-ara[1][i]);
        }

    }
    if(p1>p2)return 1;
    return 0;
}

int main()
{
    //freopen("in.txt","r",stdin);
    int b,c,i,j,k,tc,t;
    int m;
    cin>>m;
    ll a=0;
    mem(pset,0);
    for(i=0;i<m;i++)
    {
        int flg=0;
        for(j=0;j<5;j++)
        {
            scanf("%lld",&ara[i][j]);
            if(ara[i][j]>=0)pset[j]++,flg=1;
        }
        if(flg)a++;
    }
    n=a;
    for(ll ii=0;ii<100000;ii++)
    {
        if(ok(ii))
        {//print answer and exit the program.
            printf("%lld\n",ii);
            return 0;//here the code crashes.
        }
    }
    printf("-1\n");
    return 0;
}


#include<bits/stdc++.h>
#define FRU freopen("out.txt","w",stdout)
#define FRO freopen("in.txt","r",stdin)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mem(ara,n) memset(ara,n,sizeof ara)
#define loop(i,j,n) for(i=j;i<n;i++)
#define rloop(i,j,n) for(i=n;i>=j;i--)
#define INF 2147483647
#define ll long long
#define pii pair<int,int>
#define eps 1e-9
#define mii map<int,int>
#define vi vector<int>
#define all(n) n.begin(),n.end()
#define inf INF
#define INFLL 10000000000000000LL

//const int row[]={-1, -1, -1,  0,  0,  1,  1,  1};  // Kings Move
//const int col[]={-1,  0,  1, -1,  1, -1,  0,  1};  // Kings Move
//const int row[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int col[]={-1,  1, -2,  2, -2,  2, -1,  1};  // Knights Move
//const int row[]={-1,0,0,1,0};
//const int col[]={0,-1,1,0,0};
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
int lcm(int a,int b)
{
    return ((a*b)/gcd(a,b));
}

/*bool bitcheck(int n,int pos)
{
    return (bool)(n & (1<<pos));
}

int biton(int n,int pos)
{
    return n=n or (1<<pos);
}
int bitoff(int n,int pos)
{
    return n=n & ~(1<<pos);
}*/

using namespace std;
ll n,ara[3][10],pset[10];
double get_point(ll ac,ll ttl)
{
    if(ac*2>ttl)return 500;
    if(ac*4>ttl)return 1000;
    if(ac*8>ttl)return 1500;
    if(ac*16>ttl)return 2000;
    if(ac*32>ttl)return 2500;
    else return 3000;
}

bool ok(ll fake)
{
    ll p1=0,p2=0;
    for(int i=0;i<5;i++)
    {
        if(ara[0][i]==-1)
        {
            if(ara[1][i]>=0)p2+=(ll)(get_point(pset[i],n+fake)*((ara[1][i]==0)?1.0:(1.0-(double)ara[1][i]/250.0)));
        }
        else if(ara[1][i]==-1)
        {
            if(ara[0][i]>=0)p1+=(ll)(get_point(pset[i],n+fake)*((ara[0][i]==0)?1.0:(1.0-(double)ara[0][i]/250.0)));
        }
        else if(ara[0][i]<=ara[1][i])
        {
            p1+=(ll)(get_point(pset[i],n+fake)*((ara[0][i]==0)?1.0:(1.0-(double)ara[0][i]/250.0)));
            p2+=(ll)(get_point(pset[i],n+fake)*((ara[1][i]==0)?1.0:(1.0-(double)ara[1][i]/250.0)));
        }
        else
        {
            p1+=(ll)(get_point(pset[i]+fake,n+fake)*((ara[0][i]==0)?1.0:(1.0-(double)ara[0][i]/250.0)));
            p2+=(ll)(get_point(pset[i]+fake,n+fake)*((ara[1][i]==0)?1.0:(1.0-(double)ara[1][i]/250.0)));
        }
        //f(fake==27)cout<<p1<<" "<<p2<<endl;
    }
    //if(p1>p2)cout<<p1<<" "<<p2<<endl;
    return p1>p2;
}

int main()
{
//FRO;
//FRU;
//std::ios_base::sync_with_stdio(false);
    int a,b,c,i,j,k,tc,t;
    int m;
    cin>>m;
    a=0;
    mem(pset,0);
    for(i=0;i<m;i++)
    {
        int flg=0;
        for(j=0;j<5;j++)
        {
            cin>>ara[i][j];
            if(ara[i][j]>=0)pset[j]++,flg=1;

        }
        //cout<<m<<" ";for(j=0;j<5;j++)cout<<pset[j]<<" ";cout<<endl;
        if(flg)a++;
    }
    //for(i=0;i<5;i++)cout<<pset[i]<<" ";cout<<endl;
    n=(ll)a;
    for(i=0;i<100000;i++)
    {
        if(ok((ll)i))
        {
            cout<<i<<endl;
            return 0;
        }
    }
    cout<<-1<<endl;
    return 0;
}


#include<bits/stdc++.h>

#define X first
#define Y second
#define eb push_back
#define siz(a) int(a.size())

#define trace2(x, y)             cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<< endl;
#define trace3(x, y, z)          cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define trace4(a, b, c, d)       cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define trace5(a, b, c, d, e)    cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<<": "<<e<<endl;

using namespace std;

typedef long long int ll;
typedef vector < int > vi;
typedef vector < ll > vll;

const int mod=1e9+7;
const int maxn=1e6+5;

int n, m=5, arr[200][10], solve[10];

int get(int done, int total){
    long double fuck = 1.0*done/total;

    if(fuck > 1.0/2.0)
        return 500;
    if(fuck > 1.0/4.0)
        return 1000;
    if(fuck > 1.0/8.0)
        return 1500;
    if(fuck > 1.0/16.0)
        return 2000;
    if(fuck > 1.0/32.0)
        return 2500;
    return 3000;
}

bool go(int extra){
    int j;
    int curr[6];
    long double s1 = 0.0, s0 = 0.0;
    for(j=1; j<=5; j++){
        curr[j] = solve[j];
        if(arr[0][j] != -1){
            if(arr[0][j] > arr[1][j] && arr[1][j] != -1){
                curr[j] += extra;
            }
        }

//        trace5(j, curr[j], arr[0][j], arr[1][j], extra);
//        trace3(get(curr[j], n+extra), s0, s1);
        if(arr[0][j] != -1){
            s0 += get(curr[j], n+extra)*(1 - 1.0*arr[0][j]/250.0);
        }
        if(arr[1][j] != -1){
            s1 += get(curr[j], n+extra)*(1 - 1.0*arr[1][j]/250.0);
        }
    }
//    trace3(extra, s0, s1);
    return s0 > s1;
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
    int i, j;
    cin >> n;

    for(i=0; i<n; i++){
        for(j=1; j<=m; j++){
            cin >> arr[i][j];

//            trace3(i, j, arr[i][j]);

            if(arr[i][j] > -1){
                solve[j]++;
            }
        }
    }

    for(i=0; i<50*n; i++){
        if(go(i)){
            cout << i;
            return 0;
        }
    }
    cout << "-1";
	return 0;
}


#include<bits/stdc++.h>

#define X first
#define Y second
#define eb push_back
#define siz(a) int(a.size())

#define trace2(x, y)             cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<< endl;
#define trace3(x, y, z)          cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define trace4(a, b, c, d)       cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define trace5(a, b, c, d, e)    cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<<": "<<e<<endl;

using namespace std;

typedef long long int ll;
typedef vector < int > vi;
typedef vector < ll > vll;

const int mod=1e9+7;
const int maxn=1e6+5;

int n, m=5, arr[200][10], solve[10];

int get(int done, int total){
    long double fuck = 1.0*done/total;

    if(fuck > 1.0/2.0)
        return 500;
    if(fuck > 1.0/4.0)
        return 1000;
    if(fuck > 1.0/8.0)
        return 1500;
    if(fuck > 1.0/16.0)
        return 2000;
    if(fuck > 1.0/32.0)
        return 2500;
    return 3000;
}

bool go(int extra){
    int j;
    int curr[6];
    long double s1 = 0.0, s0 = 0.0;
    for(j=1; j<=5; j++){
        curr[j] = solve[j];
        if(arr[0][j] != -1){
            if(arr[0][j] > arr[1][j] && arr[1][j] != -1){
                curr[j] += extra;
            }
        } else{
            curr[j] += extra;
        }

 //       trace5(j, curr[j], arr[0][j], arr[1][j], extra);
//        trace3(get(curr[j], n+extra), s0, s1);
        if(arr[0][j] != -1){
            s0 += get(curr[j], n+extra)*(1 - 1.0*arr[0][j]/250.0);
        }
        if(arr[1][j] != -1){
            s1 += get(curr[j], n+extra)*(1 - 1.0*arr[1][j]/250.0);
        }
    }
//    trace3(extra, s0, s1);
    return s0 > s1;
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
    int i, j;
    cin >> n;

    for(i=0; i<n; i++){
        for(j=1; j<=m; j++){
            cin >> arr[i][j];

//            trace3(i, j, arr[i][j]);

            if(arr[i][j] > -1){
                solve[j]++;
            }
        }
    }

    for(i=0; i<50*n; i++){
        if(go(i)){
            cout << i;
            return 0;
        }
    }
    cout << "-1";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define FOR(i,f,t) for(int i=(int)f; i<(int)t; i++)
#define FORR(i,f,t) for(int i=(int)f; i>(int)t; i--)
#define ms(obj, val) memset(obj, val, sizeof(obj))
#define pb push_back
#define SYNC ios_base::sync_with_stdio(false)
#define inf 2000248012
#define mp make_pair
#define F first
#define S second
#define ri(x) scanf("%d",&x)
#define rii(x,y) scanf("%d %d",&x,&y)
#define riii(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define lri(x) scanf("%lld",&x)
#define lrii(x,y) scanf("%lld %lld",&x,&y)
#define lriii(x,y,z) scanf("%lld %lld %lld",&x,&y,&z)

typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> ii;
double X0;
double Y0;
const int MAXN = 5e3+3;
int n,m;
int a[120][5];
int solvers[5];
int vas,pet;
bool should[5];

int score(int j,int min){
	if(min==-1)return 0;
	int b;
	if((solvers[j]<=n)&&(2*solvers[j]>n))b=500;
	else if((2*solvers[j]<=n)&&(4*solvers[j]>n))b=1000;
	else if((4*solvers[j]<=n)&&(8*solvers[j]>n))b=1500;
	else if((8*solvers[j]<=n)&&(16*solvers[j]>n))b=2000;
	else if((16*solvers[j]<=n)&&(32*solvers[j]>n))b=2500;
	else if(32*solvers[j]<=n)b=3000;
	return (b/250)*(250-min);
}

int main(){
	ri(n);
	FOR(i,0,n){
		FOR(j,0,5){
			ri(a[i][j]);
			solvers[j]=0;
		}
	}
	FOR(i,0,n){
		FOR(j,0,5){
			if(a[i][j]!=-1)solvers[j]++;
		}
	}
	vas=0;
	pet=0;
	FOR(i,0,5){
		vas+=score(i,a[0][i]);
		pet+=score(i,a[1][i]);
	}
	if(vas>pet)printf("0\n");
	else{
		FOR(i,0,5){
			if(a[1][i]==-1)should[i]=false;
			else if(a[0][i]==-1)should[i]=false;
			else if(a[0][i]<=a[1][i])should[i]=false;
			else should[i]=true;
		}
		int m=n;
		while(n-m<=32*m){
			n++;
			FOR(i,0,5){
				if(should[i])solvers[i]++;
			}
			vas=0;
			pet=0;
			FOR(i,0,5){
				vas+=score(i,a[0][i]);
				pet+=score(i,a[1][i]);
			}
			if(vas>pet){
				printf("%d\n",n-m);
				break;
			}
		}
		if(vas<=pet)printf("-1\n");
	}

	return 0;
	}


#include <bits/stdc++.h>
using namespace std;

#define FOR(i,f,t) for(int i=(int)f; i<(int)t; i++)
#define FORR(i,f,t) for(int i=(int)f; i>(int)t; i--)
#define ms(obj, val) memset(obj, val, sizeof(obj))
#define pb push_back
#define SYNC ios_base::sync_with_stdio(false)
#define inf 2000248012
#define mp make_pair
#define F first
#define S second
#define ri(x) scanf("%d",&x)
#define rii(x,y) scanf("%d %d",&x,&y)
#define riii(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define lri(x) scanf("%lld",&x)
#define lrii(x,y) scanf("%lld %lld",&x,&y)
#define lriii(x,y,z) scanf("%lld %lld %lld",&x,&y,&z)

typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> ii;
double X0;
double Y0;
const int MAXN = 5e3+3;
int n,m;
int a[120][5];
int solvers[5];
int vas,pet;
bool should[5];

int score(int j,int min){
	if(min==-1)return 0;
	int b;
	if((solvers[j]<=n)&&(2*solvers[j]>n))b=500;
	else if((2*solvers[j]<=n)&&(4*solvers[j]>n))b=1000;
	else if((4*solvers[j]<=n)&&(8*solvers[j]>n))b=1500;
	else if((8*solvers[j]<=n)&&(16*solvers[j]>n))b=2000;
	else if((16*solvers[j]<=n)&&(32*solvers[j]>n))b=2500;
	else if(32*solvers[j]<=n)b=3000;
	return (b/250)*(250-min);
}

int main(){
	ri(n);
	FOR(i,0,n){
		FOR(j,0,5){
			ri(a[i][j]);
			solvers[j]=0;
		}
	}
	FOR(i,0,n){
		FOR(j,0,5){
			if(a[i][j]!=-1)solvers[j]++;
		}
	}
	vas=0;
	pet=0;
	FOR(i,0,5){
		vas+=score(i,a[0][i]);
		pet+=score(i,a[1][i]);
	}
	if(vas>pet)printf("0\n");
	else{
		FOR(i,0,5){
			if(a[1][i]==-1)should[i]=false;
			else if(a[0][i]==-1)should[i]=false;
			else if(a[0][i]<=a[1][i])should[i]=false;
			else should[i]=true;
		}
		int m=n;
		while(n<=500){
			n++;
			FOR(i,0,5){
				if(should[i])solvers[i]++;
			}
			vas=0;
			pet=0;
			FOR(i,0,5){
				vas+=score(i,a[0][i]);
				pet+=score(i,a[1][i]);
			}
			if(vas>pet){
				printf("%d\n",n-m);
				break;
			}
		}
		if(vas<=pet)printf("-1\n");
	}

	return 0;
	}