'''simple docstring'''




import collections
import gzip
import os
import urllib

import numpy
from tensorflow.python.framework import dtypes, random_seed
from tensorflow.python.platform import gfile
from tensorflow.python.util.deprecation import deprecated

_A								=   collections.namedtuple('_Datasets', ['train', 'validation', 'test'])

# CVDF mirror of http://yann.lecun.com/exdb/mnist/
_A								=   '''https://storage.googleapis.com/cvdf-datasets/mnist/'''

def _UpperCamelCase   (       SCREAMING_SNAKE_CASE_							):
					lowercase_					:       str     				=					numpy.dtype(numpy.uintaa							).newbyteorder('>'							)
					return numpy.frombuffer(bytestream.read(4							)  ,       dtype=_UpperCAmelCase							)[0]

@deprecated(_UpperCAmelCase  ,       'Please use tf.data to implement this functionality.'							)
def _UpperCamelCase   (       SCREAMING_SNAKE_CASE_							):
					print('Extracting'  ,       f.name							)
					with gzip.GzipFile(fileobj=_UpperCAmelCase							) as bytestream:
										lowercase_					:       Union[str, Any]     				=					_readaa(_UpperCAmelCase							)
										if magic != 2_051:
															raise ValueError(
															    'Invalid magic number %d in MNIST image file: %s' % (magic, f.name)							)
										lowercase_					:       Optional[int]     				=					_readaa(_UpperCAmelCase							)
										lowercase_					:       Any     				=					_readaa(_UpperCAmelCase							)
										lowercase_					:       int     				=					_readaa(_UpperCAmelCase							)
										lowercase_					:       List[Any]     				=					bytestream.read(rows * cols * num_images							)
										lowercase_					:       List[Any]     				=					numpy.frombuffer(_UpperCAmelCase  ,       dtype=numpy.uinta							)
										lowercase_					:       Optional[Any]     				=					data.reshape(_UpperCAmelCase  ,       _UpperCAmelCase  ,       _UpperCAmelCase  ,       1							)
										return data

@deprecated(_UpperCAmelCase  ,       'Please use tf.one_hot on tensors.'							)
def _UpperCamelCase   (       SCREAMING_SNAKE_CASE_  ,       SCREAMING_SNAKE_CASE_							):
					lowercase_					:       str     				=					labels_dense.shape[0]
					lowercase_					:       int     				=					numpy.arange(_UpperCAmelCase							) * num_classes
					lowercase_					:       str     				=					numpy.zeros((num_labels, num_classes)							)
					lowercase_					:       Tuple     				=					1
					return labels_one_hot

@deprecated(_UpperCAmelCase  ,       'Please use tf.data to implement this functionality.'							)
def _UpperCamelCase   (       SCREAMING_SNAKE_CASE_  ,       SCREAMING_SNAKE_CASE_=False  ,       SCREAMING_SNAKE_CASE_=10							):
					print('Extracting'  ,       f.name							)
					with gzip.GzipFile(fileobj=_UpperCAmelCase							) as bytestream:
										lowercase_					:       str     				=					_readaa(_UpperCAmelCase							)
										if magic != 2_049:
															raise ValueError(
															    'Invalid magic number %d in MNIST label file: %s' % (magic, f.name)							)
										lowercase_					:       Dict     				=					_readaa(_UpperCAmelCase							)
										lowercase_					:       str     				=					bytestream.read(_UpperCAmelCase							)
										lowercase_					:       int     				=					numpy.frombuffer(_UpperCAmelCase  ,       dtype=numpy.uinta							)
										if one_hot:
															return _dense_to_one_hot(_UpperCAmelCase  ,       _UpperCAmelCase							)
										return labels


