# 2015. 自修室

TTP1128 edited 4 月，3 周前

// #pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#include <set>
#include <cctype>
#include <bitset>
#define IO                       \
ios::sync_with_stdio(false); \
// cout.tie(0);
using namespace std;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 2e6 + 10;
const int maxm = 2e5 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const double pi = acos(-1);
int dis[8][2] = {1, 0, 0, -1, 0, 1, -1, 0, 1, 1, -1, -1, 1, -1, -1, 1};
// int dis[2][2] = {1, 0, 0, 1};
// int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

struct Node
{
int id, dist, r, c;
int s[100][100];
} a[1005];
int n;
bool cmp(Node a, Node b)
{
return a.dist < b.dist;
}
bool judge(int p, int x, int y)
{
int cnt = 0;
for (int i = 1; i < 4; i++)
{
int tx = x + dis[i][0];
int ty = y + dis[i][1];
if (tx >= 1 && tx <= a[p].r && ty >= 1 && ty <= a[p].c && a[p].s[tx][ty] == 0)
{
cnt++;
}
}
if (x == 1 && (y == 1 || y == a[p].c) && cnt == 1)
return true;
else if ((y == 1 || y == a[p].c || x == 1) && cnt == 2)
return true;
else
return cnt == 3;
}
bool check(int p)
{
for (int i = 1; i <= a[p].r; i++)
{
for (int j = 1; j <= a[p].c; j++)
{
if (a[p].s[i][j] == 0)
{
if (judge(p, i, j))
return true;
}
}
}
return false;
}

int main()
{
#ifdef WXY
freopen("in.txt", "r", stdin);
//	 freopen("out.txt", "w", stdout);
#endif
// IO;
while (scanf("%d", &n) == 1)
{
for (int i = 0; i < n; i++)
{
scanf("%d %d %d %d", &a[i].id, &a[i].dist, &a[i].r, &a[i].c);
for (int k = 1; k <= a[i].r; k++)
{
for (int j = 1; j <= a[i].c; j++)
{
scanf("%1d", &a[i].s[k][j]);
}
}
}
sort(a, a + n, cmp);

int f = 0;
for (int i = 0; i < n; i++)
{
if (check(i))
{
f = 1;
cout << a[i].id << "\n";
break;
}
}
if (f == 0)