#include <iostream>
#include <cstring>
#include <string>
using namespace std;

const int MOD = 2011;
int inv[2022];
char AA[100][100];
bool visited[100][100];

class Result
{
public:
	int x,y,v;
	Result()
	{}

	Result(int x, int y, int v)
		:x(x),y(y),v(v)
	{}
};

int findexpr(int x, int y, int d)
{
	while(1) {
		if(y < 0 || AA[y][x] == '!') break;
		if(visited[y][x]) break;
		if(AA[y][x] !='.') return y;

		y += d;
	}

	return -1;
}

Result expr(int x, int y);
Result term(int x, int y);
Result factor(int x, int y);
Result powexpr(int x, int y);
Result primary(int x, int y);
Result fraction(int x, int y);
Result digit(int x, int y);

Result expr(int x, int y)
{
	visited[y][x] = true;
	Result a = term(x,y);
	x = a.x; y = a.y;

	while(AA[y][x+1] == '+' || AA[y][x+1] == '-') {
		visited[y][x+1] = true;
		char op = AA[y][x+1];

		Result b = term(x + 3, y);
		x = b.x; y = b.y;
		
		if(op == '+') a.v = (a.v + b.v) % MOD;
		if(op == '-') a.v = (a.v - b.v + MOD) % MOD;
	}

	return Result(x,y,a.v);
}

Result term(int x, int y)
{
	visited[y][x] = true;

	Result a = factor(x, y);
	x = a.x; y = a.y;

	while(AA[y][x+1] == '*') {
		visited[y][x+1] = true;

		Result b = factor(x + 3, y);
		x = b.x; y = b.y;
		a.v *= b.v;
		a.v %= MOD;
	}

	return Result(x,y,a.v);
}

Result factor(int x, int y)
{
	visited[y][x] = true;

	Result a;
	if(AA[y][x] == '-') {
		if(AA[y][x+1] == '-') {
			a = fraction(x, y);
		}
		else {
			a = factor(x + 2, y);
			a.v = (-a.v + MOD) % MOD;
		}
	}
	else {
		a = powexpr(x, y);
	}

	return a;
}

Result powexpr(int x, int y)
{
	visited[y][x] = true;

	Result a = primary(x,y);
	x = a.x; y = a.y;

	if(y!=0 && isdigit(AA[y-1][x])) {
		int pw = AA[y-1][x] - '0';
		int p = a.v;
		int res = 1;
		for(int i=0; i<pw; i++) {
			res *= p;
			res %= MOD;
		}

		a.v = res % MOD;
		a.x += 1;
	}

	return a;
}

Result primary(int x, int y)
{
	visited[y][x] = true;

	Result a;
	if(isdigit(AA[y][x])) {
		a.v = AA[y][x] - '0';
		a.x = x + 1;
		a.y = y;
	}
	else {
		a = expr(x + 2,y);
		a.x += 2;
	}

	return a;
}

Result fraction(int x, int y)
{
	bool findTop=false, findBtm=false;
	Result t, b;

	int tx = x;
	while(AA[y][x] == '-') {
		visited[y][x] = true;
		x++;
	}

	x = tx;
	while(AA[y][x] == '-') {
		if(!findTop) {
			int ty = findexpr(x,y-1,-1);
			if(ty != -1) {
				t = expr(x,ty);
				findTop = true;
			}
		}
		if(!findBtm) {
			int by = findexpr(x,y+1,1);
			if(by != -1) {
				b = expr(x,by);
				findBtm = true;
			}
		}

		x++;
	}

	Result res;
	res.x = x;
	res.y = y;
	res.v = t.v * inv[b.v] % MOD;

	return res;
}

int main()
{
	for(int i=1; i<MOD; i++)
	for(int j=1; j<MOD; j++) 
		if(j * i % MOD == 1) inv[i] = j;

	int N;
	while(cin >> N, N) {
		memset(visited, 0, sizeof(visited));
		for(int i=0; i<100; i++)
		for(int j=0; j<100; j++)
			AA[j][i] = '!';

		for(int i=0; i<N; i++)
			cin >> AA[i];

		cout << expr(0, findexpr(0,0,1)).v << endl;
	}
}