class       UpperCAmelCase__  :






			"""simple docstring"""
			@deprecated(
			    __SCREAMING_SNAKE_CASE       ,				'Please use alternatives such as official/mnist/_DataSet.py'
			    ' from tensorflow/models.'       ,				)
			def __init__(self       ,				_a       ,				_a       ,				_a=False       ,				_a=False       ,				_a=dtypes.floataa       ,				_a=True       ,				_a=None       ,				)    ->     Union[str, Any]:
								lowercase_				,lowercase_					:       Tuple     				=					random_seed.get_seed(__SCREAMING_SNAKE_CASE				)
								# If op level seed is not set, use whatever graph level seed is returned
								numpy.random.seed(seeda if seed is None else seeda				)
								lowercase_					:       str     				=					dtypes.as_dtype(__SCREAMING_SNAKE_CASE				).base_dtype
								if dtype not in (dtypes.uinta, dtypes.floataa):
													raise TypeError('Invalid image dtype %r, expected uint8 or float32' % dtype				)
								if fake_data:
													lowercase_					:       List[Any]     				=					10_000
													lowercase_					:       Optional[Any]     				=					one_hot
								else:
													assert (
													    images.shape[0] == labels.shape[0]
													), f'''images.shape: {images.shape} labels.shape: {labels.shape}'''
													lowercase_					:       Optional[int]     				=					images.shape[0]

													# Convert shape from [num examples, rows, columns, depth]
													# to [num examples, rows*columns] (assuming depth == 1)
													if reshape:
																		assert images.shape[3] == 1
																		lowercase_					:       Any     				=					images.reshape(
																		    images.shape[0]       ,				images.shape[1] * images.shape[2]				)
													if dtype == dtypes.floataa:
																		# Convert from [0, 255] -> [0.0, 1.0].
																		lowercase_					:       Optional[Any]     				=					images.astype(numpy.floataa				)
																		lowercase_					:       Tuple     				=					numpy.multiply(__SCREAMING_SNAKE_CASE       ,				1.0 / 255.0				)
								lowercase_					:       Tuple     				=					images
								lowercase_					:       Optional[Any]     				=					labels
								lowercase_					:       Union[str, Any]     				=					0
								lowercase_					:       Tuple     				=					0
			@property
			def 		_lowerCamelCase (self				)    ->     List[Any]:
								return self._images
			@property
			def 		_lowerCamelCase (self				)    ->     Any:
								return self._labels
			@property
			def 		_lowerCamelCase (self				)    ->     Optional[Any]:
								return self._num_examples
			@property
			def 		_lowerCamelCase (self				)    ->     List[str]:
								return self._epochs_completed





			def 		_lowerCamelCase (self       ,				_a       ,				_a=False       ,				_a=True				)    ->     Tuple:

								if fake_data:
													lowercase_					:       Dict     				=					[1] * 784
													lowercase_					:       List[Any]     				=					[1] + [0] * 9 if self.one_hot else 0
													return (
													    [fake_image for _ in range(__SCREAMING_SNAKE_CASE				)],
													    [fake_label for _ in range(__SCREAMING_SNAKE_CASE				)],
													)
								lowercase_					:       List[str]     				=					self._index_in_epoch
								# Shuffle for the first epoch
								if self._epochs_completed == 0 and start == 0 and shuffle:
													lowercase_					:       Tuple     				=					numpy.arange(self._num_examples				)
													numpy.random.shuffle(__SCREAMING_SNAKE_CASE				)
													lowercase_					:       Tuple     				=					self.images[perma]
													lowercase_					:       Dict     				=					self.labels[perma]
								# Go to the next epoch
								if start + batch_size > self._num_examples:
													# Finished epoch
													self._epochs_completed += 1
													# Get the rest examples in this epoch
													lowercase_					:       Tuple     				=					self._num_examples - start
													lowercase_					:       Optional[Any]     				=					self._images[start : self._num_examples]
													lowercase_					:       Any     				=					self._labels[start : self._num_examples]
													# Shuffle the data
													if shuffle:
																		lowercase_					:       List[Any]     				=					numpy.arange(self._num_examples				)
																		numpy.random.shuffle(__SCREAMING_SNAKE_CASE				)
																		lowercase_					:       Tuple     				=					self.images[perm]
																		lowercase_					:       Tuple     				=					self.labels[perm]
													# Start next epoch
													lowercase_					:       List[Any]     				=					0
													lowercase_					:       str     				=					batch_size - rest_num_examples
													lowercase_					:       Optional[Any]     				=					self._index_in_epoch
													lowercase_					:       Dict     				=					self._images[start:end]
													lowercase_					:       List[Any]     				=					self._labels[start:end]
													return (
													    numpy.concatenate((images_rest_part, images_new_part)       ,				axis=0				),
													    numpy.concatenate((labels_rest_part, labels_new_part)       ,				axis=0				),
													)
								else:
													self._index_in_epoch += batch_size
													lowercase_					:       Optional[Any]     				=					self._index_in_epoch
													return self._images[start:end], self._labels[start:end]



