#include<bits/stdc++.h> 
using namespace std;
namespace Ruri{
    #define ms(a,b) memset(a,b,sizeof(a))
    #define repi(i,a,b) for(int i=a,bbb=b;i<=bbb;++i)//attention reg int or reg ll ?
    #define repd(i,a,b) for(int i=a,bbb=b;i>=bbb;--i)
    #define reps(s) for(int i=head[s],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
    #define ce(i,r) i==r?'\n':' '
    #define pb push_back 
    #define all(x) x.begin(),x.end()
    #define gmn(a,b) a=min(a,b)
    #define gmx(a,b) a=max(a,b)
    #define fi first
    #define se second                         
    typedef long long ll;
    typedef unsigned long long ull;
    typedef double db;
    const int infi=1e9+5;//infi较大，注意涉及inf相加时爆int
    const ll infl=4e18;
    inline ll ceil_div(ll a,ll b){ return (a+b-1)/b; }
    inline ll pos_mod(ll a,ll b){ return (a%b+b)%b; }
    //std::mt19937 rnd(time(0));//std::mt19937_64 rnd(time(0));
}
using namespace Ruri;
namespace Read{
    #define ss(a) scanf("%s",a)
    inline int ri(){ int x; scanf("%d",&x); return x; }
    inline ll rl(){ ll x;  scanf("%lld",&x); return x; }
    inline db rd(){ db x; scanf("%lf",&x); return x; }
}
namespace DeBug{
    #define pr(x) cout<<#x<<": "<<(x)<<endl
    #define pra(x,a,b) cout<<#x<<": "<<endl; \
            repi(i,a,b) cout<<x[i]<<" ";     \
            puts("");   
    #define FR(a) freopen(a,"r",stdin)
    #define FO(a) freopen(a,"w",stdout)
}
using namespace Read;
using namespace DeBug;
const int MAX_N=3e3+5;
int n,a[MAX_N][6];
bool ok[60];
bool Check(int x)
{
	ms(ok,0);
	repi(i,1,n){
		int state=0;
		repi(j,0,4)if(a[i][j]>=x) state|=(1<<j);
		ok[state]=true;
	}
	repi(i,1,31)if(ok[i])repi(j,i,31)if(ok[j])repi(k,j,31)if(ok[k])
		if((i|j|k)==31) return true;
	return false;
}
int main()
{
	n=ri();
	repi(i,1,n)repi(j,0,4) a[i][j]=ri();
	int l=0,r=infi,ans;
	while(l<=r){
		int mid=(l+r)>>1;
		if(Check(mid)) ans=mid,l=mid+1;
		else r=mid-1;
	}
	printf("%d\n",ans);

    return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define REP(i,n) for(ll i=0;i<ll(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=ll(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=ll(b);i--)

const ll INF = 1000000000000; //10^12:∞
const ll MOD = 1000000007; //10^9+7:合同式の法

// 使う関数の前に記述しておく
// mll a = 1;という宣言で使用できる
// tabキーで下まで移動
template<ll MOD> struct Fp {
    long long val;
    constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
        if (val < 0) val += MOD;
    }
    constexpr ll getmod() { return MOD; }
    constexpr Fp operator - () const noexcept {
        return val ? MOD - val : 0;
    }
    constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
    constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
    constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
    constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
    constexpr Fp& operator += (const Fp& r) noexcept {
        val += r.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr Fp& operator -= (const Fp& r) noexcept {
        val -= r.val;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr Fp& operator *= (const Fp& r) noexcept {
        val = val * r.val % MOD;
        return *this;
    }
    constexpr Fp& operator /= (const Fp& r) noexcept {
        long long a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b; swap(a, b);
            u -= t * v; swap(u, v);
        }
        val = val * u % MOD;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr bool operator == (const Fp& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator != (const Fp& r) const noexcept {
        return this->val != r.val;
    }
    friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
        return os << x.val;
    }
    friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
        if (n == 0) return 1;
        auto t = modpow(a, n / 2);
        t = t * t;
        if (n & 1) t = t * a;
        return t;
    }
};

using mll = Fp<MOD>;

int main(){
    ll H, W;
    cin >> H >> W;
    vector<string> s(H);
    REP(i, H) {
        cin >> s.at(i);
    }
    vector<vector<mll>> dp(H + 1, vector<mll>(W + 1, 0));
    dp.at(1).at(1) = 1;
    vector<vector<mll>> X = dp;
    vector<vector<mll>> Y = dp;
    vector<vector<mll>> Z = dp;
    FOR(i, 1, H) {
        FOR(j, 1, W) {
            if(s.at(i - 1).at(j - 1) == '#') {
                continue;
            }
            if(i == 1 && j == 1) {
                continue;
            }
            dp.at(i).at(j) = X.at(i).at(j - 1) + Y.at(i - 1).at(j) + Z.at(i - 1).at(j - 1);
            X.at(i).at(j) = X.at(i).at(j - 1) + dp.at(i).at(j);
            Y.at(i).at(j) = Y.at(i - 1).at(j) + dp.at(i).at(j);
            Z.at(i).at(j) = Z.at(i - 1).at(j - 1) + dp.at(i).at(j);
        }
    }

    cout << dp.at(H).at(W) << endl;
    return 0;
}