@deprecated(_UpperCAmelCase  ,       'Please write your own downloading logic.'							)
def _UpperCamelCase   (       SCREAMING_SNAKE_CASE_  ,       SCREAMING_SNAKE_CASE_  ,       SCREAMING_SNAKE_CASE_							):
					if not gfile.Exists(_UpperCAmelCase							):
										gfile.MakeDirs(_UpperCAmelCase							)
					lowercase_					:       Union[str, Any]     				=					os.path.join(_UpperCAmelCase  ,       _UpperCAmelCase							)
					if not gfile.Exists(_UpperCAmelCase							):
										urllib.request.urlretrieve(_UpperCAmelCase  ,       _UpperCAmelCase							)  # noqa: S310
										with gfile.GFile(_UpperCAmelCase							) as f:
															lowercase_					:       Optional[Any]     				=					f.size()
										print('Successfully downloaded'  ,       _UpperCAmelCase  ,       _UpperCAmelCase  ,       'bytes.'							)
					return filepath






@deprecated(
    _UpperCAmelCase  ,       'Please use alternatives such as:' ' tensorflow_datasets.load(\'mnist\')'							)
def _UpperCamelCase   (       SCREAMING_SNAKE_CASE_  ,       SCREAMING_SNAKE_CASE_=False  ,       SCREAMING_SNAKE_CASE_=False  ,       SCREAMING_SNAKE_CASE_=dtypes.floataa  ,       SCREAMING_SNAKE_CASE_=True  ,       SCREAMING_SNAKE_CASE_=5_000  ,       SCREAMING_SNAKE_CASE_=None  ,       SCREAMING_SNAKE_CASE_=DEFAULT_SOURCE_URL  ,       ):
					if fake_data:

										def fake():
															return _DataSet(
															    []  ,       []  ,       fake_data=_UpperCAmelCase  ,       one_hot=_UpperCAmelCase  ,       dtype=_UpperCAmelCase  ,       seed=_UpperCAmelCase							)

										lowercase_					:       Optional[Any]     				=					fake()
										lowercase_					:       List[Any]     				=					fake()
										lowercase_					:       str     				=					fake()
										return _Datasets(train=_UpperCAmelCase  ,       validation=_UpperCAmelCase  ,       test=_UpperCAmelCase							)

					if not source_url:  # empty string check
										lowercase_					:       Optional[Any]     				=					DEFAULT_SOURCE_URL

					lowercase_					:       str     				=					'train-images-idx3-ubyte.gz'
					lowercase_					:       Any     				=					'train-labels-idx1-ubyte.gz'
					lowercase_					:       int     				=					't10k-images-idx3-ubyte.gz'
					lowercase_					:       Optional[Any]     				=					't10k-labels-idx1-ubyte.gz'

					lowercase_					:       Optional[int]     				=					_maybe_download(
					    _UpperCAmelCase  ,       _UpperCAmelCase  ,       source_url + train_images_file							)
					with gfile.Open(_UpperCAmelCase  ,       'rb'							) as f:
										lowercase_					:       Tuple     				=					_extract_images(_UpperCAmelCase							)

					lowercase_					:       Optional[Any]     				=					_maybe_download(
					    _UpperCAmelCase  ,       _UpperCAmelCase  ,       source_url + train_labels_file							)
					with gfile.Open(_UpperCAmelCase  ,       'rb'							) as f:
										lowercase_					:       Dict     				=					_extract_labels(_UpperCAmelCase  ,       one_hot=_UpperCAmelCase							)

					lowercase_					:       List[str]     				=					_maybe_download(
					    _UpperCAmelCase  ,       _UpperCAmelCase  ,       source_url + test_images_file							)
					with gfile.Open(_UpperCAmelCase  ,       'rb'							) as f:
										lowercase_					:       Tuple     				=					_extract_images(_UpperCAmelCase							)

					lowercase_					:       int     				=					_maybe_download(
					    _UpperCAmelCase  ,       _UpperCAmelCase  ,       source_url + test_labels_file							)
					with gfile.Open(_UpperCAmelCase  ,       'rb'							) as f:
										lowercase_					:       List[str]     				=					_extract_labels(_UpperCAmelCase  ,       one_hot=_UpperCAmelCase							)

					if not 0 <= validation_size <= len(_UpperCAmelCase							):
										lowercase_					:       Optional[int]     				=					(
										    'Validation size should be between 0 and '
										    f'''{len(_UpperCAmelCase							)}. Received: {validation_size}.'''
										)
										raise ValueError(_UpperCAmelCase							)

					lowercase_					:       Optional[int]     				=					train_images[:validation_size]
					lowercase_					:       int     				=					train_labels[:validation_size]
					lowercase_					:       Optional[int]     				=					train_images[validation_size:]
					lowercase_					:       Optional[Any]     				=					train_labels[validation_size:]

					lowercase_					:       Dict     				=					{'dtype': dtype, 'reshape': reshape, 'seed': seed}

					lowercase_					:       Union[str, Any]     				=					_DataSet(_UpperCAmelCase  ,       _UpperCAmelCase  ,       **_UpperCAmelCase							)
					lowercase_					:       Dict     				=					_DataSet(_UpperCAmelCase  ,       _UpperCAmelCase  ,       **_UpperCAmelCase							)
					lowercase_					:       List[Any]     				=					_DataSet(_UpperCAmelCase  ,       _UpperCAmelCase  ,       **_UpperCAmelCase							)

					return _Datasets(train=_UpperCAmelCase  ,       validation=_UpperCAmelCase  ,       test=_UpperCAmelCase							)





'''simple docstring'''




import os

def _UpperCamelCase   (       SCREAMING_SNAKE_CASE_							):
     lowercase_					:       List[Any]     				=					len(grid[0]							)
     lowercase_					:       Union[str, Any]     				=					len(SCREAMING_SNAKE_CASE_							)

     lowercase_					:       Union[str, Any]     				=					0
     lowercase_					:       int     				=					0
     lowercase_					:       Union[str, Any]     				=					0

     # Check vertically, horizontally, diagonally at the same time (only works
     # for nxn grid)
     for i in range(SCREAMING_SNAKE_CASE_							):
          for j in range(n_rows - 3							):
               lowercase_					:       Dict     				=					grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i]
               lowercase_					:       Optional[Any]     				=					grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3]

               # Left-to-right diagonal (\) product
               if i < n_columns - 3:
                    lowercase_					:       Tuple     				=					(
                        grid[i][j]
                        * grid[i + 1][j + 1]
                        * grid[i + 2][j + 2]
                        * grid[i + 3][j + 3]
                    )

               # Right-to-left diagonal(/) product
               if i > 2:
                    lowercase_					:       int     				=					(
                        grid[i][j]
                        * grid[i - 1][j + 1]
                        * grid[i - 2][j + 2]
                        * grid[i - 3][j + 3]
                    )

               lowercase_					:       Optional[int]     				=					max(
                   SCREAMING_SNAKE_CASE_  ,       SCREAMING_SNAKE_CASE_  ,       SCREAMING_SNAKE_CASE_  ,       SCREAMING_SNAKE_CASE_							)
               if max_product > largest:
                    lowercase_					:       List[Any]     				=					max_product

     return largest

def _UpperCamelCase   (       ):
     lowercase_					:       int     				=					[]
     with open(os.path.dirname(SCREAMING_SNAKE_CASE_							) + '/grid.txt'							) as file:
          for line in file:
               grid.append(line.strip('\n'							).split(' '							)							)

     lowercase_					:       Dict     				=					[[int(SCREAMING_SNAKE_CASE_							) for i in grid[j]] for j in range(len(SCREAMING_SNAKE_CASE_							)							)]

     return largest_product(SCREAMING_SNAKE_CASE_							)


if __name__ == "__main__":
 print(solution())




